Search code examples
laraveldingo-api

How can I change the default error handler in Dingo/Api?


I am building a REST API using Laravel framework and the Dingo API library. Everything works well except that I don't know how I can customize the default error handler when exception happens.

For example, when my code run error, the response is simply this:

{
     "message": "Error",
     "status_code": 500
}

How can I change this response to my own format?


Solution

    • edit the errorFormat in config/api.php as you want.
    
        'errorFormat' => [
            'message' => ':message',
            'errors' => ':errors',
            'code' => 0,
            'status_code' => ':status_code',
            'result' => ':errors',
            'debug' => ':debug',
        ],