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

@financial-times/n-error

v1.1.0

Published

a convenient error creator with pure manipulation methods > patterns and tools for error parsing, descriptive error creation, and standardised handling

Downloads

23

Readme

n-error

a convenient error creator with pure manipulation methods

patterns and tools for error parsing, descriptive error creation, and standardised handling

npm version npm download node version

CircleCI Coverage Status Known Vulnerabilities Scrutinizer Code Quality Dependencies devDependencies

quickstart

import nError from '@financial-times/n-error';
throw nError({ status: 404, message: 'sessionId not found', type: 'AUTH_FAILURE' });
throw nError.notFound({ message: 'sessionId not found', type: 'AUTH_FAILURE' });
catch (e) {
  throw e.extend({
    handler: 'REDIRECT_TO_INDEX',
    user: { message: 'Authentification Failed' },
  }).remove('message');
}

install

npm install @financial-times/n-error

usage

constructor

import nError from '@financial-times/n-error';

const e = nError({ status: 404 });
console.log(e instanceof nError); // true
console.log(e.stack);  // built-in .stack for stack tracing like Error

manipulation

use .extend() and .remove() to create new copy of the error that maintains the stack trace if you want the manipulation to be pure to avoid unclear behaviour, e.g. async failure logger attached on a lower level

throw e.extend({ handler: 'SOME_ACTION' }).remove('message');

parse fetch error

parse fetch error into NError object with Category for further error handling

Error or other objects would be thrown as it is

parseFetchError() returns a Promise, recommended to use await

/* api-service */
import { parseFetchError } from '@financial-times/n-error';

try{
  await fetch(url, options);
} catch (e) {
  throw await parseFetchError(e); // use `await`
}
/* controller/middleware */
import { CATEGORIES } from '@financial-times/n-error';

try {
  await APIService.call();
} catch (e) {
  // handle the error differently in case of network errors
  if(e.catogary === CATEGORIES.FETCH_NETWORK_ERROR){
    return next(e.extend({
      user: { message: `network error: ${e.code}` }
    }));
  }
  // handle fetch response error in grace 
  // parsed message according to content-type
  // stop `e.json() is not a function` error
  if(e.category === CATEGORIES.FETCH_RESPONSE_ERROR){
    const { errorCodes } = e.message;
    return next(e.extend({
      user: { message: errorCodesToUserMessage(errorCodes) }
    }));
  }
  return next(e);
}

patterns

error sources

  • fetch response error
  • fetch network error
  • Error object
  • other custom Error object

descriptitive error objects

const e = NError({
  status: 404,
  message: 'some type of message', // message from server to be logged
  handler: 'REDIRECT_TO_INDEX', // describe error handling behaviour
  user: { message: 'Authentification Failed' } // override the default message from the server for UI
});

universal error handler

function(e, req, res, next) {
  if(e.handler && e.handler === 'REDIRECT_TO_ORIGINAL'){
    return res.redirect(303, req.originalUrl);
  }
  return res.render('errors', message: e.user.message || e.message );
}

reserved fields

operation, action, category, result (if you use n-auto-logger) are reserved fields that can be overriden, be cautious if you really want to override the default. handler is recommended to specify the error handler behaviour, which would be filtered by n-auto-logger.