3

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?

David
  • 964
  • 7
  • 14

2 Answers2

6

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...
James Moore
  • 1,881
  • 14
  • 18
  • Hmm.. so I'm aware of the try/catch behaviour, my question was focused around the use of promises and their rejection. As far as I know this won't help, as they're not throwing in the traditional sense, but rather fulfilling one of the promise callbacks. I could be wrong, but as far as I see, the only way to use a try/catch would be to *not* use a promise in this instance. – David Apr 14 '15 at 13:52
  • @David You are right that try/catch does not work with Promises. But try/catch *does* work with generators. The co library uses generators + yield, which allows you to use try/catch to catch rejections from Promises. The secret is in the way that generators work, and the way that yield works. – Pauan Jan 04 '16 at 02:59
0

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