Nodejs обработка ошибок: различия между версиями

Материал из support.qbpro.ru
imported>Supportadmin
imported>Supportadmin
Нет описания правки
Строка 4: Строка 4:
deally we'd like to avoid uncaught errors as much as possible, as such, instead of literally throwing the error, we can instead safely "throw" the error using one of the following methods depending on our code architecture:
deally we'd like to avoid uncaught errors as much as possible, as such, instead of literally throwing the error, we can instead safely "throw" the error using one of the following methods depending on our code architecture:


===For synchronous code===
'''For synchronous code, if an error happens, return the error:'''
'''For synchronous code, if an error happens, return the error:'''


Строка 43: Строка 44:
     console.log('4/0='+result);
     console.log('4/0='+result);
}</nowiki>
}</nowiki>
For callback-based (ie. asynchronous) code, the first argument of the callback is err, if an error happens err is the error, if an error doesn't happen then err is null. Any other arguments follow the err argument:


var divide = function(x,y,next) {
===For callback-based (ie. asynchronous) code===
'''For callback-based (ie. asynchronous) code''', the first argument of the callback is err, if an error happens err is the error, if an error doesn't happen then err is null. Any other arguments follow the err argument:
 
<nowiki>var divide = function(x,y,next) {
     // if error condition?
     // if error condition?
     if ( y === 0 ) {
     if ( y === 0 ) {
Строка 80: Строка 83:
         console.log('4/0='+result);
         console.log('4/0='+result);
     }
     }
});
});</nowiki>
===For eventful code===
For eventful code, where the error may happen anywhere, instead of throwing the error, fire the error event instead:
For eventful code, where the error may happen anywhere, instead of throwing the error, fire the error event instead:


// Definite our Divider Event Emitter
<nowiki>// Definite our Divider Event Emitter
var events = require('events');
var events = require('events');
var Divider = function(){
var Divider = function(){
Строка 117: Строка 121:


// Divide
// Divide
divider.divide(4,2).divide(4,0);
divider.divide(4,2).divide(4,0);</nowiki>

Версия от 11:37, 10 апреля 2014

источник

Safely "throwing" errors

deally we'd like to avoid uncaught errors as much as possible, as such, instead of literally throwing the error, we can instead safely "throw" the error using one of the following methods depending on our code architecture:

For synchronous code

For synchronous code, if an error happens, return the error:

// Define divider as a syncrhonous function
var divideSync = function(x,y) {
    // if error condition?
    if ( y === 0 ) {
        // "throw" the error safely by returning it
        return new Error("Can't divide by zero");
    }
    else {
        // no error occured, continue on
        return x/y;
    }
};

// Divide 4/2
var result;
result = divideSync(4,2);
// did an error occur?
if ( result instanceof Error ) {
    // handle the error safely
    console.log('4/2=err', result);
}
else {
    // no error occured, continue on
    console.log('4/2='+result);
}

// Divide 4/0
result = divideSync(4,0);
// did an error occur?
if ( result instanceof Error ) {
    // handle the error safely
    console.log('4/0=err', result);
}
else {
    // no error occured, continue on
    console.log('4/0='+result);
}

For callback-based (ie. asynchronous) code

For callback-based (ie. asynchronous) code, the first argument of the callback is err, if an error happens err is the error, if an error doesn't happen then err is null. Any other arguments follow the err argument:

var divide = function(x,y,next) {
    // if error condition?
    if ( y === 0 ) {
        // "throw" the error safely by calling the completion callback
        // with the first argument being the error
        next(new Error("Can't divide by zero"));
    }
    else {
        // no error occured, continue on
        next(null, x/y);
    }
};

divide(4,2,function(err,result){
    // did an error occur?
    if ( err ) {
        // handle the error safely
        console.log('4/2=err', err);
    }
    else {
        // no error occured, continue on
        console.log('4/2='+result);
    }
});

divide(4,0,function(err,result){
    // did an error occur?
    if ( err ) {
        // handle the error safely
        console.log('4/0=err', err);
    }
    else {
        // no error occured, continue on
        console.log('4/0='+result);
    }
});

For eventful code

For eventful code, where the error may happen anywhere, instead of throwing the error, fire the error event instead:

// Definite our Divider Event Emitter var events = require('events'); var Divider = function(){ events.EventEmitter.call(this); }; require('util').inherits(Divider, events.EventEmitter); // Add the divide function Divider.prototype.divide = function(x,y){ // if error condition? if ( y === 0 ) { // "throw" the error safely by emitting it var err = new Error("Can't divide by zero"); this.emit('error', err); } else { // no error occured, continue on this.emit('divided', x, y, x/y); } // Chain return this; }; // Create our divider and listen for errors var divider = new Divider(); divider.on('error', function(err){ // handle the error safely console.log(err); }); divider.on('divided', function(x,y,result){ console.log(x+'/'+y+'='+result); }); // Divide divider.divide(4,2).divide(4,0);