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

laziest-error

v0.3.0

Published

Permette di generare degli errori dal testo personalizzato in modo semplice e diretto, e decidere tramite callback quale tipo di errore restituire e quali modifiche al testo produrre automaticamente

Downloads

4

Readme

LazyError

Permette di generare degli errori dal testo personalizzato in modo semplice e diretto, e decidere tramite callback quale tipo di errore restituire e quali modifiche al testo produrre automaticamente

Problema che vuole risolvere

Normalmente vengono inseriti dei throw new TypeError(message) o throw new Error(message) sparsi all'interno del codice. Ma spesso il messaggio deve essere costruito al momento della chiamata dell'errore. Una possibile evoluzione sarebbe di costruire un oggetto con tutti gli errori che vogliamo usare

const errors = {errore1: "messaggio1, errore2: "messaggio2"}
// e poi vengono richiamati come
throw new Error(errors.errore1)

Resta ancora il problema di modificare il messaggio di errore, ad esempio inserendo il valore che ha causato l'errore

throw new Error(errors.errore1.concat('. ma è stato ricevuto ${valore}'))

LazyError vuole semplificare la generazione e l'uso degli errori, permettendo di associare fin dall'inizio il particolare tipo di errore al messaggio, e anche poter restituire un messaggio personalizzabile e dinamico.

What it works LazyError

new LazyError(tipo_di_errore, [callback])

const errors = new LazyError(TypeError)
errors.errore1 = 'messaggio'
errors.errore2 = 'altro messaggio'

// e viene chiamato in questo modo
new errors.errore1() // -> istanza di TypeError("messaggio")
new errors.errore2(valore) // -> istanza di TypeError("altro messaggio. Ricevuto ${valore}")

Utilizzo di callback per rendere il messaggio dinamico

Qui un esempio e il risulato che si ottiene con questa callback

const callback = (type_error, messaggio, ...args) => {
    let messaggio_da_restituire = 'Attenzione!'.concat(messaggio, ' gli argomenti erano ${...args}')
    let errore = new type_error(messaggio_da_restituire).
    return errore;
}

const errors = new LazyError(TypeError, callback);
errors.errore_argomenti = 'tutti gli argomenti devono essere validi'

// e viene usato come
throw new errors.errore_argomenti(arg1, arg2, arg3)

return the string message

Now is possible to return the string previously setted for a error message. You can use the property message before of error name property.

const message = 'this is an error message that will be customized by callback'
errors.error_name = message
errors.message.error_name === message  // true
errors.error_name('value') // return TypeError with message: `${message}. Received ${value}`

message as instance of error

Now is possible to custom the type of error returned by a specific error setting a new instance of error instead of a string.

const error_type_default_for_each_errors = TypeErro
const errors = new LazyError(error_type_default_for_each_errors)
errors.error1 = 'string'
errors.error2 = new Error('message')

// and for using
errors.error1('value') // return instance of TypeError with message + Received value
errors.error2('value0) // return instnace of Error with message + Received value