{"id":23995388,"url":"https://github.com/shumbo/grpc-web-error-details","last_synced_at":"2025-04-14T19:34:20.043Z","repository":{"id":37699795,"uuid":"357661171","full_name":"shumbo/grpc-web-error-details","owner":"shumbo","description":"Utility function for deserializing `grpc-status-details-bin` metadata on grpc-web","archived":false,"fork":false,"pushed_at":"2022-07-12T04:36:44.000Z","size":132,"stargazers_count":19,"open_issues_count":3,"forks_count":8,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-28T07:51:18.339Z","etag":null,"topics":["grpc","grpc-web"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/grpc-web-error-details","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shumbo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-13T19:05:03.000Z","updated_at":"2025-02-14T14:31:29.000Z","dependencies_parsed_at":"2022-07-12T16:43:39.545Z","dependency_job_id":null,"html_url":"https://github.com/shumbo/grpc-web-error-details","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shumbo%2Fgrpc-web-error-details","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shumbo%2Fgrpc-web-error-details/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shumbo%2Fgrpc-web-error-details/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shumbo%2Fgrpc-web-error-details/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shumbo","download_url":"https://codeload.github.com/shumbo/grpc-web-error-details/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248550632,"owners_count":21122930,"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":["grpc","grpc-web"],"created_at":"2025-01-07T21:59:55.304Z","updated_at":"2025-04-14T19:34:20.009Z","avatar_url":"https://github.com/shumbo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# grpc-web-error-details\n\nUtility function for deserializing the `grpc-web-details-bin` metadata value when using [grpc-web](https://github.com/grpc/grpc-web).\n\n## Motivation\n\n[gRPC provides two models of error handling: standard error model and richer error model](https://grpc.io/docs/guides/error/).\n\nStandard error model, which consists of an error status code and optional string error message, is the \"official\" gRPC error model.\n\nIf you're using protocol buffers, you can also use the richer error model developed and used by Google ([click here for more about the error model](https://cloud.google.com/apis/design/errors#error_model)). This model allows servers to return and clients to consume additional error details as one or more protobuf messages. It also defines a standard set of error message types to cover the most common error cases.\n\n[grpc-web](https://github.com/grpc/grpc-web), the official JavaScript implementation of [gRPC](https://grpc.io/) for browser clients, only supports the standard error model. The error details are encoded in `grpc-web-details-bin` metadata field. However, deserializing the data is not trivial.\n\nThis library provides an utility function that takes an error and returns the deserialized Status message and the array of deserialized error details (comparable to Go's `status.FromError` and similar functions in other languages whose gRPC client supports the richer error model).\n\nIf you're looking to use the richer error model on Node.js, check out [`@stackpath/node-grpc-error-details`](https://github.com/stackpath/node-grpc-error-details).\n\n## Installation\n\n```bash\nyarn add grpc-web-error-details\n# or if you're using npm\nnpm install grpc-web-error-details --save\n```\n\nThis library also needs `grpc-web` and `google-protobuf`, which are required by gRPC Web clients.\n\n## Usage\n\nUse `statusFromError` to deserialize error to Status and an array of error details. It returns a pair of status and an array of error details.\n\n```ts\nfunction statusFromError(err: any): [Status, ErrorDetails[]] | [null, null];\n```\n\nThe function returns null if the passed error is not deserializable.\n\nCall the function with an error object and if the return values are not null, call methods and use type assertions to handle errors.\n\n```ts\nimport * as errorDetails from \"./grpc-web-error-details\";\n\n// promise client\ntry {\n  grpcWebPromiseClient.call();\n} catch (e) {\n  const [status, details] = errorDetails.statusFromError(e);\n  if (status \u0026\u0026 details) {\n    for (const d of details) {\n      // use `instanceof` for type guard\n      if (d instanceof errorDetails.BadRequest) {\n        // use appropriate methods on details for further information\n        for (const v of d.getFieldViolationsList()) {\n          console.log(\n            `Violation at field ${v.getField()}: ${v.getDescription()}`\n          );\n        }\n      }\n    }\n  }\n}\n\n// callback client\ngrpcWebClient.call(req, {}, (err, response) =\u003e {\n  if (!err) {\n    // RPC Success\n    return;\n  }\n  const [status, details] = errorDetails.statusFromError(err);\n  // handle richer error with status and details\n});\n```\n\nTake a look at `sample/client-js/index.ts` for more examples. You can start the sample gRPC server and Envoy proxy by running `docker-compose up` inside `./sample`, and use `yarn ts-node sample/client-js/index.ts` to run the client sample.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshumbo%2Fgrpc-web-error-details","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshumbo%2Fgrpc-web-error-details","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshumbo%2Fgrpc-web-error-details/lists"}