Last active
July 6, 2020 21:14
-
-
Save 5t33/2cebca2de04f83236c89160f1a4bd271 to your computer and use it in GitHub Desktop.
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.body = body; | |
} | |
this.statusCode = statusCode; | |
} | |
} | |
module.exports.ApiError = ApiError | |
const formatApiError = (error) => { | |
return ({ | |
isBase64Encoded: false, | |
statusCode: error.statusCode, | |
headers: { | |
"Content-Type": typeof body === "string" ? "text/html" : "application/json", | |
}, | |
body: typeof error.body === "string" ? error.body : JSON.stringify(error.body) | |
}); | |
} | |
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.resolve(response); | |
} | |
}; | |
module.exports.handleResp = ( | |
body | |
) => ({ | |
isBase64Encoded: false, | |
statusCode: 200, | |
headers: { | |
"Content-Type": typeof body === "string" ? "text/html" : "application/json", | |
}, | |
body: JSON.stringify(body) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment