(msg, _code, options)
| 93 | |
| 94 | // Shows error message. Throws if fatal is true (defaults to config.fatal, overridable with options.fatal) |
| 95 | function error(msg, _code, options) { |
| 96 | // Validate input |
| 97 | if (typeof msg !== 'string') throw new Error('msg must be a string'); |
| 98 | |
| 99 | var DEFAULT_OPTIONS = { |
| 100 | continue: false, |
| 101 | code: 1, |
| 102 | prefix: state.currentCmd + ': ', |
| 103 | silent: false, |
| 104 | fatal: config.fatal, |
| 105 | }; |
| 106 | |
| 107 | if (typeof _code === 'number' && isObject(options)) { |
| 108 | options.code = _code; |
| 109 | } else if (isObject(_code)) { // no 'code' |
| 110 | options = _code; |
| 111 | } else if (typeof _code === 'number') { // no 'options' |
| 112 | options = { code: _code }; |
| 113 | } else if (typeof _code !== 'number') { // only 'msg' |
| 114 | options = {}; |
| 115 | } |
| 116 | options = Object.assign({}, DEFAULT_OPTIONS, options); |
| 117 | |
| 118 | if (!state.errorCode) state.errorCode = options.code; |
| 119 | |
| 120 | var logEntry = convertErrorOutput(options.prefix + msg); |
| 121 | state.error = state.error ? state.error + '\n' : ''; |
| 122 | state.error += logEntry; |
| 123 | |
| 124 | // Throw an error, or log the entry |
| 125 | if (options.fatal) { |
| 126 | var err = new Error(logEntry); |
| 127 | err.code = options.code; |
| 128 | throw err; |
| 129 | } |
| 130 | if (msg.length > 0 && !options.silent) log(logEntry); |
| 131 | |
| 132 | if (!options.continue) { |
| 133 | throw new CommandError(new ShellString('', state.error, state.errorCode)); |
| 134 | } |
| 135 | } |
| 136 | exports.error = error; |
| 137 | |
| 138 | //@ |
no test coverage detected
searching dependent graphs…