npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dyna-error

v3.1.2

Published

The dyna-error

Downloads

21

Readme

dynaError

dynaError extends the JavaScript's Error with more data than a message only.

Create more sophisticated errors, with more properties like the

  • userMessage property for the end-user, or
  • with more data for debugging

together with the native Error with the great stack.

Written in TypeScript.

Usage

Examples are in a TypeScript

Import

import {dynaError} from "dyna-error";

Simple example

Instead of

throw new Error ('Service not available');

do this

throw dynaError('Service not available');

or this

throw dynaError({message: 'Service not available'});

where is the same.

Now add some more info about this error

throw dynaError({
  message: 'Service not available',
  userMessage: 'Something went wrong, please retry',
  canRetry: true,
  data: {
    serviceResponse: {...} // Pass more info for debugging
  }
});

Real example

// A fetch function
const getUserSalary = async (userId: string): Promise<IUser> => {
  const salaryServiceAvaialble = await getchUserSalaryAvaialble();
  
  if (!salaryServiceAvaialble) throw dynaError({
    message: 'Service not ready',
    userMessage: 'System overlaoaded, please retry.',
    canRetry: true,
    data: {
      salaryInfo,
    },
  });
  
  return getchUserSalary();
};


// Catch the error
try {
  await getUserSalary(userId);
} catch (e) {
  const error: IDynaError = e;  // It is free cast it, even if the e is not a IDynaError.
  if (error.userMessage) alert(error.userMessage);
  setState(canRetry: !!error.canRetry);
}

API

dynaError arg object

dynaError expects a string to be the message of the error or an object of the IErrorConfig interface.

From the IErrorConfig, only the message is required.

export interface IErrorConfig {
  message: string;        // Error message for debugging.
  userMessage?: string;   // Error message for the end user (ideally translated and without sensitive info).
  code?: number;          // Developer error code, any number to identify the point where the error occurred.
  status?: number;        // Network error status, http code or any status that other parts of the app can understand.
  data?: any;             // Error data for debugging (might contain sensitive info).
  userData?: any;         // Error data that can be delivered to the client/user.
  parentError?: any;      // Parent error
  validationErrors?: any; // Validation errors
  canRetry?: boolean;     // If the action that caused this error can be retried.
}

A full example of a dynaError thrown.

throw dynaError({
  message: 'Salary service not available',
  userMessage: 'Please retry',
  code: 330010,
  status: 200,
  parentError: e,
  validationErrors: { loginName: 'Is required' },
  canRetry: true,
  data: {
    userId: 230130042,
    salaryServiceResponse,
  },
});

dynaError thrown Error

This is what dynaError returns

interface IDynaError extends Error {
  date: Date;               // The date that the error occured
  message: string;          // What you applied on `dynaError`
  userMessage?: string;     // What you applied on `dynaError`
  code?: number;            // What you applied on `dynaError`
  status?: number;          // What you applied on `dynaError`
  data?: any;               // What you applied on `dynaError`
  parentError?: any;        // What you applied on `dynaError`
  validationErrors?: any;   // What you applied on `dynaError`
  canRetry?: boolean;       // What you applied on `dynaError`
  isDynaError: true;        // Informative, just gives the info if you used the `dynaError` for this error
}

A full example of a dynaError catch.

try {
  return getSalary(userId);
} catch(e) {
  const error: IDynaError = e;
  // Here you have all properties of the above IDynaError interface.
  // You are free to cast the e, even if it is not a dynaError.
  // Since all properties of IDynaError are optional the output cast is valid.
}

Sum up

In JavaScript, you can throw anything as an error. It is not wrong to throw an object as an error, but you miss a few things.

Throwing an object as an error You don't have the stack The error is not an Error instance

With dynaError you have rich errors that can consume them easier.

IDynaError is full compatible with Javascript's Error.

Change log

v1

First version

v2

Extends Native JS Error

v3

Returns new object compatible with JS Error.

This make the error serializable for JSON.stringify.