Error handling with promises in Koa

試著忘記壹切 提交于 2019-12-10 10:18:30

问题


If I yield promises in Koa, they may be rejected:

function fetch = (){
  var deferred = q.defer(); 
  //Some async action which calls deferred.reject();
  return deferred.promise;
}

this.body = yield fetch(); //bad, not going to work

Is there a error-handling pattern in Koa to handle this besides explicitly unwrapping the promise with then and handling the error explicitly?


回答1:


Try/Catch. Under the hood koajs uses co. Check out the docs for co, which describes error handling a little better.

function fetch = (){
  var deferred = q.defer(); 
  //Some async action which calls deferred.reject();
  return deferred.promise;
}
try{
    this.body = yield fetch(); //bad, not going to work
}
catch(err){
    this.throw('something exploded');
}

Here is an rudimentary example of what's happening with the promise:

function * someGenerator () {
  let result;
  try{
    result = yield fetch();
    console.log('won\'t make it here...');
  }
  catch(err){
    console.log(err);
  }
  console.log('will make it here...');
}
// Normally co() does everything below this line, I'm including it for
// better understanding

// instantiate the generator
let gen = someGenerator();
// calling gen.next() starts execution of code up until the first yield
// or the function returns.  
let result = gen.next();
// the value returned by next() is an object with 2 attributes
// value is what is returned by the yielded item, a promise in your example
// and done which is a boolean that indicates if the generator was run 
// to completion
// ie result = {value: promise, done: false}

// now we can just call the then function on the returned promise
result.value.then(function(result){
  // calling .next(result) restarts the generator, returning the result
  // to the left of the yield keyword
  gen.next(result);
}, function(err){
  // however if there happened to be an error, calling gen.throw(err)
  // restarts the generator and throws an error that can be caught by 
  // a try / catch block.  
  // This isn't really the intention of generators, just a clever hack
  // that allows you to code in a synchronous style (yet still async code)
  gen.throw(err);
});

If your still uncertain, here are a couple other things that might help:

Watch my screencast on JavaScript Generators here: http://knowthen.com/episode-2-understanding-javascript-generators/

and/or try the following code:

// test.js
'use strict';
let co      = require('co'),
    Promise = require('bluebird');

function fetch () {
  let deffered = Promise.defer();
  deffered.reject(new Error('Some Error'));
  return deffered.promise;
}

co.wrap(function *(){
  let result;
  try{
    result = yield fetch();
    console.log('won\'t make it here...');
  }
  catch(err){
    console.log(err);
  }
  console.log('will make it here...');

})();

then on the console run

$ node test.js
[Error: Some Error]
will make it here...



回答2:


You can easily drop in the package koa-better-error-handler with npm install --save koa-better-error-handler and then implement it as such:

const errorHandler = require('koa-better-error-handler');

// override koa's undocumented error handler
app.context.onerror = errorHandler;

More info: https://github.com/niftylettuce/koa-better-error-handler



来源:https://stackoverflow.com/questions/29617117/error-handling-with-promises-in-koa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!