esdlabs/reply
Package that makes RESTful error handling easier than ever., (*1)
Features
- Pre-defined errors and response codes
- Run time errors response
- Errors defined at database
Version
1.0.4, (*2)
Installation
Preparation, (*3)
Open your composer.json file and add the following to the require array:, (*4)
"esdlabs/reply": "1.*"
Install dependencies, (*5)
php composer install
Or, (*6)
php composer update
Integration
After installing the package, open your Laravel config file app/config/app.php and add the following lines., (*7)
In the $providers array add the following service provider for this package., (*8)
'Esdlabs\Reply\ReplyServiceProvider',
In the $aliases array add the following facade for this package., (*9)
'Reply' => 'Esdlabs\Reply\Facades\Reply',
Migrations, (*10)
php artisan migrate --package=esdlabs/reply
Database definition
reply_errors
- id
- error_code
- response_code
- description, (*11)
Define your errors at the errors table as follow:, (*12)
| id |
error_code |
response_code |
description |
| 1 |
'0x001' |
400 |
'Invalid username or password' |
| 2 |
'0x002' |
406 |
' Valitation failed |
Usage
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 Output
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"
}
License
This package is open-sourced software licensed under the MIT license, (*13)