app/Exceptions/Handler.php
/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
Render an exception into an HTTP response.
Render an exception into an HTTP response .
public function render($request, Exception $e)
    {
        $status = 500;
        $exceptionClass = get_class($e);
        if (in_array($exceptionClass, $this->notFoundExceptions)) {
            $status = 404;
            $message = $exceptionClass === RecordNotFoundException::class ?
                $e->getMessage() : 'Could not find what you were looking for.';
            $response = [
                'status' => $status,
                'code' => ResponseStatus::NOT_FOUND,
                'title' => $message,
                'source' => array_merge($request->all(), ['path' => $request->getPathInfo()])
            ];
        } elseif ($e instanceof MethodNotAllowedHttpException) {
            $status = 405;
            $response = [
                'status' => $status,
                'code' => ResponseStatus::HTTP_ERROR,
                'title' => 'This method is not allowed for this endpoint.',
                'source' => array_merge($request->all(),
                    ['path' => $request->getPathInfo(), 'method' => $request->getMethod()])
            ];
        } elseif ($e instanceof ValidationException) {
            $status = 400;
            $response = [
                'status' => $status,
                'code' => ResponseStatus::VALIDATION_FAILED,
                'title' => 'Some validation errors were encountered while processing your request',
                'source' => validation_errors_to_messages($e)
            ];
        } elseif ($e instanceof ApplicationAccessDeniedException) {
            $status = 403;
            $response = [
                'status' => $status,
                'code' => ResponseStatus::HTTP_ERROR,
                'title' => $e->getMessage(),
                'source' => array_merge($request->all(),
                    ['path' => $request->getPathInfo(), 'method' => $request->getMethod()])
            ];
        } elseif (in_array($exceptionClass, $this->clientInputExceptions)) {
            $status = 400;
            $response = [
                'status' => $status,
                'code' => ResponseStatus::INPUT_ERROR,
                'title' => $e->getMessage(),
                'source' => array_merge($request->all(),
                    ['path' => $request->getPathInfo(), 'method' => $request->getMethod()])
            ];
        } elseif ($e instanceof DeletingFailedException) {
            $response = [
                'status' => $status,
                'code' => ResponseStatus::EXCEPTION,
                'title' => $e->getMessage(),
                'source' => array_merge($request->all(),
                    ['path' => $request->getPathInfo(), 'method' => $request->getMethod()])
            ];
        } elseif ($e instanceof AuthenticationException) {
            $status = 401;
            $response = [
                'status' => $status,
                'code' => ResponseStatus::HTTP_ERROR,
                'title' => $e->getMessage(),
                'source' => array_merge($request->all(),
                    ['path' => $request->getPathInfo(), 'method' => $request->getMethod()])
            ];
        } else {
            $response = [
                'status' => $status,
                'code' => ResponseStatus::EXCEPTION,
                'title' => $e->getMessage(),
            ];
        }
        return response()->json(['errors' => [$response]], $status);
    }
public function render ( $ request , Exception $ e ) { $ status = 500 ; $ exceptionClass = get_class ( $ e ) ; if ( in_array ( $ exceptionClass , $ this -> notFoundExceptions ) ) { $ status = 404 ; $ message = $ exceptionClass === RecordNotFoundException :: class ? $ e -> getMessage ( ) : 'Could not find what you were looking for.' ; $ response = [ 'status' => $ status , 'code' => ResponseStatus :: NOT_FOUND , 'title' => $ message , 'source' => array_merge ( $ request -> all ( ) , [ 'path' => $ request -> getPathInfo ( ) ] ) ] ; } elseif ( $ e instanceof MethodNotAllowedHttpException ) { $ status = 405 ; $ response = [ 'status' => $ status , 'code' => ResponseStatus :: HTTP_ERROR , 'title' => 'This method is not allowed for this endpoint.' , 'source' => array_merge ( $ request -> all ( ) , [ 'path' => $ request -> getPathInfo ( ) , 'method' => $ request -> getMethod ( ) ] ) ] ; } elseif ( $ e instanceof ValidationException ) { $ status = 400 ; $ response = [ 'status' => $ status , 'code' => ResponseStatus :: VALIDATION_FAILED , 'title' => 'Some validation errors were encountered while processing your request' , 'source' => validation_errors_to_messages ( $ e ) ] ; } elseif ( $ e instanceof ApplicationAccessDeniedException ) { $ status = 403 ; $ response = [ 'status' => $ status , 'code' => ResponseStatus :: HTTP_ERROR , 'title' => $ e -> getMessage ( ) , 'source' => array_merge ( $ request -> all ( ) , [ 'path' => $ request -> getPathInfo ( ) , 'method' => $ request -> getMethod ( ) ] ) ] ; } elseif ( in_array ( $ exceptionClass , $ this -> clientInputExceptions ) ) { $ status = 400 ; $ response = [ 'status' => $ status , 'code' => ResponseStatus :: INPUT_ERROR , 'title' => $ e -> getMessage ( ) , 'source' => array_merge ( $ request -> all ( ) , [ 'path' => $ request -> getPathInfo ( ) , 'method' => $ request -> getMethod ( ) ] ) ] ; } elseif ( $ e instanceof DeletingFailedException ) { $ response = [ 'status' => $ status , 'code' => ResponseStatus :: EXCEPTION , 'title' => $ e -> getMessage ( ) , 'source' => array_merge ( $ request -> all ( ) , [ 'path' => $ request -> getPathInfo ( ) , 'method' => $ request -> getMethod ( ) ] ) ] ; } elseif ( $ e instanceof AuthenticationException ) { $ status = 401 ; $ response = [ 'status' => $ status , 'code' => ResponseStatus :: HTTP_ERROR , 'title' => $ e -> getMessage ( ) , 'source' => array_merge ( $ request -> all ( ) , [ 'path' => $ request -> getPathInfo ( ) , 'method' => $ request -> getMethod ( ) ] ) ] ; } else { $ response = [ 'status' => $ status , 'code' => ResponseStatus :: EXCEPTION , 'title' => $ e -> getMessage ( ) , ] ; } return response ( ) -> json ( [ 'errors' => [ $ response ] ] , $ status ) ; }
Render an exception into an HTTP response.
Render an exception into an HTTP response .
# the status code # we get the class name for the exception # our response message