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

@dapi-co/derror

v1.2.2

Published

Dapi error class for Moleculer framework.

Downloads

7

Readme

Source for https://www.npmjs.com/package/@dapi-co/derror

(Class) DError

For flexibility and due to specific requirements at Dapi, DError (Dapi Error), an extension to MoleculerError was created.

DError builds an error stack as the error propagates from the place it was thrown from to the top of the call stack and it also handles putting the first external error to be thrown as the top error.

DError also produces a clean error stack where only our own files are shown in the call stack, and even builds the stack across network calls, where as MoleculerError fails to produce any call stack in case of a network call (basically always).

DError also deals with the case where the previous error in the stack is a MoleculerError or Error.

Constructors

DError(prevError, msg, code = 500, type = '', data = {}, external = false)

Note: This is a special constructor to be used when using the ErrorMap feature.

DError(prevError, type = '', data = {})

Dependencies

  • MoleculerError

Usage

Should always be used by throwing. In case it is inside a catch statement, the previous error should be included. The external parameter decides whether this error should be returned to client or not.

Very low level functions should generally not be external as they don't have information about what operation the trying was trying to do. The first function in the call stack able to give a general error message to the client describing what operation failed should be set to external.

In case of multiple external DErrors, the first one to be set to external will be returned to the user.

throw new DError(null, 'Invalid or expired token', 401, '', {}, true)

Example with try-catch

try {
  await myModel.save()
} catch (error) {
  throw new DError(error, 'Saving database model failed', 500, 'ERR_DB', {}, true)
}
{
  success: 'false',
  msg: 'My Error msg',
  type: 'ERR_DB' //Only if type is set, otherwise not sent
}

To return extra parameters to the client add an external object to the data field of DError that contains all the field to return to the client. An example is shown below.

throw new DError(null, 'Connection ID not found', 404, 'ERR_CONN_NOT_FOUND', {
  external: { errCode: 'CONNECTION_ID_NOT_FOUND' }
}, true)

In this case, errCode: 'CONNECTION_ID_NOT_FOUND' is returned to the client along with the default parameters, resulting in the following return object.

{
  success: 'false',
  msg: 'My Error msg',
  type: 'ERR_CONN_NOT_FOUND',
  errCode: 'CONNECTION_ID_NOT_FOUND'
}

Error Maps

Using DError in a consistent way can be difficult, especially given the relatively large number of arguments passed.

To help with that, DError gives you the ability to set a predefined set of errors and their properties, and then simply tell it which one to use by specifying the type of error to use.

Some examples are given below.

//Define your errors anywhere (e.g. in a DB)
const myErrMap = {
  MY_ERROR_TYPE: {
    msg: 'msg1',
    code: 222,
    external: true
  },

  SOME_OTHER_ERR: {
    msg: 'My message',
    code: 512,
    external: false
  }
}

//On startup or update
DError.SetErrorMap(myErrMap)

//
///Examples throwing an error from the map
//

//Example 1
throw new DError(null, 'MY_ERROR_TYPE', {})
//Which is equivalent to
throw new DError(null, 'msg1', 222, 'MY_ERROR_TYPE', {}, true)

//Example 2
throw new DError(null, 'SOME_OTHER_ERR', {})
//Which is equivalent to
throw new DError(null, 'My message', 512, 'SOME_OTHER_ERR', {}, true)

//You can still throw errors not defined in the map just like before
throw new DError(prevErr, 'Some msg', 512, 'NEW_STUFF', {}, true)

prevError and data parameters must not be defined in the map and must always be provided at creation time of the class.

Notes

The entire stack of DErrors is logged for internal monitoring regardless of whether they are external or not.