Created
May 23, 2024 16:12
-
-
Save sohag-pro/ba5d45c65accac735f5db69150ffdca9 to your computer and use it in GitHub Desktop.
Laravel 11 Custom json exception in bootstrap/app.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Http\Request; | |
use Illuminate\Foundation\Application; | |
use Illuminate\Foundation\Configuration\Exceptions; | |
use Illuminate\Foundation\Configuration\Middleware; | |
return Application::configure( basePath: dirname( __DIR__ ) ) | |
->withRouting( | |
web: __DIR__ . '/../routes/web.php', | |
api: __DIR__ . '/../routes/api.php', | |
commands: __DIR__ . '/../routes/console.php', | |
health: '/up', | |
) | |
->withMiddleware( function ( Middleware $middleware ) { | |
// | |
} ) | |
->withExceptions( function ( Exceptions $exceptions ) { | |
$exceptions->render( function ( Throwable $e, Request $request ) { | |
if ( !$request->is( 'api/*' ) ) { | |
return true; | |
} | |
$data = null; | |
if ( method_exists( $e, 'errors' ) ) { | |
foreach ( $e->errors() ?? [] as $key => $value ) { | |
$data[$key] = $value[0]; | |
} | |
} | |
$message = $e->getMessage(); | |
$statusCode = 400; | |
if ( method_exists( $e, 'getStatusCode' ) ) { | |
$statusCode = $e->getStatusCode(); | |
if ( $statusCode == 404 ) { | |
$message = '404 Not found'; | |
} | |
if ( $statusCode == 403 ) { | |
$message = '403 Forbidden'; | |
} | |
if ( $statusCode == 401 ) { | |
$message = '401 Unauthorized'; | |
} | |
if ( $statusCode == 405 ) { | |
$message = '405 Method not allowed'; | |
} | |
if ( $statusCode == 422 ) { | |
$message = '422 Unprocessable entity'; | |
} | |
if ( $statusCode == 500 ) { | |
$message = '500 Internal server error'; | |
} | |
if ( $statusCode == 503 ) { | |
$message = '503 Service unavailable'; | |
} | |
if ( $statusCode == 429 ) { | |
$message = '429 Too many requests'; | |
} | |
if ( $statusCode == 419 ) { | |
$message = '419 Page expired'; | |
} | |
if ( $statusCode == 400 ) { | |
$message = '400 Bad request'; | |
} | |
if ( $statusCode == 408 ) { | |
$message = '408 Request timeout'; | |
} | |
if ( $statusCode == 409 ) { | |
$message = '409 Conflict'; | |
} | |
if ( $statusCode == 410 ) { | |
$message = '410 Gone'; | |
} | |
if ( $statusCode == 411 ) { | |
$message = '411 Length required'; | |
} | |
if ( $statusCode == 412 ) { | |
$message = '412 Precondition failed'; | |
} | |
if ( $statusCode == 413 ) { | |
$message = '413 Payload too large'; | |
} | |
if ( $statusCode == 414 ) { | |
$message = '414 URI too long'; | |
} | |
if ( $statusCode == 415 ) { | |
$message = '415 Unsupported media type'; | |
} | |
if ( $statusCode == 416 ) { | |
$message = '416 Range not satisfiable'; | |
} | |
if ( $statusCode == 417 ) { | |
$message = '417 Expectation failed'; | |
} | |
if ( $statusCode == 418 ) { | |
$message = '418 I\'m a teapot'; | |
} | |
if ( $statusCode == 421 ) { | |
$message = '421 Misdirected request'; | |
} | |
} | |
if ( $message == 'Unauthenticated.' ) { | |
$statusCode = 401; | |
} | |
if ( $message == 'Invalid ability provided.' ) { | |
$statusCode = 403; | |
$message = 'You don\'t have permission to access this resource'; | |
} | |
if ( str_contains( $message, 'No query results' ) ) { | |
$message = 'Resource Not found'; | |
$statusCode = 404; | |
} | |
return response()->json( ['error' => $message, 'data' => $data], $statusCode ); | |
} ); | |
} )->create(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Api prefix and versioning