pig-dam-core
Version:
Library that should be included in every Pig DAM project we build
67 lines (66 loc) • 2.36 kB
JavaScript
;
/**
* Date: 10/29/19
* Time: 9:46 PM
* @license MIT (see project's LICENSE file)
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorToFormatModel = void 0;
const _ = require("lodash");
const error_1 = require("../error");
const stack_1 = require("../stack");
/**
* What are our error formatting concerns?
* - logging? We are going to log the whole error. Formatted messages are a mess in logs
* - console? Yes, we will either log the message or we will log the whole error if in diagnostics mode
* - responses? Nah, we send back the error or the model.
* So, we are going to supply some tools for getting format friendly data. But its up to you to put it together.
*/
/**
* Converts the error into our own representation of an error
*/
function errorToFormatModel(error) {
const cast = error;
return _.omitBy({
// note: we don't want to format details here. We only want to format them if we
// are rendering the error as text
details: (cast.details !== undefined)
? cast.details.trim()
: undefined,
location: errorToLocation(error),
message: error.message,
nested: (cast.error !== undefined)
? errorToFormatModel(cast.error)
: undefined,
stack: stack_1.parseStack(error).lines
}, _.isUndefined);
}
exports.errorToFormatModel = errorToFormatModel;
/********************
* Private Interface
********************/
/**
* We will return as much location information as possible: `module::context.method()`
*/
function errorToLocation(error) {
if (!(error instanceof error_1.PigError)) {
return undefined;
}
else {
// we are going to assume that there is more location info if we got the module. Otherwise this guy
// is going to look a little weird when formatted
let location = (error.module !== undefined)
? `${error.module}::`
: "";
if (error.context !== undefined && error.method !== undefined) {
location = `${location}${error.context}.${error.method}()`;
}
else if (error.context) {
location = `${location}${error.context}`;
}
else if (error.method) {
location = `${location}${error.method}()`;
}
return location;
}
}