UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

93 lines (92 loc) 3.31 kB
"use strict"; // Results of exeuting a command. // Includes general exit codes, specific known errors, or // room for other errors. // This consolidates success and failure into a single type. Object.defineProperty(exports, "__esModule", { value: true }); exports.failed = exports.succeeded = exports.exception = exports.notLoggedIn = exports.notFound = exports.illegal = exports.failure = exports.success = exports.ErrorCodes = exports.ResultOrValue = exports.isCommandFailedResult = void 0; const constants_1 = require("../misc/constants"); function isCommandFailedResult(object) { return (object != null && typeof object.succeeded === "boolean" && typeof object.errorCode === "number" && typeof object.errorMessage === "string"); } exports.isCommandFailedResult = isCommandFailedResult; class ResultOrValue { constructor(value, result) { this.value = value; this.result = result; } static fromValue(value) { return new ResultOrValue(value, null); } static fromResult(result) { return new ResultOrValue(null, result); } } exports.ResultOrValue = ResultOrValue; var ErrorCodes; (function (ErrorCodes) { ErrorCodes[ErrorCodes["Succeeded"] = 0] = "Succeeded"; // Command given contained illegal characters/names ErrorCodes[ErrorCodes["IllegalCommand"] = 1] = "IllegalCommand"; // Command was legal, but not found ErrorCodes[ErrorCodes["NoSuchCommand"] = 2] = "NoSuchCommand"; // Unhandled exception occurred ErrorCodes[ErrorCodes["Exception"] = 3] = "Exception"; // A parameter is invalid ErrorCodes[ErrorCodes["InvalidParameter"] = 4] = "InvalidParameter"; // Command requires logged in user ErrorCodes[ErrorCodes["NotLoggedIn"] = 5] = "NotLoggedIn"; // The requested resource was not found ErrorCodes[ErrorCodes["NotFound"] = 6] = "NotFound"; })(ErrorCodes = exports.ErrorCodes || (exports.ErrorCodes = {})); // Cache this, we only ever need one const successResult = { succeeded: true, }; // Factory functions for various results function success() { return successResult; } exports.success = success; // Used when there's a failure otherwise function failure(errorCode, errorMessage) { return { succeeded: false, errorCode, errorMessage, }; } exports.failure = failure; function illegal(command) { return failure(ErrorCodes.IllegalCommand, `Command ${command} is invalid`); } exports.illegal = illegal; function notFound(command) { return failure(ErrorCodes.NoSuchCommand, `Command ${command} not found`); } exports.notFound = notFound; function notLoggedIn(command) { return failure(ErrorCodes.NotLoggedIn, `Command '${command}' requires a logged in user. Use the '${constants_1.scriptName} login' command to log in.`); } exports.notLoggedIn = notLoggedIn; function exception(command, ex) { return { succeeded: false, errorCode: ErrorCodes.Exception, errorMessage: `Command '${command}' failed with exception "${ex.message}"`, exception: ex, }; } exports.exception = exception; // Type checks for results function succeeded(result) { return result.succeeded; } exports.succeeded = succeeded; function failed(result) { return !result.succeeded; } exports.failed = failed;