Giter VIP home page Giter VIP logo

errors's Introduction

Errors

A collection of utilities for working with JavaScript/TypeScript errors.

Table of contents

Installation

npm install @mvf4z7/errors

WrappedError

An error class that can be used to wrap an existing error object with a new one. The new error has it's own error message, but the stack trace is composed of the newly created error and the wrapped error.

const error = new Error('message');
const wrappedError = new WrappedError('Additional message', error);

WrappedError.causedBy


A reference to the original error that was wrapped.

const error = new Error('message');
const wrappedError = new WrappedError('additional message', error);

wrappedError.causedBy === error; // true

WrappedError.stack

Because WrappedError extends the built in Error class, a WrappedError instance stores a stack trace that can be referenced through the stack property. A WrappedError's stack trace is composed of the stack trace of the WrappedError's own stack trace, as well as the stack trace of the error that was was wrapped.

function fetchUsers() {
  throw new Error('Network error!');
}

try {
  try {
    fetchUsers();
  } catch (error) {
    throw new WrappedError('Failed to fetch users', error);
  }
} catch (error) {
  console.log(error.stack);

  /* error.stack value
  
  WrappedError: Failed to fetch users
      at Object.<anonymous> (/Users/john-doe/Code/errors-test/index.js:11:11)
      at Module._compile (internal/modules/cjs/loader.js:1068:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
      at Module.load (internal/modules/cjs/loader.js:933:32)
      at Function.Module._load (internal/modules/cjs/loader.js:774:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
      at internal/main/run_main_module.js:17:47
  Caused by: Error: Network error!
      at fetchUsers (/Users/john-doe/Code/errors-test/index.js:4:9)
      at Object.<anonymous> (/Users/john-doe/Code/errors-test/index.js:9:5)
      at Module._compile (internal/modules/cjs/loader.js:1068:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
      at Module.load (internal/modules/cjs/loader.js:933:32)
      at Function.Module._load (internal/modules/cjs/loader.js:774:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
      at internal/main/run_main_module.js:17:47

    */
}

Errors utility functions

.wrap(message: string, error: Error) : WrappedError


A shorthand function for creating an instance of WrappedError. See the WrappedError docs for more details on the returned error object.

try {
  const id = 123;
  getFooById(id);
} catch (error) {
  throw Errors.wrap(`Failed to get Foo with ID ${id}.`, error);
}

.wraps<T extends Error>(error: Error, ErrorClass: ClassType<T>): boolean


Tests if the provided error object is an instance of the provided error class or wraps an error of the provided error class. If the provided error object is an instance of the WrappedError class or another class that extends WrappedError, the errors causeBy property will be recursively checked for instances of the provided error class.

class CustomError extends Error {
  constructor() {
    super('A meaningful error message');
    this.name = 'CustomError';
  }
}

const customError = new CustomerError();
const wrappedError = Errors.wrap('Additional error message', customError);

Errors.wraps(customError, CustomError); // true
Errors.wraps(wrappedError, CustomError); // true
Errors.wraps(customError, TypeError); // false
Errors.wraps(wrappedError, TypeError); // false

.unwrap<T extends Error>(error: Error, ErrorClass: ClassType<T>): T | null


If the provided error object is an instance of WrappedError then the error's causedBy property is unwrapped recursively until an instance of ErrorClass is found, at which time it is returned. If the provided error is not an instance of WrappedError, the error will be returned if it is an instance of ErrorClass.

In either scenario, if an instance of ErrorClass is not found then null is returned.

class CustomError extends Error {
  constructor() {
    super('A meaningful error message');
    this.name = 'CustomError';
  }
}

const customError = new CustomError();
const wrapped = Errors.wrap('Additional error message', customError);
const wrappedTwice = Errors.wrap(
  'Yet another error message',
  wrappedCustomError
);

Errors.unwrap(wrapped, CustomError); // customError
Errors.unwrap(wrappedTwice, CustomError); // customError
Errors.unwrap(customError, CustomError); // customError

Errors.unwrap(wrapped, TypeError); // null
Errors.unwrap(customError, TypeError); // null

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.