@ehmpathy/error-fns

中文文档
1.3.7发布于 2024/09/16MITTypeScriptCommonJS近 30 天下载 1.7万

A standard set of errors and methods for simpler, safer, and easier to read code.

原始使用文档
文档目录6 节
  1. UnexpectedCodePathError
  2. BadRequestError
  3. HelpfulError
  4. getError
  5. .throw
  6. HelpfulError parameter options.cause

error-fns

ci_on_commit deploy_on_tag

Standardized helpful errors and methods for simpler, safer, and easier to read code.

Purpose

Standardize on helpful errors for simpler, safer, easier to read code

  • extend the HelpfulError for observable and actionable error messages
  • leverage the UnexpectedCodePath to eliminate complexity with narrowed codepaths
  • leverage the BadRequestError to make it clear when your logic successfully rejected a request
  • test that logic throws errors ergonomically with getError

install

npm install --save @ehmpathy/error-fns

use

UnexpectedCodePathError

The UnexpectedCodePath error is probably the most common type of error you'll throw.

It's common in business logic that you'll face a scenario that is technically possible but logically shouldn't occur.

For example, lets say you're writing on-vehicle code to check the tire pressures of a vehicle.

// given the tires to check
const tires = Tire[];

// first get the tire pressures for each
const tirePressures: number[] = tires.map(tire => getTirePressure(tire));

// now get the lowest tire pressure
const lowestTirePressure: number | undefined = tires.sort()[0];

In this case, its technically possible that lowestTirePressure could be undefined: there could not be any tires.

However, this is definitely an unexpected code path for our application. We can just halt our logic if we reach here, since we dont need to solve for it.

// sanity check that we do have a tire pressure
if (lowestTirePressure === undefined)
  throw new UnexpectedCodePath('no tire pressures found. can not compute lowest tire pressure', { tires });

With this, the type of lowestTirePressure has been narrowed from number | undefined to just number, so you wont have any type errors anymore.

Further, if this case does occur in real life, then it will be really easy to debug what happened and why. Your error message will include the tires input that caused the problem making this a breeze to debug. No more could not read property 'x' of undefined!

BadRequestError

The BadRequestError is probably the next most common type of error you'll throw.

It's common in business logic that callers will try to execute your logic with inputs that are simply logically not valid. The user may not understand that their input is not valid or there may just be a bug upstream that is resulting in invalid requests.

For example, imagine you have an api that returns the liked songs of a user

const getLikedSongsByUser = ({ userUuid }: { userUuid: string }) => {
  // lookup the user
  const user = await userDao.findByUuid({ uuid: userUuid });

  // if the user does not exist, this is an invalid request. we shouldn't be asked to lookup songs for fake users
  if (!user)
    throw new BadRequestError('user does not exist for uuid', { userUuid });

  // use a property of the user to lookup their favorite songs
  const songs = await spotifyApi.getLikesForUser({ spotifyUserId: user.spotifyUserId });
}

Whatever the reason for a caller making a logically invalid request, it's important to distinguish when your code is at fault versus when the request is at fault.

This is particularly useful when monitoring error rates. Its important to distinguish whether your software failed to execute or whether it successfully rejected the request for observability in monitoring for issues. The BadRequestError enables us to do this easily

For example, libraries such as the simple-lambda-handlers leverage BadRequestErrors to ensure that a bad request both successfully returns an error to the caller but is not marked as an lambda invocation error.

HelpfulError

The HelpfulError is the backbone of this pattern and is what you'll extend whenever you want to create a custom error.

The purpose of this error is to be as helpful as possible to whoever has to read it when its thrown.

To fulfill this goal, the error makes it very easy to specify what the issue was as well as any other information that may be helpful to understanding why it occurred at the time. It then pretty prints this information to make it easy to read when observing.

throw new HelpfulError(
  'the message of the error goes here',
  {
    context,
    relevantInfo,
    potentiallyHelpfulVariables,
    goHere,
  }
)

getError

The getError method is the cherry-on-top of this library.

When writing tests for logic that throws an error in certain situations, you may want to verify that the code indeed throws this error in a test.

The getError utility makes it really easy to assert that the expected error is thrown.

Under the hood, it executes or awaits the logic or promise you give it as input, catches the error that is throw, or throws a NoErrorThrownError of its own. It does the legwork of handling all three cases you may need to use it in and defining the return type correctly.

usecase 1: synchronous logic

const doSomething = () => { throw new HelpfulError('found me'); }

const error = getError(() => doSomething())
expect(error).toBeInstanceOf(HelpfulError);
expect(error.message).toContain('found me')

usecase 2: asynchronous logic

const doSomething = async () => { throw new HelpfulError('found me'); }

const error = await getError(() => doSomething())
expect(error).toBeInstanceOf(HelpfulError);
expect(error.message).toContain('found me')

usecase 3: a promise

const doSomething = async () => { throw new HelpfulError('found me'); }

const error = await getError(doSomething())
expect(error).toBeInstanceOf(HelpfulError);
expect(error.message).toContain('found me')

.throw

The errors extended from the HelpfulError include a .throw static method for convenient usage with ternaries or condition chains

For example, instead of

const phone = customer.phoneNumber ?? (() => {
  throw new UnexpectedCodePathError(
    'customer has relationship without phone number. how is that possible?',
    { customer },
  );
})();

You can simply write

const phone = customer.phoneNumber ?? UnexpectedCodePathError.throw(
  'customer does not have a phone. how is that possible?',
  { customer },
);

HelpfulError parameter options.cause

The .cause parameter is a helpful feature of native errors. It allows you to chain errors together in a way that retains the full stack trace across errors.

For example, sometimes, the original error that your code experiences can be reworded to make it easier to debug. By using the .cause option, you're able to retain the stack trace and reference of the original error while throwing a new, more helpful, error.

// imagine you're using some api which throws an unhelpful error
const apiGetS3Object = async (input: { key: string }) => { throw new Error("no access") }

// you can catch and extend the error to add more context
const helpfulGetS3Object = async (input: { key: string }) => {
  try {
    await getS3Object();
  } catch (error) {
    if (error.message === "no access") throw HelpfulError("getS3Object.error: could not get object", {
      cause: error, // !: by adding the "cause" here, we'll retain the stack trace of the original error
      input,
    })
  }
}

版本与发布记录

@ehmpathy/error-fns 共发布 11 个版本,首次发布于 2023/09/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.7latest2024/09/162 年前
1.3.42024/09/162 年前
1.3.32024/09/162 年前
1.3.22024/07/282 年前
1.3.12024/07/072 年前
1.3.02024/06/212 年前

关于 @ehmpathy/error-fns 的常见问题

@ehmpathy/error-fns 是什么?

A standard set of errors and methods for simpler, safer, and easier to read code.

如何安装 @ehmpathy/error-fns?

在项目目录运行 npm install @ehmpathy/error-fns,或使用 pnpm add / yarn add / bun add。

@ehmpathy/error-fns 支持 TypeScript 吗?

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

@ehmpathy/error-fns 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ehmpathy/error-fns 需要什么版本的 Node.js?

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

@ehmpathy/error-fns 可以商用吗?

@ehmpathy/error-fns 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ehmpathy/error-fns 的下载量有多少?

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

@ehmpathy/error-fns 还在维护吗?

最新版本 1.3.7 发布于 2024/09/16(2 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。