Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

futurefuture's avatar

Error Responses

Hey guys,

Been struggling with this for a while...any help would be greatly appreciated.

The app I'm building is heavily API driven, being used by mostly mobile applications.

I really like Laravel validation, but it's error response returns an array of errors and any other error that I return via exception only has one error...

Does anyone have a standardized HTTP error response that can encompass both a single exception error message AND the validation errors returned from the controller?

I'd love to just have one unified error protocol if possible so their is less confusion and ambiguity on the client side.

0 likes
3 replies
rodrigo.pedra's avatar
Level 56

You could customize your app's Exception Handler to normalize the error responses.

The docs outlines how to do it:

https://laravel.com/docs/8.x/errors#rendering-exceptions

You would add something like this:

$this->renderable(function (\Throwable $e, Request $request) {
    if (! $request->expectsJson()) {
        return null; // let Laravel handle it
    }

    if ($e instanceof ValidationException) {
        return null; // let Laravel handle it
    }

    if ($e instanceof AuthenticationException) {
        return null; // let Laravel handle it
    }

    $message = $this->isHttpException($e) ? $e->getMessage() : 'Server Error';
    $statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500;

    return response()->json([
        'message' => $message,
        'errors' => [],
    ], $statusCode);
});

Hope this helps.

Please or to participate in this conversation.