{"id":22192510,"url":"https://github.com/3sidedcube/laravel-api-errors","last_synced_at":"2025-07-26T22:32:04.902Z","repository":{"id":40546101,"uuid":"431915861","full_name":"3sidedcube/laravel-api-errors","owner":"3sidedcube","description":"A lightweight package for handling API error responses.","archived":false,"fork":false,"pushed_at":"2024-05-20T11:45:04.000Z","size":22,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"1.x","last_synced_at":"2024-11-10T04:58:07.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/3sidedcube.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-25T16:47:19.000Z","updated_at":"2023-10-12T09:47:45.000Z","dependencies_parsed_at":"2024-05-20T12:42:01.246Z","dependency_job_id":"8bff9a9e-9357-4a12-8f64-2a011b6096e6","html_url":"https://github.com/3sidedcube/laravel-api-errors","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"a6c5a8769486bd1a4409a91fcc023a7d87787bc8"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3sidedcube%2Flaravel-api-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3sidedcube%2Flaravel-api-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3sidedcube%2Flaravel-api-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3sidedcube%2Flaravel-api-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/3sidedcube","download_url":"https://codeload.github.com/3sidedcube/laravel-api-errors/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227726362,"owners_count":17810453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T12:25:40.644Z","updated_at":"2024-12-02T12:25:41.241Z","avatar_url":"https://github.com/3sidedcube.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel API Errors\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/3sidedcube/laravel-api-errors.svg?style=flat-square)](https://packagist.org/packages/3sidedcube/laravel-api-errors)\n[![Total Downloads](https://img.shields.io/packagist/dt/3sidedcube/laravel-api-errors.svg?style=flat-square)](https://packagist.org/packages/3sidedcube/laravel-api-errors)\n![GitHub Actions](https://github.com/3sidedcube/laravel-api-errors/actions/workflows/run-tests.yml/badge.svg)\n\nThis package provides an easy way to manage and handle error response for JSON API's.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require 3sidedcube/laravel-api-errors\n```\n\n## Usage\n\nThere are 2 ways of generating an API error response:\n\n### API Error Exception\n\nThis package provides an exception called `ApiErrorException` which you can extend. There are 3 methods which can\nbe set (2 of which are required):\n\n- `code()` - This is a short string indicating the error code (required).\n- `message()` - A human-readable message providing more details about the error (required).\n- `statusCode()` - This HTTP status code of the error response. By default, this is set to 400 and is optional.\n\nOnce you have an exception, you can use the `fromException()` method to generate an API error response:\n\n```php\nuse ThreeSidedCube\\LaravelApiErrors\\ApiErrorResponse;\nuse ThreeSidedCube\\LaravelApiErrors\\Exceptions\\ApiErrorException;\n\nclass UserBannedException extends ApiErrorException\n{\n    /**\n     * A short error code describing the error.\n     *\n     * @return string\n     */\n    public function code(): string\n    {\n        return 'user_account_banned';\n    }\n\n    /**\n     * A human-readable message providing more details about the error.\n     *\n     * @return string\n     */\n    public function message(): string\n    {\n        return 'User account banned.';\n    }\n\n    /**\n     * The api error status code.\n     *\n     * @return int\n     */\n    public function statusCode(): int\n    {\n        return 403;\n    }\n}\n\n$exception = new UserBannedException();\n\n// This will return an instance of JsonResponse\n$response = ApiErrorResponse::fromException($exception);\n```\n\nReturning this response would generate the following json response:\n\n```json\n{\n    \"error\": {\n        \"code\": \"user_account_banned\",\n        \"message\": \"User account banned.\"\n    }\n}\n```\n\n#### Automatically returning the exception response\n\nIf you want to automatically return the JSON response from the exception, you can add the exception to the `$dontReport`\narray in your `app/Exceptions/Handler.php` like so:\n\n```php\nuse ThreeSidedCube\\LaravelApiErrors\\Exceptions\\ApiErrorException;\n\nprotected $dontReport = [\n    ApiErrorException::class,\n];\n```\n\n### Passing data directly\n\nAlternatively you can use the `create()` method to create an API error response:\n\n```php\nuse ThreeSidedCube\\LaravelApiErrors\\ApiErrorResponse;\n\n// This will return an instance of JsonResponse\n$response = ApiErrorResponse::create('user_account_banned', 'User account banned.', 403);\n```\n\nReturning this response would generate the following json response:\n\n```json\n{\n    \"error\": {\n        \"code\": \"user_account_banned\",\n        \"message\": \"User account banned.\"\n    }\n}\n```\n\n### Additional data\n\nIf you would like to pass additional \"meta\" data to the response, you can use the `meta()` method or pass an array to\nthe create method like so:\n\n```php\nuse ThreeSidedCube\\LaravelApiErrors\\ApiErrorResponse;\nuse ThreeSidedCube\\LaravelApiErrors\\Exceptions\\ApiErrorException;\n\nclass UserBannedException extends ApiErrorException\n{\n    /**\n     * A short error code describing the error.\n     *\n     * @return string\n     */\n    public function code(): string\n    {\n        return 'user_account_banned';\n    }\n\n    /**\n     * A human-readable message providing more details about the error.\n     *\n     * @return string\n     */\n    public function message(): string\n    {\n        return 'User account banned.';\n    }\n\n    /**\n     * The api error status code.\n     *\n     * @return int\n     */\n    public function statusCode(): int\n    {\n        return 403;\n    }\n    \n    /**\n     * Any additional metadata to be included in the response.\n     *\n     * @return array\n     */\n    public function meta(): array\n    {\n        return [\n            'foo' =\u003e 'bar',\n        ];\n    }\n}\n\n$exception = new UserBannedException();\n\n// This will return an instance of JsonResponse\n$response = ApiErrorResponse::fromException($exception);\n```\n\nor\n\n```php\nuse ThreeSidedCube\\LaravelApiErrors\\ApiErrorResponse;\n\n// This will return an instance of JsonResponse\n$response = ApiErrorResponse::create('user_account_banned', 'User account banned.', 403, ['foo' =\u003e 'bar']);\n```\n\nReturning this response would generate the following json response:\n\n```json\n{\n    \"error\": {\n        \"code\": \"user_account_banned\",\n        \"message\": \"User account banned.\",\n        \"meta\": {\n            \"foo\": \"bar\"\n        }\n    }\n}\n```\n\n### Testing\n\n```bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Credits\n\n-   [Ben Sherred](https://github.com/benshered)\n-   [All Contributors](../../contributors)\n\n## License\n\nLaravel API Errors is open-sourced software licensed under the [MIT license](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3sidedcube%2Flaravel-api-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F3sidedcube%2Flaravel-api-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3sidedcube%2Flaravel-api-errors/lists"}