0

I have an old Laravel app that is upgraded to v7. Currently exceptions are returned as an HTML page. But I would like them to be returned as json response. I understand that exceptions are handled in Exceptions\Handler.php:

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array<int, class-string<Throwable>>
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {

        });
    }
}

How can I return the whole exception sack as json?

Edit: The app is old and was using a custom exception handler. Now after the upgrade to V.7 it would be nice to use the default exception handler so that exceptions are returned in json.

Edit 2: When I use

return response()->json([
    'exception' =>exception,
]); 

I get an empty object. I don't want to only return the message of the exception but the whole exception stack.

mnlixk
  • 11
  • 1
  • 6
  • 1
    Laravel automatically returns json response (including exception responses), if request wants json (Content-Type/Accept header is set to `application/json`). So, i think solutions from https://stackoverflow.com/questions/36366727/how-do-you-force-a-json-response-on-every-response-in-laravel can help you. – maximkou Mar 30 '22 at 11:45
  • Yeah true but the app is old and was using a custom handler. So after upgrading laravel I want to use the default exception handler. – mnlixk Mar 30 '22 at 11:50
  • In this case you can override `render` method of handler and return JsonResponse from it, see https://laravel.com/docs/7.x/errors#render-method – maximkou Mar 30 '22 at 11:55
  • Please see my second edit – mnlixk Mar 30 '22 at 13:53
  • Instead of `return response()->json(...)` use like this: `return $this->prepareJsonResponse($request, $exception)`, this expression returns json response in standard laravel way – maximkou Mar 31 '22 at 07:31

1 Answers1

0

You can override render function inside App\Exceptions\Handler.php and you can read more about it https://laravel.com/docs/7.x/errors#render-method

  • please see my edit 2 – mnlixk Mar 30 '22 at 13:53
  • If you want to throw the exception depend on its type you can use `Throwable` for example in `public function render($request, Throwable $e)` you can put `if ($e instanceof ModelNotFoundException) { }` – mahmoud salem Mar 30 '22 at 15:24
  • You can also use this to return the exception as json `return response(['error' => $e->getMessage()], $e->getCode() ?: 400);` you can find the answer here https://stackoverflow.com/questions/64897053/laravel-8-return-all-exceptions-as-json?rq=1 – mahmoud salem Mar 30 '22 at 16:24