gcf-helper
Version:
Google Cloud Functions Helper
48 lines (47 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const uuid = require("uuid");
const NO_FUNCTION_ID_PROVIDED = "NOFUNCIDPROVIDED";
const NO_ERROR_PROVIDED = "No error provided.";
const NO_ERROR_MESSAGE = "No error message provided.";
const NO_ERROR_STACK = "No error stack provided.";
const RECURSIVE_FLAG = "gcfhelper";
class ErrorHandler {
constructor(gcfHelper) {
this.gcfHelper = gcfHelper;
}
generateErrorPayload(error, eventPayload) {
if (!error || typeof error !== "object") {
error = {
name: "Missing",
message: NO_ERROR_PROVIDED,
};
}
return {
error_id: uuid.v4(),
function_id: this.gcfHelper.functionOptions.functionIdentifier || NO_FUNCTION_ID_PROVIDED,
error_message: JSON.stringify({
name: error.name,
message: error.message || NO_ERROR_MESSAGE,
stack: error.stack || NO_ERROR_STACK,
}),
payload: typeof eventPayload !== "string" ?
JSON.stringify(eventPayload) :
eventPayload,
error_occured_at: Date.now(),
};
}
generateErrorFromErrorPayload(errorPayload) {
const error = new Error(`${this.gcfHelper.functionOptions.errorCode} error occured`
+ `, check error stream for details: ${errorPayload.error_id}.`);
error.gcfFlag = RECURSIVE_FLAG;
return error;
}
isGCFHelperError(error) {
if (error.gcfFlag === RECURSIVE_FLAG) {
return true;
}
return false;
}
}
exports.default = ErrorHandler;