skip to Main Content

I am building api in laravel and I am catching exceptions if they occur and I am returning them as response. The problem is that I only want to show them in my dev enviroment. Setting APP_DEBUG=false doesn’t solve the problem, error message is still in response.

  public function foo()
  {
       try{
           $this->bar();
       }catch(Exception $e){
           return $this->error($e->getMessage(),'Something went wrong', 403);
       }
  }

This returns json for example:

{
    "error": "No query results for model [App\Models\User].",
    "message": "Something went wrong" 
}

I want to have this in my dev enviroment, but on production I would like to have only the message like:

    {
    "error": "500 Server Error",
    "message": "Something went wrong" 
    }

How do I achieve this and is this a good practice to do? I haven’t found any solution yet, except to overwrite message if APP_ENV=production, but I have a feeling that there is a better way to do this.

   protected function error($error, $message = '', $code = 400)
    {

        $this->response['error'] = $error;
        
        $this->response['message'] = $message;

        if(env('APP_ENV') === 'production'){
            $this->response['message'] = '500 Server Error'
        }

        return response()->json($this->response, $code);
    }

2

Answers


  1. You can catch it inside AppExceptionsHandler

    Use the render function to intercept all exceptions with json as response (for api only)

    And within add check which env is it currently being deployed.
    This is the only way to make sure your error overwrite will work only in production.

    Also use config(app.env) === production is best practice instead of using env.

    Now you can throw any kind (custom) exceptions , and all can be "filtered" by this code.

    Extra note about config as best place as pointed out by @dbf (thanks mate)

    Login or Signup to reply.
  2. Agree with @dummy.learn’s answer, the best way to handle exception is inside AppExceptionsHandler, laravel’s all exception will pass through AppExceptionsHandler before it render or report, even those you didn’t catched.

    You can handle exception like this:

    $this->renderable(function (Throwable $e, $request) {
            return new ExceptionResource($e);
    });
    

    ExceptionResource is an API resources from IlluminateHttpResourcesJsonJsonResource which you can see document from here: https://laravel.com/docs/9.x/eloquent-resources.

    And I prefer to check enviroment from app helper not env or config instead, the helper will return a bool, that I think readability is better.

    Example in ExceptionResource, showing error if environment is local dev or unit test:

    public function toArray($request)
    {
        if(app()->environment(['local', 'testing']))
            $error = $this->resource->getMessage();
        else
            $error = 'Server Error';
    
        return [
            'error' => $error,
            'message' => 'Something went wrong'
        ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search