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

damn-it

v0.3.1

Published

A very simple library which keeps track of errors that occur during runtime

Downloads

34

Readme

A very simple library which keeps track of errors that occur during runtime

NPM
Build Status Dependecies Status Coverage Status

Install

Just like all node modules, you can run the following command to install it.

npm install damn-it

You can install it globally with -g switch. However I recommand you to install it for each of your node applications locally.

Getting Started

The very first step of using damn-it is to initialize it.

// instantiate and initialize it in one line
var damnIt = require("damn-it").initialize();

// or you can instantiate it first, then initialize it
var damnIt = require("damn-it");
damnIt.initialize();

I try to keep damn-it as simple at possible. I strongly think that you shouldn't be worry about your application errors. Although errors are very important, however I believe that there are other critical issuse to think of.

sream() or silent() There is a very simple difference between these methods. Both of them save errors to the given backend. But the first one also log error in nodejs console, whereas, the later one just save error. Their usege are exactly the same.

try-catch You can use damn-it to handle your errors manually. If you have a try-catch statement, you can save its catch error with damn-it.

try {
    throw new Error("just a fake error!");
} catch(err) {
    damnIt.scream(err);
}

express middleware If your node application is based on express, you can handle your errors too.

app.use(function( err, req, res, next ) {
  damnIt.scream(err);
  next(err);
});

pure nodejs application As you know the main purpose of damn-it is to save your application errors somewhere that you can see and review them easily. There is a method for error handling in nodejs which track all errors. Although it is not recommanded to use it, however for our purpose it is good.

process.on('uncaughtException', function( err ) {
  damnIt.scream(err);
});

You can use it in express based applications too.

Options

initialize method accept a wide range of options. There is also two helper methods namely, set and get that you can use to interact with damn-it options.

backend (default="file") It handle where errors should be save.

  • backend="mysql" : save errors in a mysql table
  • backend="file" : save errors in a given file name

path (default="./errors.txt") Given file path to save errors.

type (default="json") It handle the way errors are being save.

  • type="json" : save errors in JSON format
  • type="text" : save errors in plain text

You can set or change damn-it options in two different ways. In intialize method:

var damnIt = require("damnIt").initialize({
   backend: "mysql",
   file: "logs.txt",
   type: "text"
});

Or with set method:

var damnIt = require("damnIt").initialize();
damnIt.set("backend", "mysql");
damnIt.set("file", "logs.txt");
damnIt.set("type", "text");

Backends

damn-it supports different type of backends. File and Database. You can switch between them anywhere in your application with set method.

mysql backend In order to use mysql backend, you should provide an open connection to your database. Moreover, you need a mysql driver to create a connection to mysql database. I suggest node-mysql module which is powerful and easy to use.

I assume that you are using 'node-mysql'. First you should include it and create a connection.

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : '<host>',
  database : '<database_name>',
  user     : '<database_username>',
  password : '<database_password>'
});

Then you need to pass connection to damn-it module. You have to options as before. In the first place you can initialize damn-it with an option argument which contains your database connection.

var damnIt = require("damn-it").initialize({
    backend: "mysql",
    db: connection
});

Pay attention to db property. Its name should be db.

Then every time you call scream() or silent() method, all errors will be saved into a table called errors in your database.

file backend There is nothing special about file backend. However you should know that if you choose it, all errors will be saved into a file called whatever you give as file property.

Test

You can test this module with npm test.