graphql-modules
Version:
Create reusable, maintainable, testable and extendable GraphQL modules
45 lines (44 loc) • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ERROR_LOGGER = exports.ERROR_ORIGINAL_ERROR = exports.ERROR_TYPE = void 0;
exports.getType = getType;
exports.getOriginalError = getOriginalError;
exports.getErrorLogger = getErrorLogger;
exports.wrappedError = wrappedError;
exports.stringify = stringify;
exports.ERROR_TYPE = 'diType';
exports.ERROR_ORIGINAL_ERROR = 'diOriginalError';
exports.ERROR_LOGGER = 'diErrorLogger';
function getType(error) {
return error[exports.ERROR_TYPE];
}
function getOriginalError(error) {
return error[exports.ERROR_ORIGINAL_ERROR];
}
function defaultErrorLogger(console, ...values) {
// eslint-disable-next-line no-console
console.error(...values);
}
function getErrorLogger(error) {
return error[exports.ERROR_LOGGER] || defaultErrorLogger;
}
function wrappedError(message, originalError) {
const msg = `${message} caused by: ${originalError instanceof Error ? originalError.message : originalError}`;
const error = Error(msg);
error[exports.ERROR_ORIGINAL_ERROR] = originalError;
return error;
}
function stringify(token) {
if (typeof token === 'string') {
return token;
}
if (token == null) {
return '' + token;
}
if (token.name) {
return `${token.name}`;
}
const res = token.toString();
const newLineIndex = res.indexOf('\n');
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
}