@authup/errors

中文文档
1.0.0-beta.73发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 4167

Authup's error classes, error codes and code-to-HTTP-status mapping, with guards that survive JSON.

原始使用文档
文档目录8 节
  1. Where it fits
  2. Installation
  3. Usage
  4. Throw and map to a status
  5. Recognize an error that crossed the wire
  6. API
  7. Notes
  8. License

Authup logo

@authup/errors

Authup's error classes, error codes and code-to-HTTP-status mapping, with guards that survive JSON.
One error vocabulary for the server, the HTTP client and the consoles, so a caller branches on a code, not a message.

npm version CI License: Apache-2.0

Installation · Usage · API · Documentation


Where it fits

Every Authup workspace that throws or inspects an error imports this package: the server throws its classes, the HTTP error middleware maps their code to a status, and @authup/core-http-kit and the consoles read the same codes back out of response bodies. Install it directly when your own code calls the Authup API and wants to branch on what went wrong (isEntityNotFoundError(error), error.code === ErrorCode.JWT_EXPIRED), or when you build a service on Authup's server packages and want to throw errors the same middleware understands.

Installation

npm install @authup/errors

ESM only. Node.js >=26.0.0. It has one runtime dependency, @ebec/core, and no Node-specific APIs.

Usage

Throw and map to a status

import { EntityNotFoundError, ErrorCode, httpStatusFromCode } from '@authup/errors';

const error = new EntityNotFoundError({ entity: 'user' });

error.message; // 'The user was not found.'
error.code === ErrorCode.ENTITY_NOT_FOUND; // true
httpStatusFromCode(error.code); // 404
httpStatusFromCode(ErrorCode.REGISTRATION_DISABLED); // 400, the fallback
  • Every class extends AuthupError and sets its own code and default message. Pass a string to replace the message, or an options object (message, code, data, cause, and entity on the entity classes).
  • data is merged into the JSON form, which is how LoginThrottledError, MfaThrottledError and DeviceVerificationThrottledError carry retryAfter (seconds) to the client.
  • httpStatusFromCode(code, fallback = 400) answers from ERROR_CODE_TO_STATUS; any code not listed there gets the fallback. codeFromHttpStatus goes the other way for errors from a foreign source.

Recognize an error that crossed the wire

import { isAuthupError, isEntityNotFoundError, serializeError } from '@authup/errors';

declare const error: Error;

const body = JSON.parse(JSON.stringify(serializeError(error)));

if (isEntityNotFoundError(body)) {
    // body.code === 'entity_not_found'
}

if (isAuthupError(body)) {
    console.log(body.code, body.message);
}
  • serializeError calls toJSON() when the error has one and otherwise spreads its own properties plus name and message. Use it wherever an error becomes a response body.
  • The guards do not need instanceof. Each class stamps a Symbol.for(...) marker, and toJSON() writes the marker chain as strings under the same key, so a guard recognizes the live error and the parsed JSON alike.
  • normalizeError(value) turns anything thrown (a string, a number, an object) into a real Error.

API

ExportKindPurpose
AuthupErrorclassBase class: a BaseError from @ebec/core with an optional data record merged into toJSON()
BadRequestError, ValidationErrorclassbad_request (400)
UnauthorizedError, AuthHeaderError, BearerTokenMalformedErrorclassidentity_unauthorized, unsupported_auth_header_type, bearer_token_malformed (401)
EntityNotFoundError, EntityConflictError, EntityInactiveError, EntityCredentialsInvalidErrorclassentity_not_found (404), entity_conflict (409), entity_inactive, invalid_credentials (400); option entity names the entity in the message
LoginThrottledError, MfaThrottledError, DeviceVerificationThrottledErrorclass429 with data.retryAfter
InternalErrorclassinternal_error (500)
isError, isAuthupError, is<Class>ErrorfunctionDuck-type guards, one per class
ErrorCodeenumEvery code Authup emits: generic, entity, identity, JWT, JWK, OAuth2, permission and policy codes
ERROR_CODE_TO_STATUS, httpStatusFromCode, codeFromHttpStatusconst, functionCode-to-status mapping
serializeError, normalizeErrorfunctionJSON boundary and unknown-value normalization
AuthupErrorOptions, AuthupErrorInput, AuthupEntityErrorOptions, AuthupEntityErrorInputtypeConstructor input types
matchesInstanceof, hasInstanceof, markInstanceof, serializeInstanceofChain, INSTANCEOF_PROPERTY, BASE_ERROR_INSTANCEre-exportThe @ebec/core marker primitives, for your own error classes

Each class also exports its marker symbol (ENTITY_NOT_FOUND_ERROR_INSTANCE, ...).

Notes

  • isAuthupError needs the marker chain. An object without the @instanceof list (a hand-built { code, message }, or a body from an older server) is not an AuthupError, even when it carries an Authup code. Read body.code directly in that case.
  • A class guard also accepts the plain code. isEntityNotFoundError is true for any AuthupError whose code is entity_not_found, not only for instances of that class. ValidationError and BadRequestError share bad_request, so isValidationError and isBadRequestError are both true for either class.
  • The status lives in the mapping, not on the error. No class carries an HTTP status; codes not in ERROR_CODE_TO_STATUS map to 400 by default.

License

Made with 💚

Published under the Apache 2.0 License.

版本与发布记录

@authup/errors 共发布 48 个版本,首次发布于 2025/01/21;过去 12 个月发布了 44 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0-beta.73latest2026/11/025 天前
1.0.0-beta.722026/10/2711 天前
1.0.0-beta.712026/10/2018 天前
1.0.0-beta.702026/10/1919 天前
1.0.0-beta.692026/10/1424 天前
1.0.0-beta.682026/09/251 个月前

关于 @authup/errors 的常见问题

@authup/errors 是什么?

Authup's error classes, error codes and code-to-HTTP-status mapping, with guards that survive JSON.

如何安装 @authup/errors?

在项目目录运行 npm install @authup/errors,或使用 pnpm add / yarn add / bun add。

@authup/errors 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@authup/errors 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@authup/errors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@authup/errors 可以商用吗?

@authup/errors 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@authup/errors 的下载量有多少?

根据 npm 官方下载统计,@authup/errors 在 2026/10/07 – 2026/11/05 这 30 天内被下载 4,167 次,日均约 139 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@authup/errors 还在维护吗?

最新版本 1.0.0-beta.73 发布于 2026/11/02(5 天前);至今共发布 48 个版本,其中过去 12 个月发布了 44 个;首次发布于 2025/01/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。