This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service: lambda-api-demo | |
plugins: | |
- serverless-offline | |
provider: | |
name: aws | |
runtime: nodejs12.x | |
stage: ${opt:stage} | |
region: us-east-2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Logger = require('./logger'); | |
const { handleError, handleResp, ApiError } = require('./utils'); | |
const { | |
functionThatRejects, | |
functionThatResolves, | |
functionThatRejectsWith400 | |
} = require('./anotherFile'); | |
const logger = Logger("info"); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pino = require("pino"); | |
module.exports = (level) => { | |
return pino({ | |
level, | |
name: process.env.FUNC_NAME, | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApiError extends Error { | |
statusCode; | |
body; | |
constructor(statusCode, body) { | |
super(); | |
if(Array.isArray(body)) { | |
this.body = { | |
Errors: body | |
} | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ApiError } = require('./utils'); | |
module.exports.functionThatRejectsWith400 = () => Promise.reject( | |
new ApiError(400, [ | |
"wow you messed up, huh?", | |
"Looks like somebody didn't read the documentation." | |
]) | |
) | |
module.exports.functionThatRejects = () => Promise.reject(new Error("Oh No")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.handleError = (logger, error ) => { | |
if (error instanceof ApiError) { | |
const response = formatApiError(error); | |
return Promise.resolve(response) | |
} else { | |
logger.error("Internal Server Error: ", error); | |
const response = formatApiError(new ApiError(500, "Internal Server Error")); | |
return Promise.reject(error); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functions: | |
DemoLambda: | |
role: DemoLambdaRole | |
handler: index.handler | |
integration: lambda | |
events: | |
- http: | |
path: /health_check | |
method: get | |
integration: lambda |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Logger = require('./logger'); | |
const { handleError, handleResp, ApiError } = require('./utils'); | |
const { functionThatRejects, functionThatResolves, functionThatRejectsWith400 } = require('./anotherFile'); | |
const logger = Logger("trace"); | |
module.exports.handler = async ( event, context) => { | |
const log = logger.child({ | |
requestId: context.awsRequestId, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatApiError = (error) => JSON.stringify({ | |
isBase64Encoded: false, | |
statusCode: error.statusCode, | |
headers: { | |
"Content-Type": typeof error.body === "string" ? "text/html" : "application/json", | |
}, | |
body: error.body | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.handleError = (logger, error ) => { | |
if (error instanceof ApiError) { | |
const response = formatApiError(error); | |
return Promise.reject(response) | |
} else { | |
logger.error("Internal Server Error: ", error); | |
const response = formatApiError(new ApiError(500, "Internal Server Error")); | |
return Promise.reject(response); | |
} | |
}; |
OlderNewer