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

snugerror

v0.0.6

Published

Snugerror is a library that facilitates the handling of errors in a given routine; was raised aimed at separating the code focused on business rules and error checking.

Downloads

19

Readme

Snugerror

Installation:

npm install snugerror
# or
yarn add snugerror

Methods:

Basic example with next method:

Triggers the next instruction for error checking, starts at position 0 of the array.

.next(...args)

import handleError from 'snugerror'

let errors = handleError([
    function(data){
        // Your routine for data validation here
        if (!data) throw new Error('You error: error 1')
    },
    function(a, b){
        // Your routine for data validation here
        if (a !== b) throw new Error('Values is not equals')
    },
    (data) => {
        // Your routine for data validation here
        if(!data?.name) throw new Error('Type your name')
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    let data = {
        a: 1,
        b: 1,
        name: null
    }
    
    iterator.next(data)

    console.log('hello') // this console.log will running

    data.b = 3

    iterator.next(data.a, data.b) // will launch error 'Values is not equals'

    iterator.next(data)

    res.send('Hello World') // This answer will not be delivered
});

Method repeatNext:

Repeats execution of .next() a certain number of times passed as an argument.

.repeatNext(total_repeat)(...args)

import handleError from 'snugerror'

let errors = handleError([
    function(data){
        // Your routine for data validation here
        if (!data) throw new Error('You error: error 1')
    },
    function(data){
        // Your routine for data validation here
        if (data.a !== data.b) throw new Error('Values is not equals')
    },
    (data) => {
        // Your routine for data validation here
        if(!data?.name) throw new Error('Type yout name')
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    let data = {
        a: 1,
        b: 1,
        name: null
    }

    iterator.repeatNext(3)(data) // Will iterate over function 0, 1 and 2 of the array

    res.send('Hello World') // This answer will not be delivered
});

Error method:

Returns an error from a specific position, or from a specific function.

.error(<position>)(...args) .error(<name_function>)(...args)

import handleError from 'snugerror'

let errors = handleError([
    function (data){
        // Your routine for data validation here
        if (!data) throw new Error('You error: error 1')
    },
    function check_id_user (data){
        // Your routine for data validation here
        if(!data?.id) throw new Error('User id is not valid')
    },
    (a, b) => {
        // Your routine for data validation here
        
        if (a !== b) throw new Error('Values is not equals')
    }    
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    let data = {
        a: 1,
        b: 3,
        name: null
    }
    
    iterator.error(0)(data) // will launch <Error: 'You error: error 1'>

    iterator.error(2)(data.a, data.b) // will launch <Error: 'Values is not equals'>

    iterator.error('check_id_user')(data) // will launch <Error: 'User id is not valid'>

    res.send('Hello World') // This answer will not be delivered
});

Message method:

Passes a message to the function context.

.message(<your message error>).error .message(<your message error>).next .message([<your message error 1>, <your message error 2>]).repeatNext

import handleError from 'snugerror'

let errors = handleError([
    function(data){
        // Your routine for data validation here
        if (!data) throw new Error(this.message || 'Failed operation') // will launch <Error:  'You error: error 1'>
    },
    function(data){
        // Your routine for data validation here
        if (!data) throw new Error(this.message || 'Failed operation') // will launch <Error:  'this is error 2'>
    },
    function(data){
        // Your routine for data validation here
        if (!data) throw new Error(this.message || 'Failed operation') // will launch <Error:  'this is error 3'>
    },
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    iterator.message('You error: error 1').next(undefined)
    iterator.message('You error: error 1').error(0)(undefined)
    iterator.message(['this is error 2', 'this is error 3']).repeatNext(2)(undefined) 

    res.send('Hello World') // This answer will not be delivered
});

Method errors:

Returns a list of errors issued up to the call of this function.

import handleError from 'snugerror'

let errors = handleError([
    function (data){
        // Your routine for data validation here
        if (!data) throw new Error('You error: error 1')
    },
    function check_id_user (a, b){
        // Your routine for data validation here
        if(!data?.id) throw new Error('User id is not valid')
    },
    (data) => {
        // Your routine for data validation here
        if (a !== b) throw new Error('Values is not equals')
    }    
])

...

app.get("/", (req, res) => {
    
    const iterator = errors()

    try{
        iterator.next(undefined)
        iterator.next(1, 3)
        iterator.next({name: null})
    } catch (error){}

    console.log(iterator.errors) // [<uncaughtException>, <uncaughtException>, <uncaughtException>]
    ...
});

Method checkAll:

Cycles through all error handling functions.

import handleError from 'snugerror'

let errors = handleError([
    function (data){
        // Your routine for data validation here
        if (!data) throw new Error('You error: error 1')
    },
    function check_id_user (a, b){
        // Your routine for data validation here
        if(!data?.id) throw new Error('User id is not valid')
    },
    (data) => {
        // Your routine for data validation here
        if (a !== b) throw new Error('Values is not equals')
    }    
])

...

app.get("/", (req, res) => {
    
    const iterator = errors()
    iterator.checkAll(undefined)
    ...
});

Attributes:

Parameter passing: this.params

Pass parameters in constructor.

import handleError from 'snugerror'

let errors = handleError([
    function(data){
        console.log(this.params) // [{foo: 'bar'}, 'hello', 'world', 1]
        ...
    },
    function(a, b){
        const [obj, var_1, var_2, var_3] = this.params

        console.log(obj) // {foo: 'bar'}
        console.log(var_1) // hello
        console.log(var_2) // world
        console.log(var_3) // 1
        ...
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors({foo: 'bar'}, 'hello', 'world', 1)
    ...
});

Return message: this.message

Returns the message passed when the error was issued, see message method

Error handler/function name: this.errorName

Returns the name of the exception-throwing function, ie the name of the error/function handle if you prefer.

import handleError from 'snugerror'

let errors = handleError([
    function check_payment(data){
        console.log(this.errorName) // check_payment
        ...
    },
    function verify_user_id(data){
        console.log(this.errorName) // verify_user_id
        ...
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()
    ...
});

Method return: this.methods

Returns all methods.

import handleError from 'snugerror'

let errors = handleError([
    function (data){
        console.log(this.methods) // {next, repeatNext, error, errors, message}
        ...
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()
    ...
});

Throw attribute: this.throw

Throw an exception.

this.throw(<error_name>, <error_message>) or this.throw(<error_message>)

import handleError from 'snugerror'

let errors = handleError([
    function (data) {
        if (!data?.name) this.throw('E001', 'Here is a error') // <E001: Here is a error> instead of <Error: Here is a error>
    }
])

// ----------------------------or---------------------------

let errors = handleError([
    function (data) {
        if (!data?.name) this.throw('Here is a error') // <Error: Here is a error>
    }
])

// ----------------------------or---------------------------

let errors = handleError({
    'E001': (foo) => 'erro 1 '+foo
}, [
    function (data) {
        if (!data?.name) this.throw('E001', 'here') // <E001: erro 1 here>
    }
])
...

app.get("/", (req, res) => {
    const iterator = errors()
    ...
});

Other settings:

Dictionary errors:

import handleError from 'snugerror'

let errors = handleError({
    'E001': () => 'Example error 1',
    'E002': (a, b) => `Values is not equals: ${a} !== ${b}`
}, [
    function(data){
        // Your routine for data validation here
        if (!data) this.throw('E001') // will launch <E001: 'Example error 1'>
    },
    function(a, b){
        // Your routine for data validation here
        if (a !== b) this.throw('E002', a, b) // will launch <E002: 'Values is not equals: 1 !== 3'>
    },
    (data) => {
        // Your routine for data validation here
        if(!data?.name) throw new Error('Type yout name') // will launch <Error: 'Type yout name'>
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    let data = {
        a: 1,
        b: 1,
        name: null
    }
    
    iterator.next(data)

    console.log('hello') // this console.log will running

    data.b = 3

    iterator.next(data.a, data.b) // will launch error 'Values is not equals'

    iterator.next(data)

    res.send('Hello World') // This answer will not be delivered
});

Create method:

In this way it is possible to configure an error message dictionary and an exception listener.


//----------------config.ts-------------------------------
import snugerror from 'snugerror'

export const handleError = snugerror.create({
    dictionary: {
        'E001': () => 'Example error 1',
        'E002': (a, b) => `Values is not equals: ${a} !== ${b}`
        'E003': () => 'ERRO 3 here',
    },
    onError(error){
        console.log(error.name, error.message)
    }
})

//----------------any-file.ts-------------------------------

let errors = handleError([
    function(data){
        // Your routine for data validation here
        if (!data) this.throw('E001') // will launch <E001: 'Example error 1'> Obs.: error by dictionary in config.ts
    },
    function(a, b){
        // Your routine for data validation here
        if (a !== b) this.throw('E002', a, b) // will launch <E002: 'Values is not equals: 1 !== 3'> Obs.: error by dictionary in config.ts
    },
    (data) => {
        // Your routine for data validation here
        if(!data?.name) throw new Error('Type yout name') // will launch <Error: 'Type yout name'>
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    let data = {
        a: 1,
        b: 1,
        name: null
    }
    
    iterator.next(data)

    console.log('hello') // this console.log will running

    data.b = 3

    iterator.next(data.a, data.b) // will launch error 'Values is not equals'

    iterator.next(data)

    res.send('Hello World') // This answer will not be delivered
});

Create method and local dictionary:

It is possible to rewrite certain dictionary items if I pass a local dictionary.


//----------------config.ts-------------------------------
import snugerror from 'snugerror'

export const handleError = snugerror.create({
    dictionary: {
        'E001': () => 'Example error 1',
        'E002': (a, b) => `Values is not equals: ${a} !== ${b}`
        'E003': () => 'ERRO 3 here',
    },
    onError(error){
        console.log(error.name, error.message)
    }
})

//----------------any-file.ts-------------------------------

let errors = handleError({
    'E001': () => 'Example error 1, hello i am a local error',
    'OTHER': (a, b) => `Hello, data are bad`,
},[
    function(data){
        // Your routine for data validation here
        if (!data) this.throw('E001') // will launch <E001: 'Example error 1, hello i am a local error'> Obs.: error by local dictionary
    },
    function(a, b){
        // Your routine for data validation here
        if (a !== b) this.throw('E002', a, b) // will launch <E002: 'Values is not equals: 1 !== 3'> Obs.: error by dictionary in config.ts
    },
    (data) => {
        // Your routine for data validation here
        if(!data?.name) this.throw('OTHER') // will launch <OTHER: 'Hello, data are bad'>
    }
])

...

app.get("/", (req, res) => {
    const iterator = errors()

    let data = {
        a: 1,
        b: 1,
        name: null
    }
    
    iterator.next(data)

    console.log('hello') // this console.log will running

    data.b = 3

    iterator.next(data.a, data.b) // will launch error 'Values is not equals'

    iterator.next(data)

    res.send('Hello World') // This answer will not be delivered
});