1.1.2 • Published 4 years ago

easy-errors v1.1.2

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

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.

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago