Sometimes in development mode, you want to display exception message to know what happened to your web application. But in production environment, you may want to display general message instead of direct exception message. Laravel error message displayer is a package for displaying exception message or trace string based on APP_DEBUG
condition.
- php 7.0+
- Laravel 5.5+
Install package through composer.
composer require ptdot/errormessage
Next, if using Laravel under 5.5, include the service provider and Facade within your config/app.php
file.
'providers' => [
Ptdot\ErrorMessage\ErrorMessageServiceProvider::class,
],
'aliases' => [
'ErrorMessage' => Ptdot\ErrorMessage\ErrorMessage::class,
]
Since Laravel 5.5+ is using Package Discovery, there is no need manually insert service provider and facade inside your app.php
.
Publish config using command:
php artisan vendor:publish
Set your default error message in config/errormessage.php
.
You may use this package to handle exception display through your REST API or flash message.
<?php
// default usage using facade
try {
throw new \Exception('This is exception message');
} catch (\Exception $exception) {
return response()->json([
'errors' => ErrorMessage::displayExceptionMessage($exception)
], 500);
}
If you want to debug and need display traceAsString
option, you may call traceAsString()
method:
// you may need traceAsString option enabled using this way:
return response()->json([
'errors' => ErrorMessage::traceAsString()->displayExceptionMessage($exception)
], 500);
Want to override default message? Don't worry this package is able to do that.
// Need to override exception message? Don't worry
return response()->json([
'errors' => ErrorMessage::displayExceptionMessage($exception, 'exception message will be overrided with this')
], 500);
Feel free to report an issue or merge request if you want to help this package become better and useful.