{"id":13846924,"url":"https://github.com/EasyGraphQL/easygraphql-format-error","last_synced_at":"2025-07-12T08:30:56.844Z","repository":{"id":57218875,"uuid":"142509011","full_name":"EasyGraphQL/easygraphql-format-error","owner":"EasyGraphQL","description":"Return custom errors with statusCode","archived":false,"fork":false,"pushed_at":"2018-11-22T21:48:28.000Z","size":68,"stargazers_count":23,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-06T16:44:13.530Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/EasyGraphQL.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-07-27T00:41:52.000Z","updated_at":"2023-11-14T06:30:19.000Z","dependencies_parsed_at":"2022-08-29T02:11:50.020Z","dependency_job_id":null,"html_url":"https://github.com/EasyGraphQL/easygraphql-format-error","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/EasyGraphQL/easygraphql-format-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGraphQL%2Feasygraphql-format-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGraphQL%2Feasygraphql-format-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGraphQL%2Feasygraphql-format-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGraphQL%2Feasygraphql-format-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EasyGraphQL","download_url":"https://codeload.github.com/EasyGraphQL/easygraphql-format-error/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EasyGraphQL%2Feasygraphql-format-error/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264962019,"owners_count":23689716,"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-08-04T18:00:50.392Z","updated_at":"2025-07-12T08:30:56.562Z","avatar_url":"https://github.com/EasyGraphQL.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/EasyGraphQL/easygraphql-now/master/logo.png\" alt=\"EasyGraphQL \" width=\"350\"\u003e\n  \u003cbr\u003e\n    easygraphql-format-error\n  \u003cbr\u003e\n  \u003cbr\u003e\n\u003c/h1\u003e\n\nEasy GraphQL Format Error is a node library used to handle the errors and return them with the respective\nstatus code.\n\n## Installation\n```bash\n$ npm install easygraphql-format-error --save\n```\n\n## Usage\nTo get started with the format error, you might need to follow the next steps:\n\n### Basic\n```js\n// App.js\nconst FormatError = require('easygraphql-format-error')\n\nconst formatError = new FormatError()\n// pass the errorName on the context\nconst errorName = formatError.errorName\n\napp.use('/graphql', (req, res) =\u003e {\n  graphqlHTTP({\n    schema,\n    rootValue: root,\n    graphiql: true,\n    context: { errorName },\n    formatError: (err) =\u003e {\n      return formatError.getError(err)\n    }\n  })(req, res)\n})\n\n// Resolver\nuserInformation: ({ isLoggedIn }, { errorName }) =\u003e {\n  if (!isLoggedIn) {\n    throw new Error(errorName.UNAUTHORIZED)\n  }\n\n  return 'My username'\n}\n\n// If the user is not loggedIn the response will be\n{\n  \"errors\": [\n    {\n      \"message\": \"Unauthorized\",\n      \"statusCode\": 401\n    }\n  ],\n  \"data\": null\n}\n```\n\n### With Custom errors\nYou can pass custom error and access those errors from the resolver, calling `errorName.YOUR_ERROR_NAME`\n\n```js\n// App.js\nconst FormatError = require('easygraphql-format-error')\n\nconst formatError = new FormatError([{\n  name: 'INVALID_EMAIL',\n  message: 'The email is not valid',\n  statusCode: '400'\n}])\n// pass the errorName on the context\nconst errorName = formatError.errorName\n\napp.use('/graphql', (req, res) =\u003e {\n  graphqlHTTP({\n    schema,\n    rootValue: root,\n    graphiql: true,\n    context: { errorName },\n    formatError: (err) =\u003e {\n      return formatError.getError(err)\n    }\n  })(req, res)\n})\n\n// Resolver\nfindUserByEmail: ({ email }, { errorName }) =\u003e {\n  const re = /\\S+@\\S+\\.\\S+/;\n  if (!re.test(email)) {\n    throw new Error(errorName.INVALID_EMAIL)\n  }\n\n  return email\n}\n\n// If the email is not valid the response will be\n{\n  \"errors\": [\n    {\n      \"message\": \"The email is not valid\",\n      \"statusCode\": \"400\"\n    }\n  ],\n  \"data\": null\n}\n```\n\n## Demo\nHere is an [Example](https://github.com/EasyGraphQL/easygraphql-format-error/tree/master/example) that can be useful!\n\n## Default errors\n```\n  BAD_REQUEST: {\n    message: 'Bad Request',\n    statusCode: 400\n  },\n  UNAUTHORIZED: {\n    message: 'Unauthorized',\n    statusCode: 401\n  },\n  PAYMENT_REQUIRED: {\n    message: 'Payment Required',\n    statusCode: 402\n  },\n  FORBIDDEN: {\n    message: 'Forbidden',\n    statusCode: 403\n  },\n  NOT_FOUND: {\n    message: 'Not Found',\n    statusCode: 404\n  },\n  METHOD_NOT_ALLOWED: {\n    message: 'Method Not Allowed',\n    statusCode: 405\n  },\n  NOT_ACCEPTABLE: {\n    message: 'Not Acceptable',\n    statusCode: 406\n  },\n  PROXY_AUTHENTICATION_REQUIRED: {\n    message: 'Proxy Authentication Required',\n    statusCode: 407\n  },\n  REQUEST_TIMEOUT: {\n    message: 'Request Timeout',\n    statusCode: 408\n  },\n  CONFLICT: {\n    message: 'Conflict',\n    statusCode: 409\n  },\n  GONE: {\n    message: 'Gone',\n    statusCode: 410\n  },\n  LENGTH_REQUIRED: {\n    message: 'Length Required',\n    statusCode: 411\n  },\n  PRECONDITION_FAILED: {\n    message: 'Precondition Failed',\n    statusCode: 412\n  },\n  PAYLOAD_TOO_LARGE: {\n    message: 'Payload Too Large',\n    statusCode: 413\n  },\n  URI_TOO_LONG: {\n    message: 'URI Too Long',\n    statusCode: 414\n  },\n  UNSUPPORTED_MEDIA_TYPE: {\n    message: 'Unsupported Media Type',\n    statusCode: 415\n  },\n  RANGE_NOT_SATISFIABLE: {\n    message: 'Range Not Satisfiable',\n    statusCode: 416\n  },\n  EXPECTATION_FAILED: {\n    message: 'Expectation Failed',\n    statusCode: 417\n  },\n  IM_A_TEAPOT: {\n    message: 'I\\'m a teapot',\n    statusCode: 418\n  },\n  MISDIRECTED_REQUEST: {\n    message: 'Misdirected Request',\n    statusCode: 421\n  },\n  UNPROCESSABLE_ENTITY: {\n    message: 'Unprocessable Entity',\n    statusCode: 422\n  },\n  LOCKED: {\n    message: 'Locked',\n    statusCode: 423\n  },\n  FAILED_DEPENDENCY: {\n    message: 'Failed Dependency',\n    statusCode: 424\n  },\n  UNORDERED_COLLECTION: {\n    message: 'Unordered Collection',\n    statusCode: 425\n  },\n  UPGRADE_REQUIRED: {\n    message: 'Upgrade Required',\n    statusCode: 426\n  },\n  PRECONDITION_REQUIRED: {\n    message: 'Precondition Required',\n    statusCode: 428\n  },\n  TOO_MANY_REQUESTS: {\n    message: 'Too Many Requests',\n    statusCode: 429\n  },\n  REQUEST_HEADER_FIELDS_TOO_LARGE: {\n    message: 'Request Header Fields Too Large',\n    statusCode: 431\n  },\n  UNAVAILABLE_FOR_LEGAL_REASONS: {\n    message: 'Unavailable For Legal Reasons',\n    statusCode: 451\n  },\n  INTERNAL_SERVER_ERROR: {\n    message: 'Internal Server Error',\n    statusCode: 500\n  },\n  NOT_IMPLEMENTED: {\n    message: 'Not Implemented',\n    statusCode: 501\n  },\n  BAD_GATEWAY: {\n    message: 'Bad Gateway',\n    statusCode: 502\n  },\n  SERVICE_UNAVAILABLE: {\n    message: 'Service Unavailable',\n    statusCode: 503\n  },\n  GATEWAY_TIMEOUT: {\n    message: 'Gateway Timeout',\n    statusCode: 504\n  },\n  HTTP_VERSION_NOT_SUPPORTED: {\n    message: 'HTTP Version Not Supported',\n    statusCode: 505\n  },\n  VARIANT_ALSO_NEGOTIATES: {\n    message: 'Variant Also Negotiates',\n    statusCode: 506\n  },\n  INSUFFICIENT_STORAGE: {\n    message: 'Insufficient Storage',\n    statusCode: 507\n  },\n  LOOP_DETECTED: {\n    message: 'Loop Detected',\n    statusCode: 508\n  },\n  BANDWIDTH_LIMIT_EXCEEDED: {\n    message: 'Bandwidth Limit Exceeded',\n    statusCode: 509\n  },\n  NOT_EXTENDED: {\n    message: 'Not Extended',\n    statusCode: 510\n  },\n  NETWORK_AUTHENTICATION_REQUIRED: {\n    message: 'Network Authentication Required',\n    statusCode: 511\n  }\n```\n\nif the error to throw is `UNAUTHORIZED` just do `throw new Error(errorName.UNAUTHORIZED)`\nand the result will be:\n```\n{\n  \"errors\": [\n    {\n      \"message\": 'Unauthorized',\n      \"statusCode\": 401\n    }\n  ],\n  \"data\": null\n}\n```\n\n\n# License\n### The MIT License\n\nCopyright (c) 2018 EasyGraphQL\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEasyGraphQL%2Feasygraphql-format-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEasyGraphQL%2Feasygraphql-format-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEasyGraphQL%2Feasygraphql-format-error/lists"}