heimdal icon indicating copy to clipboard operation
heimdal copied to clipboard

status code is always set to 500

Open leandroruel opened this issue 7 years ago • 3 comments

this line is setting the status code always to 500 https://github.com/esbenp/heimdal/blob/master/src/Formatters/ExceptionFormatter.php#L13

leandroruel avatar Nov 27 '17 16:11 leandroruel

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.

alande-amorim avatar May 04 '18 02:05 alande-amorim

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?

msayanece avatar Sep 24 '18 10:09 msayanece

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

msayanece avatar Sep 24 '18 11:09 msayanece