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

@growsari/errors

v0.0.6

Published

Errors package

Downloads

1,170

Readme

Errors

This package provides a semantic, convenient way of throwing errors and serving error messages. Use in conjunction with the response package to render the other details in the response.

Usage

Validation

throw new ValidationError(validatorOutput, message = "Validation error", { httpStatusCode = 404, errorCode = "-999" })

Typically used by validators. Call this with the validator output. There are default values for the message and the other fields, so you can change it as needed by your application.

Record not found

throw new RecordNotFoundError(message = "Record not found", { httpStatusCode = 404, errorCode = "-999", data })

Proposes defaults for when a record is not found. This can be thrown without parameters. Ideally, pass the search/filter parameters into data.

Record already exists

throw new RecordAlreadyExistsError(message = "Record already exists", { httpStatusCode = 400, errorCode = "-999", data })

Proposes defaults for when creating a record with a key that already exists. This can be thrown without parameters. Ideally, pass the create parameters, or conflicting parameters, into data.

Unauthorized

throw new UnauthorizedError(message = "Unauthorized", { httpStatusCode = 401, errorCode = "-999", data })

Proposes defaults for when the user attempts to access resources he does not have access to. Can be thrown without parameters. You may course further details into message, e.g. "User XXX cannot access object YYY", or into data.

Business logic error

throw new BusinessLogicError(message = "An error has occurred", { httpStatusCode = 400, errorCode = "-999", data })

Represents an error message corresponding to business flow. For example, if the user intends to update an already paid for an Order, and it is not allowed in your application, throw a new business logic error. Ideally, include further details in your parameters.

Recommendations

Add meaningful error messages

For the most part, your services will have error messages specific to a situation. "The token you provided is invalid" makes more sense than "Record not found". There may be cases where these need to already be translated before it gets to the consuming application. To entertain these problems, assign a more meaningful error message on instantiation.

Assign unique error codes

Because we're interacting with multiple services, it is useful to be able to declare error codes specific to our application. When you can, utilize the errorCode field in initializing your error instances. Start with a prefix like DP for digital-products, and add a sequence number to it to identify that specific error, to aid in debugging.

Extend these classes for your services

It is likely that your app will need to throw errors with similar parameters. To prevent repeating yourself with the possibility of error in future edits, please create your own lib/ folder extending these errors. For example:

const { RecordNotFoundError, BusinessLogicError } = require('@growsari/errors')

// Translation
class NewRecordNotFoundError extends RecordNotFoundError {
  constructor() {
    super("Invalid ang inyong ginamit", { errorCode: "DP001"})
  }
}

// Possibly repetitive error
class CouponClaimingError extends BusinessLogicError {
  constructor(details) {
    super("May problema sa iyong request. Mangyaring ulitin mamaya", { errorCode: "DP001", data: details })
  }
}

module.exports = {
  RecordNotFoundError: NewRecordNotFoundError,
  CouponClaimingError
}

Having your own errors lib will make it easier to manage messages and parameters later on.