{"id":18780502,"url":"https://github.com/wunderwerkio/jsonapi-error","last_synced_at":"2026-02-03T03:01:41.626Z","repository":{"id":149038650,"uuid":"620834329","full_name":"wunderwerkio/jsonapi-error","owner":"wunderwerkio","description":"Simple class to conveniently create a JSON error that complies with the JSON:API specification.","archived":false,"fork":false,"pushed_at":"2023-04-05T13:48:30.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-24T16:48:04.491Z","etag":null,"topics":["drupal","json-api","php-library","php8","symfony"],"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/wunderwerkio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-03-29T13:15:08.000Z","updated_at":"2024-06-10T11:05:26.482Z","dependencies_parsed_at":null,"dependency_job_id":"882e1c6d-b412-44ea-a61e-56da0262bcb7","html_url":"https://github.com/wunderwerkio/jsonapi-error","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wunderwerkio%2Fjsonapi-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wunderwerkio%2Fjsonapi-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wunderwerkio%2Fjsonapi-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wunderwerkio%2Fjsonapi-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wunderwerkio","download_url":"https://codeload.github.com/wunderwerkio/jsonapi-error/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239696310,"owners_count":19682214,"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":["drupal","json-api","php-library","php8","symfony"],"created_at":"2024-11-07T20:26:41.153Z","updated_at":"2025-12-18T20:30:33.528Z","avatar_url":"https://github.com/wunderwerkio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON:API Error \n[![Test](https://github.com/wunderwerkio/jsonapi-error/actions/workflows/main.yml/badge.svg)](https://github.com/wunderwerkio/jsonapi-error/actions/workflows/main.yml)\n\nThis package provides `JsonApiError` and `JsonApiErrorResponse` classses to conveniently handle errors following the [JSON:API specification](https://jsonapi.org/format/#errors).\n\nThe `JsonApiErrorResponse` extends the `JsonResponse` from `symfony/http-foundation`, so this package is meant to be used in projects using that.\n\n**Table of contents:**\n- [Install](#install)\n- [Usage](#usage)\n    - [Return a simple error response](#return-a-simple-error-response)\n    - [Return multiple errors](#return-multiple-errors)\n    - [Build response from `JsonApiError` objects](#build-response-from-jsonapierror-objects)\n- [Local Development](#local-development)\n- [Credits](#credits)\n\n## Install\n\nInstall this package via composer:\n\n```bash\ncomposer require wunderwerkio/jsonapi-error\n```\n\n## Usage\n\n### Return a simple error response\n\n```php\n\u003c?php\n\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Wunderwerk\\JsonApiError\\JsonApiErrorResponse;\n\nfunction someRequestHandler(): Response {\n  return JsonApiErrorResponse::fromArray([\n    'code' =\u003e 'application_error_code',\n    'title' =\u003e 'An error occured!',\n    'status' =\u003e 500,\n  ]);\n}\n```\n\nThe above code would result in a JSON response with the following payload:\n\n```json\n{\n  \"errors\": [{\n    \"status\": 500,\n    \"code\": \"application_error_code\",\n    \"title\": \"An error occured!\"\n  }]\n}\n```\n\n### Return multiple errors\n\n```php\n\u003c?php\n\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Wunderwerk\\JsonApiError\\JsonApiErrorResponse;\n\nfunction someRequestHandler(): Response {\n  return JsonApiErrorResponse::fromArrayMultiple([\n    [\n      'status' =\u003e 422,\n      'code' =\u003e 'validation_failed',\n      'title' =\u003e 'Invalid request payload',\n      'detail' =\u003e 'The \"name\" field is required.',\n      'source' =\u003e [\n        'pointer' =\u003e '/data/name'\n      ]\n    ],\n    [\n      'status' =\u003e 422,\n      'code' =\u003e 'validation_failed',\n      'title' =\u003e 'Invalid request payload',\n      'detail' =\u003e 'The \"description\" field is required.',\n      'source' =\u003e [\n        'pointer' =\u003e '/data/description'\n      ]\n    ],\n  ]);\n}\n```\n\nThe above code would result in a JSON response with the following payload:\n\n```json\n{\n  \"errors\": [{\n    \"status\": 422,\n    \"code\": \"validation_failed\",\n    \"title\": \"Invalid request payload\",\n    \"detail\": \"The \\\"name\\\" field is required.\",\n    \"source\": {\n      \"pointer\": \"/data/name\"\n    }\n  }, {\n    \"status\": 422,\n    \"code\": \"validation_failed\",\n    \"title\": \"Invalid request payload\",\n    \"detail\": \"The \\\"description\\\" field is required.\",\n    \"source\": {\n      \"pointer\": \"/data/description\"\n    }\n  }]\n}\n```\n\n### Build response from `JsonApiError` objects\n\nTo ease building a response with multiple errors, the response can also be created by constricting it by\npassing an array of `JsonApiError` objects.\n\n```php\n\u003c?php\n\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\JsonResponse;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Wunderwerk\\JsonApiError\\JsonApiError;\nuse Wunderwerk\\JsonApiError\\JsonApiErrorResponse;\n\nfunction someRequestHandler(Request $request): Response {\n  /** @var JsonApiError[] $errors */\n  $errors = [];\n\n  $payload = $request-\u003egetContent();\n  $entity = json_decode($payload, TRUE);\n\n  // Make sure 'name' field is set.\n  if (!array_key_exists('name', $entity['data'])) {\n    $errors[] = JsonApiError::fromArray([\n      'status' =\u003e 422,\n      'code' =\u003e 'validation_failed',\n      'title' =\u003e 'Invalid request payload',\n      'detail' =\u003e 'The \"name\" field is required.',\n      'source' =\u003e [\n        'pointer' =\u003e '/data/name',\n      ],\n    ]);\n  }\n\n  // Make sure 'description' field is set.\n  if (!array_key_exists('description', $entity['data'])) {\n    $errors[] = JsonApiError::fromArray([\n      'status' =\u003e 422,\n      'code' =\u003e 'validation_failed',\n      'title' =\u003e 'Invalid request payload',\n      'detail' =\u003e 'The \"description\" field is required.',\n      'source' =\u003e [\n        'pointer' =\u003e '/data/description',\n      ],\n    ]);\n  }\n\n  if (!empty($errors)) {\n    return new JsonApiErrorResponse($errors);\n  }\n\n  return new JsonResponse([\n    'status' =\u003e 'success',\n  ]);\n}\n```\n\n## Local Development\n\nA local dev environment can be created using `nix`:\n\n```bash\n# For PHP 8.1\nnix develop '#.php81'\n# For PHP 8.2\nnix develop '#.php82'\n# For PHP 8.3\nnix develop '#.php83'\n```\n\n**Run tests**\n\n```bash\ncomposer test\n```\n\n**Lint with PHPStan**\n\n```bash\ncomposer analyze\n```\n\n## Credits\n\nThis project took inspiration from the following awesome projects:\n\n- [`SineMah/json-api-error`](https://github.com/SineMah/json-api-error)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwunderwerkio%2Fjsonapi-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwunderwerkio%2Fjsonapi-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwunderwerkio%2Fjsonapi-error/lists"}