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)
    {
        if (str_contains($request->fullUrl(), '/api/')) {
            $code = 1000;
            $msg = $exception->getMessage();
            $debug = [
                'message' => $exception->getMessage(),
                'code' => $exception->getCode(),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => $exception->getTraceAsString(),
            ];
            if ($exception instanceof MethodNotAllowedHttpException) {
                $msg = 'method mot allowed.';
            }
            if ($exception instanceof NotFoundHttpException) {
                $msg = 'Api Not Found.';
            }
            if ($exception instanceof ValidationException) {
                $errors = $exception->validator->errors()->getMessages();
                $errorMessages = [];
                foreach ($errors as $attribute => $messages) {
                    $errorMessages[] = $attribute . ':' . implode(' ', $messages);
                }
                $msg = implode(';', $errorMessages);
            }
            return response()->json([
                'code' => $code,
                'msg' => $msg,
                'debug' => config('app.debug') ? $debug : null,
            ]);
        } else {
            return parent::render($request, $exception);
        }
    }
public function render ( $ request , Exception $ exception ) { if ( str_contains ( $ request -> fullUrl ( ) , '/api/' ) ) { $ code = 1000 ; $ msg = $ exception -> getMessage ( ) ; $ debug = [ 'message' => $ exception -> getMessage ( ) , 'code' => $ exception -> getCode ( ) , 'file' => $ exception -> getFile ( ) , 'line' => $ exception -> getLine ( ) , 'trace' => $ exception -> getTraceAsString ( ) , ] ; if ( $ exception instanceof MethodNotAllowedHttpException ) { $ msg = 'method mot allowed.' ; } if ( $ exception instanceof NotFoundHttpException ) { $ msg = 'Api Not Found.' ; } if ( $ exception instanceof ValidationException ) { $ errors = $ exception -> validator -> errors ( ) -> getMessages ( ) ; $ errorMessages = [ ] ; foreach ( $ errors as $ attribute => $ messages ) { $ errorMessages [ ] = $ attribute . ':' . implode ( ' ' , $ messages ) ; } $ msg = implode ( ';' , $ errorMessages ) ; } return response ( ) -> json ( [ 'code' => $ code , 'msg' => $ msg , 'debug' => config ( 'app.debug' ) ? $ debug : null , ] ) ; } else { return parent :: render ( $ request , $ exception ) ; } }
Render an exception into an HTTP response.
Render an exception into an HTTP response .