Welcome Guest, Not a member yet? Register   Sign In
Custom Exception Handler Response Status Code
#4

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?
Reply


Messages In This Thread
RE: Custom Exception Handler Response Status Code - by ltarrant - 11-28-2023, 07:40 PM



Theme © iAndrew 2016 - Forum software by © MyBB