2012-12-08 2 views
2

error를 throw하면 express는 connect errorHandler 미들웨어를 사용하여 멋지게 렌더링합니다.promise.done()에서 던진 오류를 처리하지 않는 express error middleware

exports.list = function(req, res){ 
    throw new Error('asdf'); 
    res.send("doesn't get here because Error is thrown synchronously"); 
}; 

약속 내에 오류가 발생하면 무시됩니다. 내가 약속에서 오류가 발생하고 호출 할 경우 예외가 미들웨어에 의해 체포되지 않기 때문에

exports.list = function(req, res){ 
    Q = require('q'); 
    Q.fcall(function(){ 
    throw new Error('asdf'); 
    }); 
    res.send("we get here because our exception was thrown async"); 
}; 

그러나, 노드 충돌 "완료". 이 출력 위, 노드 충돌을 실행 한 후

exports.list = function(req, res){ 
    Q = require('q'); 
    Q.fcall(function(){ 
    throw new Error('asdf'); 
    }).done(); 
    res.send("This prints. done() must not be throwing."); 
}; 

:

node.js:201 
     throw e; // process.nextTick error, or 'error' event on first tick 
      ^
Error: asdf 
    at /path/to/demo/routes/user.js:9:11 

그래서 내 결론은() 완료가 예외를 throw되지 않는 것입니다,하지만 예외가 다른 슬로우됩니다. 그게 맞습니까? 약속의 오류가 미들웨어에 의해 처리 될 수있는 방법을 시도하고 있습니까?

FYI :이 해킹은 최상위 수준에서 예외를 잡아낼 수 있지만 미들웨어 영역 밖에 있으므로 내 요구 사항을 충족하지 못합니다 (오류를 멋지게 렌더링하기 위해).

//in app.js #configure 
process.on('uncaughtException', function(error) { 
    console.log('uncaught expection: ' + error); 
}) 

답변

2

아마도 비동기 오류 처리에 유용한 connect-domain 미들웨어가 있습니다. 이 미들웨어를 사용하면 일반적인 오류와 같은 비동기 오류를 처리 할 수 ​​있습니다.

var 
    connect = require('connect'), 
    connectDomain = require('connect-domain'); 

var app = connect() 
    .use(connectDomain()) 
    .use(function(req, res){ 
     process.nextTick(function() { 
      // This async error will be handled by connect-domain middleware 
      throw new Error('Async error'); 
      res.end('Hello world!'); 
     }); 
    }) 
    .use(function(err, req, res, next) { 
     res.end(err.message); 
    }); 

app.listen(3131); 
+0

나를 위해 어려운 부분은 nodejs의 이벤트 루프가 내가 당연한 것으로서 받아 들여지고있는 것과 호환되지 않는다는 것을 이해하고 있다고 생각합니다. 도메인 모듈은이 문제를 해결하기 위해 도입 된 것으로 보인다. 감사! http://nodejs.org/api/domain.html – mkirk

관련 문제