Package that makes RESTful error handling easier than ever.
- Pre-defined errors and response codes
- Run time errors response
- Errors defined at database
1.0.4
Preparation
Open your composer.json file and add the following to the require array:
"esdlabs/reply": "1.*"Install dependencies
php composer installOr
php composer updateAfter installing the package, open your Laravel config file app/config/app.php and add the following lines.
In the $providers array add the following service provider for this package.
'Esdlabs\Reply\ReplyServiceProvider',In the $aliases array add the following facade for this package.
'Reply' => 'Esdlabs\Reply\Facades\Reply',Migrations
php artisan migrate --package=esdlabs/replyreply_errors
- id
- error_code
- response_code
- description
Define your errors at the errors table as follow:
| id | error_code | response_code | description | |---|---|---|---|---| | 1 | '0x001' | 400 | 'Invalid username or password' | | 2 | '0x002' | 406 | ' Valitation failed |
class LoginController extends Controller {
public function store()
{
try
{
...
}
catch (CustomException $e)
{
return Reply::error('0x001');
}
catch (AnotherException $e)
{
return Reply::error('0x002', array('note 1', 'note 2');
}
catch (Exception $e)
{
return Reply::customError('Custom error description', 500, "Note description");
}
}
}HTTP/1.1 400 Bad Request
{
"error_code": "0x001",
"description"": "Invalid username or password"
}HTTP/1.1 406 Not Acceptable
{
"error_code": "0x002",
"description"": "Valitation failed",
"notes" : [
"note 1",
"note 2"
]
}HTTP/1.1 500 Internal Server Error
{
"error_code": "UNK-ERROR",
"description"": "Custom error description",
"notes": "Note description"
}This package is open-sourced software licensed under the MIT license