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

easy-errors

v1.1.2

Published

This is a package for validating an object for presence of mandatory attributes. It also provides some errors you can use.

Downloads

23

Readme

easy-errors

npm

This packages offeres often used tasks for validating given input for interfaces. You can validate input and expect mandatory attributes. You may also secure input by encoding it for HTML and avoid XSS attacks.

Installation

npm install easy-errors

Usage

Validation

When you provide data in form of an object for an interface (e.g. method, http interface, ...) you sometimes rely on attributes that have to be present. There is a validation method, which does exactly that.

const { validation } = require('easy-errors');

try {
  // call validation method. First parameter is the data, which should be tested. Second parameter is a 
  // list of attributes, which must be set in the data object (i.e. first parameter). If validation succeeds
  // the promise resolves without a value. If not it rejects with a MissingParametersError.
  
  await validation({ key: 10 }, ['key', 'test']);
  
  // [...] 
} catch(error) {
  console.error(error);
  # -> MissingAttributesError: 'test' is missing
}

Errors

When propagating errors you often want some meta data attached to it (e.g. code, message, ...). Maintaining this is cumbersome and should be done descriptively. This package introduces common errors with useful meta information attached to it that you can use. Also the validation method uses them as well!

const { errors } = require('easy-errors');
throw new errors.MissingParametersError();

You can be provide an object with more meta data, which will be added to the error.

const { errors } = require('easy-errors');
throw new errors.MissingParametersError({ detail: ['There is something wrong'], success: false });

See a full list of the introduced errors:

| Name | Message | Code | Status Code | |:-|:-|:-|:-| | MissingParametersError | Some parameters are missing | 10000 | 400 | | InvalidParametersError | Some parameters are invalid | 10001 | 400 | | NotFoundError | The requested resources was not found | 10002 | 404 | | ForbiddenError | You are not allowed to perform this action | 10003 | 403 | | UnauthorizedError | There was an error with the authorization | 10004 | 401 | | ConfigurationError | There was an configuration error | 10005 | 500 |
| TopicNotFoundError | The requested topic is not available or was not found | 10006 | 404 |
| SubscriptionError | The device could not be subscribed to the topic | 10007 | 500 |
| EndpointNotFoundError | The requested endpoint is not available or was not found | 10008 | 404 |
| ConflictError | The request could not be completed due to a conflict with the current state of the target resource | 10009 | 409 |
| DecodingError | Signature mismatch | 10010 | 401 | | TokenExpirationError | JWT Token lifetime has expired | 10011 | 401 |

The Status Code is a value, which is a recommendation as a response status, when dealing with HTTP/S. The Code is something to identify the error by.

Secure

You can encode input for HTML and thus avoid XSS attacks.

const { secure } = require('easy-errors');
const xssAttack = '<script>alert("DANGER")</script>'

secure.encodeString(xssAttack); // -> &lt;script>alert("DANGER")&lt;/script>

License

Copyright 2019 appcom interactive GmbH Copyright 2019 NanoGiants GmbH

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.