Logo

dev-resources.site

for different kinds of informations.

ExtendableError usage in changesets errors package

Published at
11/15/2024
Categories
javascript
opensource
typescript
class
Author
ramunarasinga-11
Author
16 person written this
ramunarasinga-11
open
ExtendableError usage in changesets errors package

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

import { ExitError, InternalError } from "@changesets/errors";
Enter fullscreen mode Exit fullscreen mode

You will learn the below concepts:

1. ExtendableError package

2. ExitError

3. InternalError

Image description

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.

Image description

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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

export class ExitError extends ExtendableError {
 code: number;
 constructor(code: number) {
 super(`The process exited with code: ${code}`);
 this.code = code;
 }
}
Enter fullscreen mode Exit fullscreen mode

InternalError

The below code is picked from Changets errors package

export class InternalError extends ExtendableError {
 constructor(message: string) {
 super(message);
 }
}
Enter fullscreen mode Exit fullscreen mode

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on an interesting project. Send me an email at [email protected]

My Github - https://github.com/ramu-narasinga
My website - https://ramunarasinga.com
My Youtube channel - https://www.youtube.com/@thinkthroo
Learning platform - https://thinkthroo.com
Codebase Architecture - https://app.thinkthroo.com/architecture
Best practices - https://app.thinkthroo.com/best-practices
Production-grade projects - https://app.thinkthroo.com/production-grade-projects

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

class Article's
30 articles in total
Favicon
Day3 Class and object in java:
Favicon
Day2 java program
Favicon
Day 6 - Object & Class
Favicon
Unflatten in PyTorch
Favicon
Day 4 java class
Favicon
Get 10th Class Math Notes (Matric Part 2) – Download Now for Free
Favicon
Day 18 - Object Oriented Programming
Favicon
Flatten in PyTorch
Favicon
Elevate Learning with Our E-Class: The Ultimate Student Performance Tracking Software
Favicon
Elevate Learning with Class Connect Pro: The Ultimate Student Performance Tracking Software
Favicon
ExtendableError usage in changesets errors package
Favicon
WordPress Training In Hyderabad
Favicon
Understanding the Distinction Between Class and Object in Object-Oriented Programming
Favicon
OOP concepts are importants
Favicon
Class : Inheritance✅ | Struct : Inheritance ❌
Favicon
One JS Class to speak them all
Favicon
Classes in C# | Uzbek
Favicon
Javascript classes explanation in a simple way
Favicon
Unveiling the Power of the :empty() CSS Pseudo-Class
Favicon
Mastering the :not() CSS Pseudo-Class
Favicon
Exploring the :has() CSS Pseudo-Class
Favicon
Unveiling the Java Superhero: The Mysterious Object Class
Favicon
How to use functions in react class components?
Favicon
Safeguarding the Seas: The Strategic Importance of Virginia-Class Submarines
Favicon
Mastering Metaclasses in Python using real-life scenarios
Favicon
TypeScript: Namespace VS Class
Favicon
Understanding Object-Oriented Programming: Unveiling the Power of Classes
Favicon
Learning Python classes
Favicon
Java Polymorphism Best Practices: Writing Clean and Extensible Code
Favicon
This Is How I Prepare The Very Best Regularization / Classification Images Dataset For Stable Diffusion

Featured ones: