heimdal
heimdal copied to clipboard
status code is always set to 500
this line is setting the status code always to 500 https://github.com/esbenp/heimdal/blob/master/src/Formatters/ExceptionFormatter.php#L13
I might be late, but I had the same issue and managed to solve it.
This happens because some exceptions are not of the type HttpExceptions. Only those come with http codes attached. For instance: ModelNotFoundException doesn't have any http status code attached to it, so what Laravel does is convert it to HttpNotFoundException as per https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Exceptions/Handler.php#L198 After doing that, you can happily call $e->getStatusCode().
What I did was simply overriding Optimus\Heimdal\ExceptionHandler::render() method adding this bit at the beginning $e = $this->prepareException($e);
After that, you can create your own Exception Formatters and get the http status code by invoking $e->getStatusCode().
I hadn't tested it extensively, but it worked so far.
I am having the same issue. I am unable to solve the problem using your solution. @alande-amorim Is there any other way to solve that?
Found the solution! just use the following code in your model whenever you want to send any exception with custom HTTP status code. Here is the code...
abort(404, 'The resource you are looking for could not be found');
Here is the reference...
https://scotch.io/tutorials/creating-a-laravel-404-page-using-custom-exception-handlers