@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
41 lines (37 loc) • 1.49 kB
JavaScript
const cds = require('../../cds')
function getMessage(errorText, { error, command, help = command && getCommandHelp(command) } = {}) {
const DEBUG = cds.log('cli')._debug
let message = errorText ? errorText.trim() : '';
if (error) {
const includeStack = DEBUG && error.stack?.includes('\n');
if (message.length) {
message = message.replace(/(?<!\p{P})$/u, ':'); // Append colon if no punctuation at the end
message += includeStack ? '\n' : ' ';
}
const oriMsg = error.response?.data?.message // case: response body == error
|| error.response?.data?.error?.message // case: response body == { error }
|| (includeStack ? '' : error.message);
message += oriMsg;
if (includeStack) {
message += (oriMsg ? '\n' : '') + error.stack;
}
if (error.cause) {
if (DEBUG) {
message += '\nCaused by: ' + getMessage('', { error: error.cause }).replace(/^(?= *at)/gm, ' ');
} else if (error.cause.status) {
message += `\nHTTP status: ${error.cause.status}`;
}
}
}
if (typeof help === 'string') {
message += '\n' + require('../../../bin/help').formatHelp(help);
}
return message;
}
function getCommandHelp(command) {
const cli = require('../../../bin/cds');
return cli?.load(command, () => ({ help: '' }))?.help ?? '';
}
module.exports = {
getMessage
};