spring-media / aws-lambda-router

Improved routing for AWS Lambda like SNS and ApiGateway
Apache License 2.0
98 stars 52 forks source link

Adding generic errorHandler to proxyIntegration #60

Closed swaner closed 3 years ago

swaner commented 3 years ago

fixes #41 .Adding a generic error handler. The idéa is to have a place for handling error logging and also return custom error messages. Ex:


onError: (error, event, context) => {
    // Global exceptions goes here, works for sns, s3 and sqs should end up here aswell
    console.log(error)
},
proxyIntegration: {
    onError: (error, request, context) => {
        // proxy integration exceptions goes here
        console.log('Error', error, request, context)
    },
    routes: ...
}

// Also support returning a response:
proxyIntegration: {
    onError: async (error) => {
        console.log('Error', error)
        await someAsyncMethod();
        return {
           statusCode: 401,
           body: Not allowed
        }
    },
    routes: ...
}