Socket
Book a DemoInstallSign in
Socket

easy-errors

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-errors

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

1.1.2
latest
Source
npmnpm
Version published
Weekly downloads
14
600%
Maintainers
1
Weekly downloads
 
Created
Source

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:

NameMessageCodeStatus Code
MissingParametersErrorSome parameters are missing10000400
InvalidParametersErrorSome parameters are invalid10001400
NotFoundErrorThe requested resources was not found10002404
ForbiddenErrorYou are not allowed to perform this action10003403
UnauthorizedErrorThere was an error with the authorization10004401
ConfigurationErrorThere was an configuration error10005500
TopicNotFoundErrorThe requested topic is not available or was not found10006404
SubscriptionErrorThe device could not be subscribed to the topic10007500
EndpointNotFoundErrorThe requested endpoint is not available or was not found10008404
ConflictErrorThe request could not be completed due to a conflict with the current state of the target resource10009409
DecodingErrorSignature mismatch10010401
TokenExpirationErrorJWT Token lifetime has expired10011401

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.

Keywords

easy

FAQs

Package last updated on 07 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.