@netlify/content-engine
Version:
54 lines • 2 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const stack_trace_1 = __importDefault(require("stack-trace"));
const error_schema_1 = require("./error-schema");
const error_map_1 = require("./error-map");
const errors_1 = require("../reporter/errors");
// Merge partial error details with information from the errorMap
// Validate the constructed object against an error schema
const constructError = ({ details: { id, ...otherDetails } }, suppliedErrorMap) => {
let errorMapEntry = error_map_1.defaultError;
if (id) {
// Look at original errorMap, ids cannot be overwritten
if (error_map_1.errorMap[id]) {
errorMapEntry = error_map_1.errorMap[id];
}
else if (suppliedErrorMap[id]) {
errorMapEntry = {
type: `PLUGIN`,
level: `ERROR`,
...suppliedErrorMap[id],
};
}
}
const type = typeof errorMapEntry.type === `function`
? errorMapEntry.type(otherDetails.context)
: errorMapEntry.type;
// merge
const structuredError = {
context: {},
...otherDetails,
...errorMapEntry,
type,
text: errorMapEntry.text(otherDetails.context),
stack: otherDetails.error
? (0, errors_1.sanitizeStructuredStackTrace)(stack_trace_1.default.parse(otherDetails.error))
: [],
docsUrl: errorMapEntry.docsUrl || `https://gatsby.dev/issue-how-to`,
};
if (id) {
structuredError.code = id;
}
// validate
const { error } = error_schema_1.errorSchema.validate(structuredError);
if (error) {
console.log(`Failed to validate error`, error);
process.exit(1);
}
return structuredError;
};
exports.default = constructError;
//# sourceMappingURL=construct-error.js.map
;