Get error page from controller

Hi,

I have a blogging system with pagination.
I want to get 404 if page is not found, for exemple page=21 if there are just 4 pages on the site.
In controller, if I just use go(‘error’), it performs a 302 redirect to 404 error page.
I don’t want this, I want to stay on same URL, so I found the first part of the solution :

if($kirby->request()->get('p') > $pagination->lastPage()){
	$kirby->response()->code(404);
	echo page('error')->render();
        exit();
}

But response code is not 404.
Is it possible to get 404 response code with this solution ?

Thanks

If you want to redirect to the error page, use go('error');

I tried go(‘error’), but it redirects with 302 response code to error page.
I don’t think it’s good for SEO.

You can pass an error code as second parameter:

go('error', 404);

It doesn’t work, it always redirects first with a 302.
If possible I don’t want a redirection, but respond on the same URL with a 404 response code and display the content of error template.