I may have spoken to soon.
I get the correct 404 response when throw:
Code:
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
However, if I throw any other Exception I only ever get a 500 response regardless of what I pass as the 2nd parameter for the exception.
Code:
throw new \Exception('Forbidden!', 403);
As suggested in the guide I tried creating my own Exception class implementing the HTTPExceptionInterface. Like in the \CodeIgniter\Exceptions\PageNotFoundException I set the property $code to another status code (403 in my test) but still only 500 in returned by the ExceptionHandler.
Looking that ExceptionHandler handle method where is the $statusCode parameter passed in from?
If I call $exception->getCode(); from within the handle method I get the expected code 403 but the $statusCode parameter returns 500.
As a solution I have now just using $exception->getCode() to set the header status code. But I'm still wondering what the purpose of passing the separate $statusCode parameter is?