iopipe / serverless-iopipe-layers

Please migrate to the equivalent New Relic Serverless plugin: serverless-newrelic-lambda-layers
https://github.com/iopipe/serverless-newrelic-lambda-layers
Apache License 2.0
14 stars 8 forks source link

Detect API Gateway errors #37

Closed jeffggardner closed 5 years ago

jeffggardner commented 5 years ago

We use the Lambda proxy integration in AWS API Gateway and it seems that iopipe can't detect errors within our functions.

Due to the way that integration works our lambdas don't use things like context.fail() nor can we throw the error. Instead, the integration uses the JSON body to determine the response to the client; e.g:

exports.handler = iopipe(async (event, context) => {
try {
   <code here throws exception>
  } catch (err) {
    logger.error('------ There was an error');
    logger.error(err);

    response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
    response.body = 'There was an error';

    return response;  //
  } 
}

iopipe does not report this an error

mrickard commented 5 years ago

Hi @jeffggardner !

If a function is handling an error, IOpipe doesn't consider that to be an error as such. If, on the other hand, a request generated an unhandled error state, that would be flagged as an error in your dashboard.

What I’d suggest is that if you want to log handled errors, you could do that with a custom label in your error handler function.

jeffggardner commented 5 years ago

Hi @mrickard I understand. However if you are using the API Gateway "Lambda Proxy Integration" the response from the lambda must be in a very specific format.

See here: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format

So throwing the error will break the proxy integration and the API Gateway will return an HTTP 200 in those cases.

And looking further, I think that you would have to modify this file: https://github.com/iopipe/iopipe-js-core/blob/master/src/index.js#L232 in a way that inspects the statusCode of the response where that value >= 400 and report that as an error

jeffggardner commented 5 years ago

@mrickard I should probably open this issue on the iopipe-js-core repo

jeffggardner commented 5 years ago

closing in favor of: https://github.com/iopipe/iopipe-js-core/issues/347