Home >Web Front-end >JS Tutorial >ExtendableError usage in changesets errors package

ExtendableError usage in changesets errors package

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-10 15:40:10424browse

There is this import found at line#2 in Changesets CLI package source code

import { ExitError, InternalError } from "@changesets/errors";

You will learn the below concepts:

1. ExtendableError package

2. ExitError

3. InternalError

ExtendableError usage in changesets errors package

ExtendableError

You will find that ExitError extends ExtendableError.

I assumed ExtendableError is probably another class defined within the same file, that was not the case.

ExtendableError usage in changesets errors package

It is imported from extendable-error. What’s this?

A simple abstract extendable error class that extends Error, which handles the error name, message and stack property.

Install

npm install extendable-error - save

Usage

This usage example from the docs.

import ExtendableError from 'extendable-error';

class SomeError extends ExtendableError {
 constructor(
 message: string,
 public code: number
 ) {
 super(message);
 }
}

let someError = new SomeError('Some error', 0x0001);

Purpose of ExtendableError is to create custom error classes in JavaScript

with consistent behavior for error handling. ExtendableError preserves the error stack and name.

Extending ExtendableError vs Extending Error

I asked ChatGPT to list the differences between extending ExtendableError and extending Error directly and below is picked from the ChatGPT answer:

  1. name Property Mismatch (In Some Environments):
  • Direct Error Inheritance: In some older JavaScript environments (e.g., older versions of Node.js or some non-browser environments), if you extend Error directly, the name property might not always be correctly set to the name of the error class (e.g., ValidationError).

  • ExtendableError: It explicitly sets this.name = this.constructor.name;, which ensures that the name property is set correctly across all environments, even if the environment doesn’t behave correctly

    with Error inheritance.

2. Stack Trace Reliability:

  • Direct Error Inheritance: In some environments, especially Node.js, using Error.captureStackTrace directly in your custom error class ensures the stack trace points to the custom error class. If you

    don’t use this, the stack trace might not behave as expected and could potentially show the wrong location in the call stack.

  • ExtendableError: By using Error.captureStackTrace, ExtendableError ensures that the stack trace is correctly generated, pointing to the location where the error was thrown. This is critical in environments

    like Node.js where debugging is more reliant on stack traces.

ExitError

The below code is picked from Changesets errors package

import { ExitError, InternalError } from "@changesets/errors";

InternalError

The below code is picked from Changets errors package

npm install extendable-error - save

About us:

At Thinkthroo, we study large open source projects and provide architectural guides. We have developed reusable Components, built with tailwind, that you can use in your project. We offer Next.js, React and Node development services.

Book a meeting with us to discuss your project.

ExtendableError usage in changesets errors package

References:

  1. https://github.com/changesets/changesets/blob/main/packages/cli/src/index.ts#L9

  2. https://github.com/changesets/changesets/blob/main/packages/errors/src/index.ts#L13

  3. https://www.npmjs.com/package/extendable-error

The above is the detailed content of ExtendableError usage in changesets errors package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn