{"id":50965595,"url":"https://github.com/balcieren/connect-errors-es","last_synced_at":"2026-06-18T20:01:18.750Z","repository":{"id":342656910,"uuid":"1165035952","full_name":"balcieren/connect-errors-es","owner":"balcieren","description":"Define errors in .proto, generate type-safe TypeScript constructors, catch bugs at compile time","archived":false,"fork":false,"pushed_at":"2026-06-11T08:16:29.000Z","size":464,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-11T10:11:21.590Z","etag":null,"topics":["connectrpc","ecmascript","error-handling","grpc","javascript","microservice","nodejs","protobuf","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/balcieren.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","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}},"created_at":"2026-02-23T18:54:47.000Z","updated_at":"2026-06-11T08:15:24.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/balcieren/connect-errors-es","commit_stats":null,"previous_names":["balcieren/connect-errors-es"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/balcieren/connect-errors-es","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/balcieren%2Fconnect-errors-es","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/balcieren%2Fconnect-errors-es/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/balcieren%2Fconnect-errors-es/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/balcieren%2Fconnect-errors-es/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/balcieren","download_url":"https://codeload.github.com/balcieren/connect-errors-es/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/balcieren%2Fconnect-errors-es/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34505423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-18T02:00:06.871Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["connectrpc","ecmascript","error-handling","grpc","javascript","microservice","nodejs","protobuf","typescript"],"created_at":"2026-06-18T20:01:18.039Z","updated_at":"2026-06-18T20:01:18.742Z","avatar_url":"https://github.com/balcieren.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# connect-errors\n\n[![Test](https://github.com/balcieren/connect-errors-es/actions/workflows/test.yml/badge.svg)](https://github.com/balcieren/connect-errors-es/actions/workflows/test.yml)\n[![Lint](https://github.com/balcieren/connect-errors-es/actions/workflows/lint.yml/badge.svg)](https://github.com/balcieren/connect-errors-es/actions/workflows/lint.yml)\n[![npm](https://img.shields.io/npm/v/connect-errors)](https://www.npmjs.com/package/connect-errors)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**Define errors in `.proto`, generate type-safe TypeScript constructors, catch bugs at compile time.**\n\nThe ECMAScript/TypeScript counterpart of [`connect-go-errors`](https://github.com/balcieren/connect-go-errors). A proto-first, isomorphic error handling package for [Connect RPC](https://connectrpc.com) that works on **both server-side** (Node.js, Bun, Deno) **and client-side** (React, Vue, Svelte, browser). Define your errors alongside your service definitions, run `buf generate`, and get fully typed constructor functions with typed parameters — no magic strings, no typos, no runtime surprises.\n\n```protobuf\n// Define in your .proto file\noption (connecterrors.v1.error) = {\n  code: \"ERROR_USER_NOT_FOUND\"\n  message: \"User '{{id}}' not found\"\n  connect_code: CODE_NOT_FOUND\n};\n```\n\n```typescript\n// Use the generated typed constructor (server-side)\nthrow createUserNotFoundError({ id: req.id }); // ← IDE autocomplete, compile-time checked\n```\n\n```typescript\n// Match errors on the client\nif (isUserNotFoundError(err)) {\n  showToast(\"User not found!\");\n}\n```\n\n\u003e Wrong field name? **Won't compile.** Missing field? **IDE warns you.** Wrong error code? **Doesn't exist.**\n\n## Quick Start\n\n```bash\nnpm install connect-errors\n```\n\nFor code generation:\n\n```bash\nnpm install -D connect-errors\n```\n\n---\n\n## Step 1: Configure Buf\n\nFirst, add the protobuf dependency to your `buf.yaml`:\n\n```yaml\n# buf.yaml\nversion: v2\nmodules:\n  - path: proto\ndeps:\n  - buf.build/balcieren/connect-errors\n```\n\nRun `buf dep update` to download the schema.\n\n### Option A: Local Mode\n\nConfigure `buf.gen.yaml` to use your local binary (installed via `npm install -D connect-errors`):\n\n```yaml\n# buf.gen.yaml\nversion: v2\n\nmanaged:\n  enabled: true\n\nplugins:\n  - local: protoc-gen-es\n    out: gen/ts\n    opt: target=ts\n  - local: protoc-gen-connect-es\n    out: gen/ts\n    opt: target=ts\n  # ⭐ Local plugin binary provided by npm\n  - local: protoc-gen-connect-errors-es\n    out: gen/ts\n    opt: target=ts\n\n  # Note: If buf cannot find the plugin in your environment,\n  # you can explicitly invoke it via npx:\n  # - local: [\"npx\", \"connect-errors\"]\n  #   out: gen/ts\n  #   opt: target=ts\n```\n\nThen run `buf` via `npx` so it finds the binary in your `node_modules`:\n\n```bash\nnpx buf generate\n```\n\n### Option B: Basic Protoc (without Buf)\n\nIf you don't use Buf and rely on the standard `protoc` compiler, you can invoke the local plugin via `npx` directly in your terminal command:\n\n```bash\nnpx protoc \\\n  --es_out=gen/ts \\\n  --connect-es_out=gen/ts \\\n  --connect-errors-es_out=gen/ts \\\n  --plugin=protoc-gen-connect-errors-es=./node_modules/.bin/protoc-gen-connect-errors-es \\\n  proto/service.proto\n```\n\n## Step 2: Define Errors in Proto\n\nSee the [full instructions](https://buf.build/balcieren/connect-errors) for defining errors using the custom `connecterrors.v1` proto options.\n\n## Step 3: Available Connect Error Codes\n\nWhen defining errors in your `.proto` file, use the following values for the `connect_code` field. These map to standard [Connect RPC status codes](https://connectrpc.com/docs/protocol/#error-codes).\n\n| `connect_code`             | Description                                                                             |\n| :------------------------- | :-------------------------------------------------------------------------------------- |\n| `CODE_CANCELED`            | The operation was canceled.                                                             |\n| `CODE_UNKNOWN`             | Unknown error.                                                                          |\n| `CODE_INVALID_ARGUMENT`    | Client specified an invalid argument.                                                   |\n| `CODE_DEADLINE_EXCEEDED`   | Deadline expired before operation could complete.                                       |\n| `CODE_NOT_FOUND`           | Some requested entity was not found.                                                    |\n| `CODE_ALREADY_EXISTS`      | Some entity that we attempted to create already exists.                                 |\n| `CODE_PERMISSION_DENIED`   | The caller does not have permission to execute the operation.                           |\n| `CODE_RESOURCE_EXHAUSTED`  | Some resource has been exhausted (e.g. per-user quota).                                 |\n| `CODE_FAILED_PRECONDITION` | Operation was rejected because the system is not in a state required for its execution. |\n| `CODE_ABORTED`             | The operation was aborted.                                                              |\n| `CODE_OUT_OF_RANGE`        | Operation was attempted past the valid range.                                           |\n| `CODE_UNIMPLEMENTED`       | Operation is not implemented or not supported/enabled.                                  |\n| `CODE_INTERNAL`            | Internal errors.                                                                        |\n| `CODE_UNAVAILABLE`         | The service is currently unavailable.                                                   |\n| `CODE_DATA_LOSS`           | Unrecoverable data loss or corruption.                                                  |\n| `CODE_UNAUTHENTICATED`     | The request does not have valid authentication credentials.                             |\n\n## Features \u0026 Usage\n\n### Error Matching\n\nThe library provides `matchesError` and `matchError` (switch-like) utilities to handle specific errors gracefully. Both helpers check headers and `ErrorInfo` details out of the box using your generated `ErrorCodeXxx` constants.\n\n```typescript\nimport { matchError, matchesError } from \"connect-errors\";\nimport { ErrorCodeUserNotFound, ErrorCodeRateLimited } from \"./gen/ts/service_connect_errors\";\n\n// Switch-like matching\nmatchError(err, {\n  [ErrorCodeUserNotFound]: () =\u003e showToast(\"User not found!\"),\n  [ErrorCodeRateLimited]: () =\u003e showToast(\"Please slow down.\"),\n});\n\n// Boolean matching\nif (matchesError(err, ErrorCodeUserNotFound)) {\n  // ...\n}\n```\n\n### Protocol Buffer Details (ErrorInfo \u0026 RetryInfo)\n\nWhen throwing an error from the server side, `connect-errors` automatically attaches standard `google.rpc.ErrorInfo` and `google.rpc.RetryInfo` (if `retryable: true`) details to the `ConnectError`.\n\nYou can extract them on the client:\n\n```typescript\nimport { extractErrorInfo, extractRetryInfo } from \"connect-errors\";\nimport { extractUserNotFoundInfo } from \"./gen/ts/service_connect_errors\";\n\nconst errorInfo = extractErrorInfo(err);\nconsole.log(errorInfo?.reason); // \"ERROR_USER_NOT_FOUND\"\nconsole.log(errorInfo?.metadata); // { id: \"123\" }\n\n// Or use the generated typed extractor for specific errors:\nconst userInfo = extractUserNotFoundInfo(err);\nif (userInfo) {\n  console.log(\"Missing user ID:\", userInfo.metadata.id);\n}\n\nconst retryInfo = extractRetryInfo(err);\nconsole.log(retryInfo?.retryDelay?.seconds);\n```\n\n### Header Metadata\n\nBy default, the library attaches `x-error-code` and `x-retryable` HTTP headers to all created errors. This is useful for load balancers or lightweight clients that don't want to parse protobuf Any details.\n\n```typescript\nimport { extractErrorCode, isRetryable, setHeaderKeys } from \"connect-errors\";\n\n// Optional: Override default header keys globally\nsetHeaderKeys(\"x-custom-code\", \"x-custom-retry\");\n\nconsole.log(extractErrorCode(err)); // \"ERROR_USER_NOT_FOUND\"\nconsole.log(isRetryable(err)); // true\n```\n\n### Global Interceptor\n\nYou can use `createErrorInterceptor` on the server-side to centrally log or trace errors using their definitions. It only triggers on known errors registered in your proto files.\n\n```typescript\nimport { createErrorInterceptor } from \"connect-errors\";\n\nconst loggingInterceptor = createErrorInterceptor((err, def) =\u003e {\n  console.error(\"RPC Error:\", def.code, \"Retryable:\", def.retryable);\n});\n```\n\n## Compatibility\n\n| Environment        | Supported |\n| ------------------ | --------- |\n| Node.js 18+        | ✅        |\n| Bun                | ✅        |\n| Deno               | ✅        |\n| Browser (ESM)      | ✅        |\n| React / Next.js    | ✅        |\n| Vue / Nuxt         | ✅        |\n| Svelte / SvelteKit | ✅        |\n| React Native       | ✅        |\n\n## Contributing\n\nSee [CONTRIBUTING.md](docs/CONTRIBUTING.md) for guidelines.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbalcieren%2Fconnect-errors-es","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbalcieren%2Fconnect-errors-es","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbalcieren%2Fconnect-errors-es/lists"}