{"id":37002092,"url":"https://github.com/pedrosalpr/laravel-api-problem","last_synced_at":"2026-01-14T00:27:33.449Z","repository":{"id":221780319,"uuid":"754121130","full_name":"pedrosalpr/laravel-api-problem","owner":"pedrosalpr","description":"The objective of this package is to facilitate error outputs from API requests in accordance with the RFC 9457 standard.","archived":false,"fork":false,"pushed_at":"2025-09-17T00:56:56.000Z","size":106,"stargazers_count":9,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-17T02:39:49.398Z","etag":null,"topics":["api","api-problem","laravel","php","rfc-7807","rfc-9457"],"latest_commit_sha":null,"homepage":"","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/pedrosalpr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"pedrosalpr"}},"created_at":"2024-02-07T12:50:13.000Z","updated_at":"2025-09-17T00:57:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"f2ede6f6-bece-4844-9f7f-801fde76d3d8","html_url":"https://github.com/pedrosalpr/laravel-api-problem","commit_stats":null,"previous_names":["pedrosalpr/laravel-api-problem"],"tags_count":4,"template":false,"template_full_name":"spatie/package-skeleton-laravel","purl":"pkg:github/pedrosalpr/laravel-api-problem","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedrosalpr%2Flaravel-api-problem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedrosalpr%2Flaravel-api-problem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedrosalpr%2Flaravel-api-problem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedrosalpr%2Flaravel-api-problem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pedrosalpr","download_url":"https://codeload.github.com/pedrosalpr/laravel-api-problem/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedrosalpr%2Flaravel-api-problem/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28406485,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["api","api-problem","laravel","php","rfc-7807","rfc-9457"],"created_at":"2026-01-14T00:27:32.774Z","updated_at":"2026-01-14T00:27:33.433Z","avatar_url":"https://github.com/pedrosalpr.png","language":"PHP","funding_links":["https://github.com/sponsors/pedrosalpr"],"categories":[],"sub_categories":[],"readme":"# Laravel Api Problem\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/pedrosalpr/laravel-api-problem.svg?style=flat-square)](https://packagist.org/packages/pedrosalpr/laravel-api-problem)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/pedrosalpr/laravel-api-problem/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/pedrosalpr/laravel-api-problem/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/pedrosalpr/laravel-api-problem/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/pedrosalpr/laravel-api-problem/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/pedrosalpr/laravel-api-problem.svg?style=flat-square)](https://packagist.org/packages/pedrosalpr/laravel-api-problem)\n\nThe objective of this package is to facilitate error outputs from API requests in accordance with the [RFC 9457](https://datatracker.ietf.org/doc/rfc9457/) standard.\n\nIt transforms error outputs into json format with the following characteristics:\n- header:\n    - `Content-Type: application/problem+json`\n- response:\n    - `type`: URI that identifies the type of error that occured, for example `https://example.com/validation-error`.\n    - `title`: Human-readable identifier, usually the same type field should have the same title field alongside. An example would be something like Form validation failed.\n    - `status`: A copy of the HTTP status code.\n    - `detail`: More information about the specific problem, and if it's appropriate also steps to correct it. For example information about a form validation problem Username is already taken, please choose a different username.\n    - `instance`: An identifier for this specific occurence, which may not be useful to the client but may be included in a bug report for example.\n    - **Additional fields**: Any fields may be added to give additional information, and consuming clients are expected to ignore any that they don't have specific support for.\n        - `timestamp`: (RFC 9457 enhancement): A timestamp indicating when the problem was generated. This helps in logging and tracing the error, especially in systems where timing is critical.\n\nExample:\n\nServer Response:\n```bash\nHTTP/1.1 422 Unprocessable Entity\nContent-Type: application/problem+json\nContent-Language: en\n{\n     \"status\": 422,\n     \"type\": \"https://example.test/validation-error\",\n     \"title\": \"Form validation failed\",\n     \"detail\": \"One or more fields have validation errors. Please check and try again.\",\n     \"instance\": \"http://example.test/api/test/1\",\n     \"timestamp\": \"2024-02-09T11:55:51.252968Z\",\n     \"errors\": [\n        {\n            \"name\": \"username\",\n            \"reason\": \"Username is already taken.\"\n        },\n        {\n            \"name\": \"email\",\n            \"reason\": \"Email format is invalid.\"\n        }\n    ]\n}\n```\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require pedrosalpr/laravel-api-problem\n```\n\n## Usage\n\n### Default Mode (Older Version Laravel 9 and 10)\n\nTo use it, just go to the `register` method within `Exceptions\\Handler.php` and add the following code:\n\n```php\nuse Pedrosalpr\\LaravelApiProblem\\LaravelApiProblem;\n\npublic function register(): void\n{\n    ...\n\n    $this-\u003erenderable(function (\\Throwable $e, Request $request) {\n        if ($request-\u003eis('api/*') || $this-\u003eshouldReturnJson($request, $e)) {\n            $apiProblem = new LaravelApiProblem($e, $request);\n            return $apiProblem-\u003erender();\n        }\n    });\n}\n```\n\nIf you want to debug, just add the following line before the return:\n\n`dd($apiProblem-\u003etoDebuggableArray());`\n\n### Default Mode (Laravel 11)\n\nTo use it, add the following code to the Exception Closure in the `bootstrap/app.php` file:\n\n```php\nuse Pedrosalpr\\LaravelApiProblem\\LaravelApiProblem;\n\n    ...\n\n    -\u003ewithExceptions(function (Exceptions $exceptions) {\n        $exceptions-\u003erender(function (\\Throwable $e, Request $request) {\n            if ($request-\u003eis('api/*') || $this-\u003eshouldReturnJson($request, $e)) {\n                $apiProblem = new LaravelApiProblem($e, $request);\n                return $apiProblem-\u003erender();\n            }\n        });\n    })...\n\n```\n\n#### Creating Exceptions in the Api Problem pattern\n\nThere is the possibility of creating exceptions that extend `LaravelApiProblemException`.\n\nThis already makes it easier to transform the exception into the Api Problem pattern.\n\nTo do this, simply run the following command:\n\n`php artisan laravel-api-problem:exception {name}`\n\nFor example: \n\n`php artisan laravel-api-problem:exception DummyException`\n\nThis creates a class exception inside `Exceptions\\ApiProblem`;\n\n```php\n\u003c?php\n\nnamespace App\\Exceptions\\ApiProblem;\n\nuse Pedrosalpr\\LaravelApiProblem\\Exceptions\\LaravelApiProblemException;\n\nclass DummyException extends LaravelApiProblemException\n{\n\n}\n```\n\n### Custom Mode\n\nIf you want to customize an `Api Problem` class to add your guidelines for which error responses should be returned, simply extend the class with the following command:\n\n`php artisan laravel-api-problem:extend`\n\nFor example:\n\n`php artisan laravel-api-problem:extend DummyApiProblem`\n\nThis creates a class Api Problem inside `ApiProblem`;\n\n```php\n\u003c?php\n\nnamespace App\\ApiProblem;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Http\\Response;\nuse Pedrosalpr\\LaravelApiProblem\\Http\\LaravelHttpApiProblem;\nuse Pedrosalpr\\LaravelApiProblem\\LaravelApiProblem;\n\nclass DummyApiProblem extends LaravelApiProblem\n{\n    public function __construct(\n        protected \\Throwable $exception,\n        protected Request $request\n    ) {\n        match (get_class($exception)) {\n            \\Exception::class =\u003e $this-\u003edummy(),\n            default =\u003e parent::__construct($exception, $request)\n        };\n    }\n\n    protected function dummy()\n    {\n        $extensions = [\n            'errors' =\u003e \"Dummy\",\n        ];\n        $this-\u003eapiProblem = new LaravelHttpApiProblem(\n            Response::HTTP_I_AM_A_TEAPOT,\n            $this-\u003eexception-\u003egetMessage(),\n            $this-\u003egetUriInstance(),\n            $extensions\n        );\n    }\n}\n```\n\nAnd within the match, add the names of the exceptions classes with their respective methods, such as `dummy()`.\n\n#### Older Version Laravel 9 and 10\n\nIn the `Handler.php` file replace the `LaravelApiProblem` object instance to `DummyApiProblem`.\n\n```php\n    $this-\u003erenderable(function (\\Throwable $e, Request $request) {\n        if ($request-\u003eis('api/*') || $this-\u003eshouldReturnJson($request, $e)) {\n            $apiProblem = new DummyApiProblem($e, $request);\n            return $apiProblem-\u003erender();\n        }\n    });\n```\n\n#### Laravel 11\n\nAdd the following code to the Exception Closure in the `bootstrap/app.php` file:\n\n```php\n    -\u003ewithExceptions(function (Exceptions $exceptions) {\n        $exceptions-\u003erender(function (\\Throwable $e, Request $request) {\n            if ($request-\u003eis('api/*') || $this-\u003eshouldReturnJson($request, $e)) {\n                $apiProblem = new DummyApiProblem($e, $request);\n                return $apiProblem-\u003erender();\n            }\n        });\n    })\n```\n\n## Testing\n\nTODO\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Leandro Pedrosa Rodrigues](https://github.com/pedrosalpr)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedrosalpr%2Flaravel-api-problem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpedrosalpr%2Flaravel-api-problem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedrosalpr%2Flaravel-api-problem/lists"}