{"id":15597361,"url":"https://github.com/cdimascio/japi-errors","last_synced_at":"2025-04-24T06:40:47.980Z","repository":{"id":45535442,"uuid":"154177964","full_name":"cdimascio/japi-errors","owner":"cdimascio","description":"⚡Customizable errors for RESTful and HTTP services.","archived":false,"fork":false,"pushed_at":"2022-11-16T11:31:19.000Z","size":228,"stargazers_count":5,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T07:51:14.552Z","etag":null,"topics":["errors","http","http-errors","http-exceptions","http-status-codes","rest"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdimascio.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}},"created_at":"2018-10-22T16:32:11.000Z","updated_at":"2023-09-03T13:57:11.000Z","dependencies_parsed_at":"2023-01-21T19:19:28.282Z","dependency_job_id":null,"html_url":"https://github.com/cdimascio/japi-errors","commit_stats":null,"previous_names":["cdimascio/jwcp-errors"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fjapi-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fjapi-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fjapi-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fjapi-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdimascio","download_url":"https://codeload.github.com/cdimascio/japi-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250580723,"owners_count":21453531,"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":["errors","http","http-errors","http-exceptions","http-status-codes","rest"],"created_at":"2024-10-03T01:21:32.300Z","updated_at":"2025-04-24T06:40:47.962Z","avatar_url":"https://github.com/cdimascio.png","language":"Java","readme":"# japi-errors\n\n![](https://travis-ci.org/cdimascio/japi-errors.svg?branch=master) ![](https://img.shields.io/badge/license-Apache%202-blue.svg)\n\nCustomizable errors for RESTful and HTTP services. \n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/cdimascio/japi-errors/master/assets/japi-errors.png\" width=\"600\"\u003e\n\u003c/p\u003e\n\nOut of the box, **japi-errors** provides [two error formats](#configure) or enables you to [provide](#customize) your own. \n\nAll 'out of the box' errors are Jackson ready and can be serialized as *JSON*, *XML*, and *YAML*. If you'd like to use GSON or something else, [create a custom error creator](#customize).\n\n## Install\n\nGradle\n\n```groovy\ncompile 'io.github.cdimascio:japi-errors:1.3.1'\n```\n\nMaven\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.cdimascio\u003c/groupId\u003e\n  \u003cartifactId\u003ejapi-errors\u003c/artifactId\u003e\n  \u003cversion\u003e1.3.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Usage\n\nImport error methods\n\n```java\nimport static io.github.cdimascio.japierrors.ApiError.badRequest;\n// ...\n```\n\nThrow any HTTP error and optionally pass an exception or custom message.\n\n```java\nthrow notFound();\nthrow badRequest(\"id required.\");\nthrow internalServerError(exception);\n// ...\n```\n\nAssign\n\n```shell\nApiError error = unauthorized();\n```\n\nSee [examples](#examples) with Spring MVC\n\n## Configure\n\n**japi-errors** supports two error formats \"out of the box\". They are enabled as follows:\n\n```\nApiError.creator(ApiErrorCreators.BASIC); // The default\nApiError.creator(ApiErrorCreators.WCP);\n```\n\n### Basic (default)\n\n```json\n{\n  \"code\": 400,\n  \"error\": \"id required.\"\n}\n```\n\n### WCP\n\n```json\n{\n  \"trace\": \"1f96a430-dfd8-11e8-9f32-f2801f1b9fd1\",\n  \"errors\": [{\n    \"code\": \"bad_request\",\n    \"message\": \"id required.\"\n  }]\n}\n```\n\n## Customize\n\n**japi-errors** enables developers to craft custom error objects. To do so, implement the `ApiErrorCreator` interface, then pass an instance of your creator to `ApiError.creator(myApiErrorCreator)`\n\nTo see a working example, check out this [source code](https://github.com/cdimascio/japi-errors/blob/master/src/main/java/io/github/cdimascio/apierrors/basic/ApiErrorBasic.java)\n\nExample:\n\n```java\n// Create an api error creator\npublic class MyApiErrorCreator implements IApiErrorCreator {\n    @Override\n    public ApiError create(HttpStatus status, String message) {\n        return new MyApiError(message);\n    }\n\n    @Override\n    public ApiError create(HttpStatus status, Throwable t) {\n        return new MyApiError(t.getMessage());\n    }\n}\n```\n\n```java\n// Create a custom api error object\n// Add Json ignore properties (see source code link above)\npublic class MyApiError extends ApiError {\n  @JsonProperty\n  private String message;\n  \n  MyApiError() {\n      super();\n  }\n  \n  MyApiError(String message) {\n    this.message = message;\n  }\n  \n  public String getMessage() {\n    return message;\n  }\n}\n```\n\n## Examples\n\nCheck out the following examples to see **japi-errors** in use: \n\n- with [Spring MVC](https://github.com/cdimascio/kotlin-spring-mvc-template/blob/master/src/main/kotlin/api/users/UsersController.kt#L38)\n\n## License\n[Apache 2](LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fjapi-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdimascio%2Fjapi-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fjapi-errors/lists"}