GeezerVison is showing github.com — src: consolidate exit codes in the code base · nodejs/node@be525d7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/async_hooks.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | Symbol, | 9 | 9 | } = primordials; | 10 | 10 |
| | 11 | +const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | | 12 | + | 11 | 13 | const promiseHooks = require('internal/promise_hooks'); | 12 | 14 |
| 13 | 15 | const async_wrap = internalBinding('async_wrap'); | @@ -171,7 +173,7 @@ function fatalError(e) { | 171 | 173 | if (getOptionValue('--abort-on-uncaught-exception')) { | 172 | 174 | process.abort(); | 173 | 175 | } | 174 | | - process.exit(1); | | 176 | + process.exit(kGenericUserError); | 175 | 177 | } | 176 | 178 |
| 177 | 179 | function lookupPublicResource(resource) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/child.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,8 @@ const EventEmitter = require('events'); | 15 | 15 | const { owner_symbol } = require('internal/async_hooks').symbols; | 16 | 16 | const Worker = require('internal/cluster/worker'); | 17 | 17 | const { internal, sendHelper } = require('internal/cluster/utils'); | | 18 | +const { exitCodes: { kNoFailure } } = internalBinding('errors'); | | 19 | + | 18 | 20 | const cluster = new EventEmitter(); | 19 | 21 | const handles = new SafeMap(); | 20 | 22 | const indexes = new SafeMap(); | @@ -43,7 +45,7 @@ cluster._setupWorker = function() { | 43 | 45 | if (!worker.exitedAfterDisconnect) { | 44 | 46 | // Unexpected disconnect, primary exited, or some such nastiness, so | 45 | 47 | // worker exits immediately. | 46 | | - process.exit(0); | | 48 | + process.exit(kNoFailure); | 47 | 49 | } | 48 | 50 | }); | 49 | 51 |
| @@ -278,10 +280,10 @@ Worker.prototype.destroy = function() { | 278 | 280 |
| 279 | 281 | this.exitedAfterDisconnect = true; | 280 | 282 | if (!this.isConnected()) { | 281 | | - process.exit(0); | | 283 | + process.exit(kNoFailure); | 282 | 284 | } else { | 283 | 285 | this.state = 'destroying'; | 284 | 286 | send({ act: 'exitedAfterDisconnect' }, () => process.disconnect()); | 285 | | - process.once('disconnect', () => process.exit(0)); | | 287 | + process.once('disconnect', () => process.exit(kNoFailure)); | 286 | 288 | } | 287 | 289 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/debugger/inspect.js +12-5Lines changed: 12 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,6 +44,12 @@ const { 0: InspectClient, 1: createRepl } = | 44 | 44 | const debuglog = util.debuglog('inspect'); | 45 | 45 |
| 46 | 46 | const { ERR_DEBUGGER_STARTUP_ERROR } = require('internal/errors').codes; | | 47 | +const { | | 48 | + exitCodes: { | | 49 | + kGenericUserError, | | 50 | + kNoFailure, | | 51 | + }, | | 52 | +} = internalBinding('errors'); | 47 | 53 |
| 48 | 54 | async function portIsFree(host, port, timeout = 3000) { | 49 | 55 | if (port === 0) return; // Binding to a random port. | @@ -167,7 +173,7 @@ class NodeInspector { | 167 | 173 |
| 168 | 174 | // Handle all possible exits | 169 | 175 | process.on('exit', () => this.killChild()); | 170 | | - const exitCodeZero = () => process.exit(0); | | 176 | + const exitCodeZero = () => process.exit(kNoFailure); | 171 | 177 | process.once('SIGTERM', exitCodeZero); | 172 | 178 | process.once('SIGHUP', exitCodeZero); | 173 | 179 |
| @@ -234,7 +240,7 @@ class NodeInspector { | 234 | 240 | } | 235 | 241 | } | 236 | 242 | this.stdout.write(' failed to connect, please retry\n'); | 237 | | - process.exit(1); | | 243 | + process.exit(kGenericUserError); | 238 | 244 | } | 239 | 245 |
| 240 | 246 | clearLine() { | @@ -314,7 +320,7 @@ function parseArgv(args) { | 314 | 320 | } catch (e) { | 315 | 321 | if (e.code === 'ESRCH') { | 316 | 322 | console.error(`Target process: ${pid} doesn't exist.`); | 317 | | - process.exit(1); | | 323 | + process.exit(kGenericUserError); | 318 | 324 | } | 319 | 325 | throw e; | 320 | 326 | } | @@ -337,7 +343,8 @@ function startInspect(argv = ArrayPrototypeSlice(process.argv, 2), | 337 | 343 | console.error(` ${invokedAs} <host>:<port>`); | 338 | 344 | console.error(` ${invokedAs} --port=<port>`); | 339 | 345 | console.error(` ${invokedAs} -p <pid>`); | 340 | | - process.exit(1); | | 346 | + // TODO(joyeecheung): should be kInvalidCommandLineArgument. | | 347 | + process.exit(kGenericUserError); | 341 | 348 | } | 342 | 349 |
| 343 | 350 | const options = parseArgv(argv); | @@ -355,7 +362,7 @@ function startInspect(argv = ArrayPrototypeSlice(process.argv, 2), | 355 | 362 | console.error(e.message); | 356 | 363 | } | 357 | 364 | if (inspector.child) inspector.child.kill(); | 358 | | - process.exit(1); | | 365 | + process.exit(kGenericUserError); | 359 | 366 | } | 360 | 367 |
| 361 | 368 | process.on('uncaughtException', handleUnexpectedError); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/repl.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,8 @@ const console = require('internal/console/global'); | 17 | 17 |
| 18 | 18 | const { getOptionValue } = require('internal/options'); | 19 | 19 |
| | 20 | +const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | | 21 | + | 20 | 22 | prepareMainThreadExecution(); | 21 | 23 |
| 22 | 24 | markBootstrapComplete(); | @@ -31,7 +33,8 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) { | 31 | 33 | // If we can't write to stderr, we'd like to make this a noop, | 32 | 34 | // so use console.error. | 33 | 35 | console.error('Cannot specify --input-type for REPL'); | 34 | | - process.exit(1); | | 36 | + // TODO(joyeecheung): should be kInvalidCommandLineArgument. | | 37 | + process.exit(kGenericUserError); | 35 | 38 | } | 36 | 39 |
| 37 | 40 | esmLoader.loadESM(() => { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/test_runner.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | } = require('internal/process/pre_execution'); | 6 | 6 | const { isUsingInspector } = require('internal/util/inspector'); | 7 | 7 | const { run } = require('internal/test_runner/runner'); | | 8 | +const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | 8 | 9 |
| 9 | 10 | prepareMainThreadExecution(false); | 10 | 11 | markBootstrapComplete(); | @@ -22,5 +23,5 @@ if (isUsingInspector()) { | 22 | 23 | const tapStream = run({ concurrency, inspectPort }); | 23 | 24 | tapStream.pipe(process.stdout); | 24 | 25 | tapStream.once('test:fail', () => { | 25 | | - process.exitCode = 1; | | 26 | + process.exitCode = kGenericUserError; | 26 | 27 | }); |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,10 @@ const { | 12 | 12 | prepareMainThreadExecution, | 13 | 13 | markBootstrapComplete | 14 | 14 | } = require('internal/process/pre_execution'); | 15 | | -const { triggerUncaughtException } = internalBinding('errors'); | | 15 | +const { | | 16 | + triggerUncaughtException, | | 17 | + exitCodes: { kNoFailure }, | | 18 | +} = internalBinding('errors'); | 16 | 19 | const { getOptionValue } = require('internal/options'); | 17 | 20 | const { emitExperimentalWarning } = require('internal/util'); | 18 | 21 | const { FilesWatcher } = require('internal/watch_mode/files_watcher'); | @@ -24,7 +27,6 @@ const { setTimeout, clearTimeout } = require('timers'); | 24 | 27 | const { resolve } = require('path'); | 25 | 28 | const { once, on } = require('events'); | 26 | 29 |
| 27 | | - | 28 | 30 | prepareMainThreadExecution(false, false); | 29 | 31 | markBootstrapComplete(); | 30 | 32 |
| @@ -125,7 +127,7 @@ function signalHandler(signal) { | 125 | 127 | return async () => { | 126 | 128 | watcher.clear(); | 127 | 129 | const exitCode = await killAndWait(signal, true); | 128 | | - process.exit(exitCode ?? 0); | | 130 | + process.exit(exitCode ?? kNoFailure); | 129 | 131 | }; | 130 | 132 | } | 131 | 133 | process.on('SIGTERM', signalHandler('SIGTERM')); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/worker_thread.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,7 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => { | 66 | 66 | }); | 67 | 67 |
| 68 | 68 | const assert = require('internal/assert'); | | 69 | +const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | 69 | 70 |
| 70 | 71 | patchProcessObject(); | 71 | 72 | setupInspectorHooks(); | @@ -234,7 +235,7 @@ function workerOnGlobalUncaughtException(error, fromPromise) { | 234 | 235 | if (!process._exiting) { | 235 | 236 | try { | 236 | 237 | process._exiting = true; | 237 | | - process.exitCode = 1; | | 238 | + process.exitCode = kGenericUserError; | 238 | 239 | if (!handlerThrew) { | 239 | 240 | process.emit('exit', process.exitCode); | 240 | 241 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/handle_process_exit.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| | 3 | +const { exitCodes: { kUnfinishedTopLevelAwait } } = internalBinding('errors'); | | 4 | + | 3 | 5 | // Handle a Promise from running code that potentially does Top-Level Await. | 4 | 6 | // In that case, it makes sense to set the exit code to a specific non-zero | 5 | 7 | // value if the main code never finishes running. | 6 | 8 | function handleProcessExit() { | 7 | | - process.exitCode ??= 13; | | 9 | + process.exitCode ??= kUnfinishedTopLevelAwait; | 8 | 10 | } | 9 | 11 |
| 10 | 12 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/policy/manifest.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,8 @@ const { getOptionValue } = require('internal/options'); | 40 | 40 | const shouldAbortOnUncaughtException = getOptionValue( | 41 | 41 | '--abort-on-uncaught-exception' | 42 | 42 | ); | | 43 | +const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | | 44 | + | 43 | 45 | const { abort, exit, _rawDebug } = process; | 44 | 46 | // #endregion | 45 | 47 |
| @@ -72,7 +74,7 @@ function REACTION_EXIT(error) { | 72 | 74 | if (shouldAbortOnUncaughtException) { | 73 | 75 | abort(); | 74 | 76 | } | 75 | | - exit(1); | | 77 | + exit(kGenericUserError); | 76 | 78 | } | 77 | 79 |
| 78 | 80 | function REACTION_LOG(error) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/execution.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ const { | 14 | 14 | ERR_EVAL_ESM_CANNOT_PRINT, | 15 | 15 | }, | 16 | 16 | } = require('internal/errors'); | | 17 | +const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | 17 | 18 |
| 18 | 19 | const { | 19 | 20 | executionAsyncId, | @@ -161,8 +162,8 @@ function createOnGlobalUncaughtException() { | 161 | 162 | try { | 162 | 163 | if (!process._exiting) { | 163 | 164 | process._exiting = true; | 164 | | - process.exitCode = 1; | 165 | | - process.emit('exit', 1); | | 165 | + process.exitCode = kGenericUserError; | | 166 | + process.emit('exit', kGenericUserError); | 166 | 167 | } | 167 | 168 | } catch { | 168 | 169 | // Nothing to be done about it at this point. | |
|