apigee-127 / swagger-node-runner

The heart of Swagger-Node
MIT License
102 stars 123 forks source link

Added callback to controllerFunction catch if it is Promise/async func #105

Closed serratedserenade closed 5 years ago

serratedserenade commented 7 years ago

Tested: node v8.1.2

Thought it would be appropriate that if the evoked controllerFunction is an async function, have it handle the callback when an error is thrown by having the callback within its catch method instead of giving an unhandled promise rejection error.

Example:

async function hello(req, res) {
  throw Error("Hello");
}

// Results in this error, and the request never resolves
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Hello

Fixed:

async function hello(req, res) {
  throw Error("Hello");
}

// Error stack properly displayed in stderr, request continues on to whatever error handler
coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.01%) to 95.761% when pulling 821e8fe2f195a6a7a0b823261318d1339ef3f299 on serratedserenade:master into a944eb97059da3f8fc26c614cbc39f8a9633b40f on theganyo:master.

jwalton commented 6 years ago

A minor suggestion, but I would instead do:

if (resolvedFunction.then) {
    resolvedFunction.then(
        function() {},
        cb
    );
}

This way this will work for bluebird promises or other "thenables", even if they don't inherit from Promise.

Possibly, you might also want to do:

if (resolvedFunction.then) {
    resolvedFunction.then(
        function() {cb()},
        cb
    );
}

That way the callback gets called either way, even if there's no error.