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

Материал из support.qbpro.ru
imported>Supportadmin
(Новая страница: «[http://stackoverflow.com/questions/7310521/node-js-best-practice-exception-handling источник] ==Safely "throwing" errors== deally we'd like to avoid unc…»)
 
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, if an error happens, return the error:
'''For synchronous code, if an error happens, return the error:'''


// Define divider as a syncrhonous function
<nowiki>// Define divider as a syncrhonous function
var divideSync = function(x,y) {
var divideSync = function(x,y) {
     // if error condition?
     // if error condition?
Строка 42: Строка 42:
     // no error occured, continue on
     // no error occured, continue on
     console.log('4/0='+result);
     console.log('4/0='+result);
}
}</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:
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:



Версия от 11:07, 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, 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, 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, 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);