Laravel API Exception Handler with readable error messages. However, this package is specifically designed to be compatible with older PHP versions and Laravel or Lumen legacy projects.
composer require ohansyah/laravel-exception-handler
$app->register(Ohansyah\ExceptionHandler\ExceptionHandlerServiceProvider::class);
$app->configure('exception-handler');
composer dump-autoload
php artisan exception-handler:publish
use the Traits and simpy using available method resException(), resError(), resSuccess()
use Ohansyah\ExceptionHandler\Traits\ResponseTransform;
class Name
{
use ResponseTransform;
...
resException(Exception $exception);
resError($responseMessage, int $responseCode = 400, array $additionals = []);
resSuccess($responseMessage, int $responseCode = 200);
resThrowable(Throwable $throwable);
}
// app\Exceptions\Handler.php
if (!($exception instanceof ValidationException) && !($exception instanceof NotFoundHttpException)) {
return $this->resException($exception);
}
Request:
GET {{your-base-url}}/exception-handler/error?code=500
Response:
{
"status": "error",
"message": "Kami telah menerima laporan bahwa layanan ini sedang mengalami gangguan. Mohon maaf atas ketidaknyamanannya dan mohon menunggu sebentar.",
"errors": {
"message": [
"Kami telah menerima laporan bahwa layanan ini sedang mengalami gangguan. Mohon maaf atas ketidaknyamanannya dan mohon menunggu sebentar."
]
},
"title": "Ups!! Terjadi Kendala Teknis",
"exception": null
}
resources\lang\en\http.php
resources\lang\id\http.php
// config\exception-handler.php
'trace_exception' => env('SERVICE_TRACE_EXCEPTION', true),
The Laravel Exception Handler package is open-sourced software licensed under the MIT License.