0.0.11 • Published 11 years ago

error-agent v0.0.11

Weekly downloads
-
License
-
Repository
github
Last release
11 years ago

about Error-agent

error agent is a module help you deal with the errors, and respond them to http client

install

npm install error-agent

how to use

when dealing with errors, many people simply use string, instead of Error object, which is not appropiate

you need to use Error object, attached with some properties, so you know how to deal with it later

so, when you're in async code

var errorAgent= require('error-agent');

DB.queryUser(function(err, userData){
	if(!userData.auth) //if this user is not authenticated to read this page
		callback( errorAgent.handle({ status: 403, msg: 'you need to sign in to see' }) )  //callback an error
})

or simply throw

if(!userData.auth)
	throw errorAgent.handle({ status: 403, msg: 'you need to sign in to see' });

as you might guess, errorAgent.handle return an Error object with the properties you pass in

send error message to client

you should call it at your last callback

and pass the error object to use, pass req, res to send

it will deal with it for you

errorAgent.send(err, req, res);

using code property

you can use code property, so you can register a function to deal with certain error code

if(user.hasNoName)
	throw errorAgent.handle({code: 'USER_NONAME'});

register a code error handler first

ErrorAgent.register('USER_NONAME', function (req, res) {
	res.send('you got no name!');
});

use error file instead of register

just give a file path, that will exports error code handlers

would be better

ErrorAgent.config({ codesPath: __dirname + '/errors.js' })

express example

see here

todo

  • error page folder path
0.0.11

11 years ago

0.0.10

11 years ago

0.0.9

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago