UNPKG

@ethersphere/swarm-cli

Version:
88 lines (87 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.errorHandler = exports.CommandLineError = void 0; const cafe_utility_1 = require("cafe-utility"); const _1 = require("."); const printer_1 = require("../command/root-command/printer"); const printer_2 = require("../printer"); /** * Thrown when the error is not related to Bee/network */ class CommandLineError extends Error { constructor() { super(...arguments); Object.defineProperty(this, "type", { enumerable: true, configurable: true, writable: true, value: 'CommandLineError' }); } } exports.CommandLineError = CommandLineError; function hasStatusCode(error, statusCode) { return error?.response?.status === statusCode; } function isNotFoundError(error) { const message = cafe_utility_1.Objects.getDeep(error, 'message'); if (typeof message === 'string' && message.includes('status code 404')) { return true; } return hasStatusCode(error, 404); } function isInternalServerError(error) { return hasStatusCode(error, 500); } function errorHandler(error, options) { if (!process.exitCode) { process.exitCode = 1; } // grab error.message, or error if it is a string const message = typeof error === 'string' ? error : error?.response?.data?.message || error?.message; const type = (0, _1.getFieldOrNull)(error, 'type'); // write custom message for 500 if (isInternalServerError(error)) { printer_2.printer.printError(printer_1.FORMATTED_ERROR + ' Bee responded with HTTP 500 (Internal Server Error).'); if (!isGenericErrorPattern('Internal Server Error', message)) { printer_2.printer.printError(''); printer_2.printer.printError('The error message is: ' + message); } // write custom message for 404 } else if (isNotFoundError(error)) { printer_2.printer.printError(printer_1.FORMATTED_ERROR + ' Bee responded with HTTP 404 (Not Found).'); if (options?.notFoundMessage || !isGenericErrorPattern('Not Found', message)) { printer_2.printer.printError(''); printer_2.printer.printError('The error message is: ' + (options?.notFoundMessage || message)); } // print 'command failed' message with error message if available } else if (message) { printer_2.printer.printError(printer_1.FORMATTED_ERROR + ' ' + message); } else { printer_2.printer.printError(printer_1.FORMATTED_ERROR + ' The command failed, but there is no error message available.'); } // print 'check bee logs' message if error does not originate from the cli if (type !== 'CommandLineError') { if (message) { printer_2.printer.printError(''); printer_2.printer.printError('There may be additional information in the Bee logs.'); } else { printer_2.printer.printError(''); printer_2.printer.printError('Check your Bee log to learn if your request reached the node.'); } } } exports.errorHandler = errorHandler; function isGenericErrorPattern(errorName, message) { if (!message || typeof message !== 'string') { return true; } errorName = errorName.toLowerCase(); message = message.toLowerCase(); // also handles Internal Server Error: Internal Server Error message pattern return message === errorName || message === `${errorName}: ${errorName}`; }