app/Exceptions/Handler.php
/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
Render an exception into an HTTP response.
Render an exception into an HTTP response .
public function render($request, Exception $exception)
    {
        $debug = config('app.debug');
        $message = '';
        $status_code = 500;
        if ($exception instanceof ModelNotFoundException) {
            $message = 'Resource is not found';
            $status_code = 404;
        } elseif ($exception instanceof NotFoundHttpException) {
            $message = 'Endpoint is not found';
            $status_code = 404;
        } elseif ($exception instanceof MethodNotAllowedHttpException) {
            $message = 'Method is not allowed';
            $status_code = 405;
        } else if ($exception instanceof ValidationException) {
            $validationErrors = $exception->validator->errors()->getMessages();
            $validationErrors = array_map(function ($error) {
                return array_map(function ($message) {
                    return $message;
                }, $error);
            }, $validationErrors);
            $message = $validationErrors;
            $status_code = 405;
        } else if ($exception instanceof QueryException) {
            if ($debug) {
                $message = $exception->getMessage();
            } else {
                $message = 'Query failed to execute';
            }
            $status_code = 500;
        }
        $rendered = parent::render($request, $exception);
        $status_code = $rendered->getStatusCode();
        if (empty($message)) {
            $message = $exception->getMessage();
        }
        $errors = [];
        if ($debug) {
            $errors['exception'] = get_class($exception);
            $errors['trace'] = explode("\n", $exception->getTraceAsString());
        }
        return response()->json(
            [
                'status'    => 'error',
                'message'   => $message,
                'data'      => null,
                'errors'    => $errors,
            ],
            $status_code
        );
    }
public function render ( $ request , Exception $ exception ) { $ debug = config ( 'app.debug' ) ; $ message = '' ; $ status_code = 500 ; if ( $ exception instanceof ModelNotFoundException ) { $ message = 'Resource is not found' ; $ status_code = 404 ; } elseif ( $ exception instanceof NotFoundHttpException ) { $ message = 'Endpoint is not found' ; $ status_code = 404 ; } elseif ( $ exception instanceof MethodNotAllowedHttpException ) { $ message = 'Method is not allowed' ; $ status_code = 405 ; } else if ( $ exception instanceof ValidationException ) { $ validationErrors = $ exception -> validator -> errors ( ) -> getMessages ( ) ; $ validationErrors = array_map ( function ( $ error ) { return array_map ( function ( $ message ) { return $ message ; } , $ error ) ; } , $ validationErrors ) ; $ message = $ validationErrors ; $ status_code = 405 ; } else if ( $ exception instanceof QueryException ) { if ( $ debug ) { $ message = $ exception -> getMessage ( ) ; } else { $ message = 'Query failed to execute' ; } $ status_code = 500 ; } $ rendered = parent :: render ( $ request , $ exception ) ; $ status_code = $ rendered -> getStatusCode ( ) ; if ( empty ( $ message ) ) { $ message = $ exception -> getMessage ( ) ; } $ errors = [ ] ; if ( $ debug ) { $ errors [ 'exception' ] = get_class ( $ exception ) ; $ errors [ 'trace' ] = explode ( " \n " , $ exception -> getTraceAsString ( ) ) ; } return response ( ) -> json ( [ 'status' => 'error' , 'message' => $ message , 'data' => null , 'errors' => $ errors , ] , $ status_code ) ; }
Render an exception into an HTTP response.
Render an exception into an HTTP response .