0.2.0 • Published 8 years ago

snerr v0.2.0

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

Snerr

An error-message and error-code manager for developers and teams

Create your own Error Code Standard and share easily with your team! Snerr helps manage error-code creation, simplifies code, and helps decode error codes into meaningful messages in a format matching your requirement.

Usage

Initialize

const sn = require('snerr')

In your code, say you need:

if(err){
    const error = {
        code : "SN201NF",
        raw : err,
        message : "Database error in User : Not Found"
    }
}

Using snerr accomplish this in one line,

if(err)
    const error = sn.err('S201NF', err, 1)

Error codes are generated as a JSON object, where,

{
    "code": {
        "SN" : {
            "is" : "prefix",
            "1" : {...},
            "2" : {
                "is" : "Database",
                "01" : {
                    "is" : "User",
                    "NF" : {
                        "is" : "Not Found"
                    },
                    "UP" : {
                        "is" : "Update"
                    }
                },
                "02" : {
                    "is" : "Room",
                    "NF" : {...}
                }
            },
            "3": {...}
        }       
    }
}

Dev feeds in the config as an array representing the nested levels of the code

{
    "config" : [{
        "pre" : "",
        "post" : "",
        "length" : 1,
        "skip" : true
    },{
      "pre": "",
      "post": " error ",
      "length": 1,
      "skip": false
    },{
      "pre": " in ",
      "post": "",
      "length": 2,
      "skip": false
    },{
      "pre": " : ",
      "post": "",
      "length": 2,
      "skip": false
    }]
}

Every time an sn.err() function is called, the code object is checked and/or a new node created if the code is missing. The dev fills in the values according to his need after one run of the server covering all sn.err calls.

{
    "code" : {
        "SN" :{
            "is": ""
        }
    }
}

The message need not be passed either; as long as the collaborator/other developer has a copy of snerrfile.json, they can decode the snerr code. Infact, the decoding can be done with a different configuration, formatted according to new requirements.

ex. the front end guy can decode SN401NF as User Not Found by simply tweaking the config.

Create a snerrfile.json file in the root directory of your application. Please follow the format for the config strictly; you can alternatively choose to copy the file from node_modules/snerr/snerrfile.json, or snerr will do it for you.

Docs

snerr exposes two methods:

  • err(snerr,raw, addMessage) where,

    VariableData TypeAbout
    errobjecterror object containing code, err, (and message)
    snerrstringerror code in snerr configuration
    rawobjectraw system error data
    addMessagebooleanadd decoded message? (false by default)
  • show(snerr) where,

    VariableData TypeAbout
    showstringdecoded message string
    snerrstringsnerr code

License

ISC

0.2.0

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago