diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index cd438d415d55b2..c2175e97b3ed59 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -185,6 +185,7 @@ rules: - groups: [['&&', '||']] # Custom rules in tools/eslint-rules + node-core/alphabetize-errors: error node-core/alphabetize-primordials: error node-core/avoid-prototype-pollution: error node-core/lowercase-name-for-primitive: error diff --git a/lib/_http_client.js b/lib/_http_client.js index f9fd3700c3d19d..09ccd2e4dbf61c 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -72,14 +72,16 @@ const { traceEnd, getNextTraceEventId, } = require('internal/http'); -const { ConnResetException, codes } = require('internal/errors'); const { - ERR_HTTP_HEADERS_SENT, - ERR_INVALID_ARG_TYPE, - ERR_INVALID_HTTP_TOKEN, - ERR_INVALID_PROTOCOL, - ERR_UNESCAPED_CHARACTERS, -} = codes; + ConnResetException, + codes: { + ERR_HTTP_HEADERS_SENT, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_HTTP_TOKEN, + ERR_INVALID_PROTOCOL, + ERR_UNESCAPED_CHARACTERS, + }, +} = require('internal/errors'); const { validateInteger, validateBoolean, diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 5e9df1c42b26ee..0b7ed15622910d 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -56,21 +56,21 @@ const { } = require('internal/async_hooks'); const { codes: { + ERR_HTTP_BODY_NOT_ALLOWED, ERR_HTTP_CONTENT_LENGTH_MISMATCH, ERR_HTTP_HEADERS_SENT, ERR_HTTP_INVALID_HEADER_VALUE, ERR_HTTP_TRAILER_INVALID, - ERR_HTTP_BODY_NOT_ALLOWED, - ERR_INVALID_HTTP_TOKEN, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_INVALID_CHAR, + ERR_INVALID_HTTP_TOKEN, ERR_METHOD_NOT_IMPLEMENTED, - ERR_STREAM_CANNOT_PIPE, ERR_STREAM_ALREADY_FINISHED, - ERR_STREAM_WRITE_AFTER_END, - ERR_STREAM_NULL_VALUES, + ERR_STREAM_CANNOT_PIPE, ERR_STREAM_DESTROYED, + ERR_STREAM_NULL_VALUES, + ERR_STREAM_WRITE_AFTER_END, }, hideStackFrames, } = require('internal/errors'); diff --git a/lib/_http_server.js b/lib/_http_server.js index d67669fea9433b..420c3c338e2689 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -70,17 +70,17 @@ const { const { IncomingMessage } = require('_http_incoming'); const { ConnResetException, - codes, + codes: { + ERR_HTTP_HEADERS_SENT, + ERR_HTTP_INVALID_STATUS_CODE, + ERR_HTTP_REQUEST_TIMEOUT, + ERR_HTTP_SOCKET_ASSIGNED, + ERR_HTTP_SOCKET_ENCODING, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_CHAR, + ERR_OUT_OF_RANGE, + }, } = require('internal/errors'); -const { - ERR_HTTP_REQUEST_TIMEOUT, - ERR_HTTP_HEADERS_SENT, - ERR_HTTP_INVALID_STATUS_CODE, - ERR_HTTP_SOCKET_ENCODING, - ERR_HTTP_SOCKET_ASSIGNED, - ERR_INVALID_ARG_VALUE, - ERR_INVALID_CHAR, -} = codes; const { kEmptyObject, promisify, @@ -467,7 +467,7 @@ function storeHTTPOptions(options) { } if (this.requestTimeout > 0 && this.headersTimeout > 0 && this.headersTimeout > this.requestTimeout) { - throw new codes.ERR_OUT_OF_RANGE('headersTimeout', '<= requestTimeout', headersTimeout); + throw new ERR_OUT_OF_RANGE('headersTimeout', '<= requestTimeout', headersTimeout); } const keepAliveTimeout = options.keepAliveTimeout; diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 95f61cb9f6c289..cc79b30aacac7d 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -66,23 +66,25 @@ const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap'); const { owner_symbol } = require('internal/async_hooks').symbols; const { isArrayBufferView } = require('internal/util/types'); const { SecureContext: NativeSecureContext } = internalBinding('crypto'); -const { ConnResetException, codes } = require('internal/errors'); const { - ERR_INVALID_ARG_TYPE, - ERR_INVALID_ARG_VALUE, - ERR_MULTIPLE_CALLBACK, - ERR_SOCKET_CLOSED, - ERR_TLS_ALPN_CALLBACK_INVALID_RESULT, - ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS, - ERR_TLS_DH_PARAM_SIZE, - ERR_TLS_HANDSHAKE_TIMEOUT, - ERR_TLS_INVALID_CONTEXT, - ERR_TLS_RENEGOTIATION_DISABLED, - ERR_TLS_REQUIRED_SERVER_NAME, - ERR_TLS_SESSION_ATTACK, - ERR_TLS_SNI_FROM_SERVER, - ERR_TLS_INVALID_STATE, -} = codes; + ConnResetException, + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_MULTIPLE_CALLBACK, + ERR_SOCKET_CLOSED, + ERR_TLS_ALPN_CALLBACK_INVALID_RESULT, + ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS, + ERR_TLS_DH_PARAM_SIZE, + ERR_TLS_HANDSHAKE_TIMEOUT, + ERR_TLS_INVALID_CONTEXT, + ERR_TLS_INVALID_STATE, + ERR_TLS_RENEGOTIATION_DISABLED, + ERR_TLS_REQUIRED_SERVER_NAME, + ERR_TLS_SESSION_ATTACK, + ERR_TLS_SNI_FROM_SERVER, + }, +} = require('internal/errors'); const { onpskexchange: kOnPskExchange } = internalBinding('symbols'); const { getOptionValue, diff --git a/lib/buffer.js b/lib/buffer.js index d593d9832f56e3..823a9249eccc52 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -103,8 +103,8 @@ const { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_INVALID_BUFFER_SIZE, - ERR_OUT_OF_RANGE, ERR_MISSING_ARGS, + ERR_OUT_OF_RANGE, ERR_UNKNOWN_ENCODING, }, genericNodeError, diff --git a/lib/child_process.js b/lib/child_process.js index 6ecfb0b2d5a111..41206dc7eda1f7 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -64,16 +64,15 @@ const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap'); const { AbortError, - codes: errorCodes, + codes: { + ERR_CHILD_PROCESS_IPC_REQUIRED, + ERR_CHILD_PROCESS_STDIO_MAXBUFFER, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_OUT_OF_RANGE, + }, genericNodeError, } = require('internal/errors'); -const { - ERR_INVALID_ARG_VALUE, - ERR_CHILD_PROCESS_IPC_REQUIRED, - ERR_CHILD_PROCESS_STDIO_MAXBUFFER, - ERR_INVALID_ARG_TYPE, - ERR_OUT_OF_RANGE, -} = errorCodes; const { clearTimeout, setTimeout } = require('timers'); const { getValidatedPath } = require('internal/fs/utils'); const { diff --git a/lib/dgram.js b/lib/dgram.js index e34e2e81f8d43d..a9e9e2a6a9a038 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -34,24 +34,27 @@ const { SymbolDispose, } = primordials; -const errors = require('internal/errors'); +const { + ErrnoException, + ExceptionWithHostPort, + codes: { + ERR_BUFFER_OUT_OF_BOUNDS, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_FD_TYPE, + ERR_MISSING_ARGS, + ERR_SOCKET_ALREADY_BOUND, + ERR_SOCKET_BAD_BUFFER_SIZE, + ERR_SOCKET_BUFFER_SIZE, + ERR_SOCKET_DGRAM_IS_CONNECTED, + ERR_SOCKET_DGRAM_NOT_CONNECTED, + ERR_SOCKET_DGRAM_NOT_RUNNING, + }, +} = require('internal/errors'); const { kStateSymbol, _createSocketHandle, newHandle, } = require('internal/dgram'); -const { - ERR_BUFFER_OUT_OF_BOUNDS, - ERR_INVALID_ARG_TYPE, - ERR_MISSING_ARGS, - ERR_SOCKET_ALREADY_BOUND, - ERR_SOCKET_BAD_BUFFER_SIZE, - ERR_SOCKET_BUFFER_SIZE, - ERR_SOCKET_DGRAM_IS_CONNECTED, - ERR_SOCKET_DGRAM_NOT_CONNECTED, - ERR_SOCKET_DGRAM_NOT_RUNNING, - ERR_INVALID_FD_TYPE, -} = errors.codes; const { isInt32, validateAbortSignal, @@ -97,11 +100,6 @@ function lazyLoadCluster() { return _cluster; } -const { - ErrnoException, - ExceptionWithHostPort, -} = errors; - function Socket(type, listener) { FunctionPrototypeCall(EventEmitter, this); let lookup; diff --git a/lib/dns.js b/lib/dns.js index 9924f07f41947b..219934f00d84cd 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -31,12 +31,12 @@ const cares = internalBinding('cares_wrap'); const { isIP } = require('internal/net'); const { customPromisifyArgs } = require('internal/util'); const { + DNSException, codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS, }, - DNSException, } = require('internal/errors'); const { bindDefaultResolver, diff --git a/lib/events.js b/lib/events.js index 83d85a1dc1b203..8b30b13272a1cb 100644 --- a/lib/events.js +++ b/lib/events.js @@ -66,13 +66,13 @@ let kResistStopPropagation; const { AbortError, - kEnhanceStackBeforeInspector, codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_THIS, ERR_UNHANDLED_ERROR, }, genericNodeError, + kEnhanceStackBeforeInspector, } = require('internal/errors'); const { diff --git a/lib/fs.js b/lib/fs.js index 1b4647498fc642..a16fcf426a641d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -70,13 +70,13 @@ const { Buffer } = require('buffer'); const { isBuffer: BufferIsBuffer } = Buffer; const BufferToString = uncurryThis(Buffer.prototype.toString); const { + AbortError, aggregateTwoErrors, codes: { ERR_ACCESS_DENIED, ERR_FS_FILE_TOO_LARGE, ERR_INVALID_ARG_VALUE, }, - AbortError, } = require('internal/errors'); const { diff --git a/lib/internal/assert/calltracker.js b/lib/internal/assert/calltracker.js index 0e2275305321ef..0cbfea6d23d038 100644 --- a/lib/internal/assert/calltracker.js +++ b/lib/internal/assert/calltracker.js @@ -14,8 +14,8 @@ const { const { codes: { - ERR_UNAVAILABLE_DURING_EXIT, ERR_INVALID_ARG_VALUE, + ERR_UNAVAILABLE_DURING_EXIT, }, } = require('internal/errors'); const AssertionError = require('internal/assert/assertion_error'); diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 55ef31c72f1871..e2cf4ada95fb91 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -56,11 +56,11 @@ const { inspect } = require('internal/util/inspect'); const { codes: { + ERR_BUFFER_TOO_LARGE, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, - ERR_INVALID_THIS, ERR_INVALID_STATE, - ERR_BUFFER_TOO_LARGE, + ERR_INVALID_THIS, }, } = require('internal/errors'); diff --git a/lib/internal/bootstrap/switches/does_not_own_process_state.js b/lib/internal/bootstrap/switches/does_not_own_process_state.js index 21be25384ff673..8350a7c79d8ba2 100644 --- a/lib/internal/bootstrap/switches/does_not_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_not_own_process_state.js @@ -23,7 +23,9 @@ if (credentials.implementsPosixCredentials) { // ---- compare the setups side-by-side ----- const { - codes: { ERR_WORKER_UNSUPPORTED_OPERATION }, + codes: { + ERR_WORKER_UNSUPPORTED_OPERATION, + }, } = require('internal/errors'); function wrappedUmask(mask) { diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 36868a34fe5635..a248afb3999b65 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -40,12 +40,12 @@ const { const { trace } = internalBinding('trace_events'); const { - isStackOverflowError, codes: { ERR_CONSOLE_WRITABLE_STREAM, - ERR_INVALID_ARG_VALUE, ERR_INCOMPATIBLE_OPTION_PAIR, + ERR_INVALID_ARG_VALUE, }, + isStackOverflowError, } = require('internal/errors'); const { validateArray, diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 260458e1f2a0c5..ee1d09581696d3 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -35,8 +35,8 @@ const { const { codes: { ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS, - ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE, ERR_CRYPTO_INVALID_JWK, + ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE, ERR_ILLEGAL_CONSTRUCTOR, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 0533216969bc8e..80cf3224744ec7 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -40,8 +40,8 @@ const { Buffer, kMaxLength } = require('buffer'); const { codes: { ERR_INVALID_ARG_TYPE, - ERR_OUT_OF_RANGE, ERR_OPERATION_FAILED, + ERR_OUT_OF_RANGE, }, } = require('internal/errors'); diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index e429cbc1e744e6..368b1bc012eff0 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -43,7 +43,6 @@ const { const normalizeHashName = require('internal/crypto/hashnames'); const { - hideStackFrames, codes: { ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED, ERR_CRYPTO_ENGINE_UNKNOWN, @@ -51,6 +50,7 @@ const { ERR_INVALID_ARG_VALUE, ERR_OUT_OF_RANGE, }, + hideStackFrames, } = require('internal/errors'); const { diff --git a/lib/internal/dgram.js b/lib/internal/dgram.js index 435adaeaec7e73..bae5da1c1f0def 100644 --- a/lib/internal/dgram.js +++ b/lib/internal/dgram.js @@ -5,7 +5,9 @@ const { Symbol, } = primordials; -const { codes } = require('internal/errors'); +const { codes: { + ERR_SOCKET_BAD_TYPE, +} } = require('internal/errors'); const { UDP } = internalBinding('udp_wrap'); const { guessHandleType } = require('internal/util'); const { @@ -13,9 +15,6 @@ const { validateFunction, } = require('internal/validators'); const { UV_EINVAL } = internalBinding('uv'); -const { - ERR_SOCKET_BAD_TYPE, -} = codes; const kStateSymbol = Symbol('state symbol'); let dns; // Lazy load for startup performance. diff --git a/lib/internal/dns/callback_resolver.js b/lib/internal/dns/callback_resolver.js index 9675d680f3ab0d..c7bf1ce1d69cfd 100644 --- a/lib/internal/dns/callback_resolver.js +++ b/lib/internal/dns/callback_resolver.js @@ -8,11 +8,11 @@ const { } = primordials; const { + DNSException, codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, }, - DNSException, } = require('internal/errors'); const { diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 602ff447fd31f5..59e5252f7e14d5 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -44,7 +44,14 @@ const { ADDRGETNETWORKPARAMS, CANCELLED, } = dnsErrorCodes; -const { codes, DNSException } = require('internal/errors'); +const { + DNSException, + codes: { + ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, + ERR_MISSING_ARGS, + }, +} = require('internal/errors'); const { isIP } = require('internal/net'); const { getaddrinfo, @@ -56,11 +63,6 @@ const { DNS_ORDER_IPV4_FIRST, DNS_ORDER_IPV6_FIRST, } = internalBinding('cares_wrap'); -const { - ERR_INVALID_ARG_TYPE, - ERR_INVALID_ARG_VALUE, - ERR_MISSING_ARGS, -} = codes; const { validateBoolean, validateNumber, diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index a79a5b846a6d18..e8c8accc501e0d 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -11,7 +11,13 @@ const { Symbol, } = primordials; -const errors = require('internal/errors'); +const { + codes: { + ERR_DNS_SET_SERVERS_FAILED, + ERR_INVALID_ARG_VALUE, + ERR_INVALID_IP_ADDRESS, + }, +} = require('internal/errors'); const { isIP } = require('internal/net'); const { getOptionValue } = require('internal/options'); const { @@ -28,11 +34,6 @@ function lazyBinding() { const IANA_DNS_PORT = 53; const IPv6RE = /^\[([^[\]]*)\]/; const addrSplitRE = /(^.+?)(?::(\d+))?$/; -const { - ERR_DNS_SET_SERVERS_FAILED, - ERR_INVALID_ARG_VALUE, - ERR_INVALID_IP_ADDRESS, -} = errors.codes; const { namespace: { diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 4f598ec812a3d0..e29b7eb58638e6 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1,5 +1,5 @@ /* eslint node-core/documented-errors: "error" */ -/* eslint node-core/alphabetize-errors: "error" */ +/* eslint node-core/alphabetize-errors: ["error", {checkErrorDeclarations: true}] */ /* eslint node-core/prefer-util-format-errors: "error" */ 'use strict'; diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index bbe56c5578f329..6a774eb94465ba 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -25,10 +25,10 @@ const { const { codes: { - ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, - ERR_MISSING_ARGS, + ERR_INVALID_ARG_TYPE, ERR_INVALID_THIS, + ERR_MISSING_ARGS, }, } = require('internal/errors'); const { diff --git a/lib/internal/fs/cp/cp-sync.js b/lib/internal/fs/cp/cp-sync.js index fe342f2dda19ec..2aaf36aa7df47e 100644 --- a/lib/internal/fs/cp/cp-sync.js +++ b/lib/internal/fs/cp/cp-sync.js @@ -3,18 +3,7 @@ // This file is a modified version of the fs-extra's copySync method. const { areIdentical, isSrcSubdir } = require('internal/fs/cp/cp'); -const { codes } = require('internal/errors'); -const { - os: { - errno: { - EEXIST, - EISDIR, - EINVAL, - ENOTDIR, - }, - }, -} = internalBinding('constants'); -const { +const { codes: { ERR_FS_CP_DIR_TO_NON_DIR, ERR_FS_CP_EEXIST, ERR_FS_CP_EINVAL, @@ -25,7 +14,17 @@ const { ERR_FS_CP_UNKNOWN, ERR_FS_EISDIR, ERR_INVALID_RETURN_VALUE, -} = codes; +} } = require('internal/errors'); +const { + os: { + errno: { + EEXIST, + EISDIR, + EINVAL, + ENOTDIR, + }, + }, +} = internalBinding('constants'); const { chmodSync, copyFileSync, diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 5ae9966d937135..3b6bc934810ccd 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -35,6 +35,8 @@ const { isBuffer: BufferIsBuffer } = Buffer; const BufferToString = uncurryThis(Buffer.prototype.toString); const { + AbortError, + aggregateTwoErrors, codes: { ERR_ACCESS_DENIED, ERR_FS_FILE_TOO_LARGE, @@ -42,8 +44,6 @@ const { ERR_INVALID_STATE, ERR_METHOD_NOT_IMPLEMENTED, }, - AbortError, - aggregateTwoErrors, } = require('internal/errors'); const { isArrayBufferView } = require('internal/util/types'); diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 10aeae4dfa4b8f..ca15c986d3a4fb 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -31,6 +31,7 @@ const permission = require('internal/process/permission'); const { Buffer } = require('buffer'); const { + UVException, codes: { ERR_FS_EISDIR, ERR_FS_INVALID_SYMLINK_TYPE, @@ -40,7 +41,6 @@ const { ERR_OUT_OF_RANGE, }, hideStackFrames, - UVException, } = require('internal/errors'); const { isArrayBufferView, diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index cb757313009dc0..fe0fab0ea5d2f7 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -24,8 +24,8 @@ const { inspect } = require('util'); const { codes: { ERR_ILLEGAL_CONSTRUCTOR, - ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_INVALID_THIS, ERR_OUT_OF_RANGE, }, diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 0e9bf899b5d9ff..c9dfc8a2769894 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -71,6 +71,7 @@ const { }, } = require('internal/async_hooks'); const { + AbortError, aggregateTwoErrors, codes: { ERR_HTTP2_ALTSVC_INVALID_ORIGIN, @@ -120,7 +121,6 @@ const { ERR_SOCKET_CLOSED, }, hideStackFrames, - AbortError, } = require('internal/errors'); const { isUint32, diff --git a/lib/internal/net.js b/lib/internal/net.js index 0f2fc5dfff5ea8..cc616000777e4d 100644 --- a/lib/internal/net.js +++ b/lib/internal/net.js @@ -8,7 +8,9 @@ const { const Buffer = require('buffer').Buffer; const { writeBuffer } = internalBinding('fs'); -const errors = require('internal/errors'); +const { + UVException, +} = require('internal/errors'); // IPv4 Segment const v4Seg = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])'; @@ -58,7 +60,7 @@ function makeSyncWrite(fd) { const ctx = {}; writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx); if (ctx.errno !== undefined) { - const ex = new errors.UVException(ctx); + const ex = new UVException(ctx); ex.errno = ctx.errno; return cb(ex); } diff --git a/lib/internal/perf/observe.js b/lib/internal/perf/observe.js index 244d01f541d886..048eb47cebaae7 100644 --- a/lib/internal/perf/observe.js +++ b/lib/internal/perf/observe.js @@ -43,8 +43,8 @@ const { const { codes: { ERR_ILLEGAL_CONSTRUCTOR, - ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS, }, } = require('internal/errors'); diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 9fcaa8372f825f..10f9e52244cac2 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -10,9 +10,9 @@ const path = require('path'); const { codes: { + ERR_EVAL_ESM_CANNOT_PRINT, ERR_INVALID_ARG_TYPE, ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET, - ERR_EVAL_ESM_CANNOT_PRINT, }, } = require('internal/errors'); const { pathToFileURL } = require('internal/url'); diff --git a/lib/internal/process/worker_thread_only.js b/lib/internal/process/worker_thread_only.js index 2f5a10c9d63f19..60c5637c85d1bc 100644 --- a/lib/internal/process/worker_thread_only.js +++ b/lib/internal/process/worker_thread_only.js @@ -4,7 +4,9 @@ // run in the worker thread. const { - codes: { ERR_WORKER_UNSUPPORTED_OPERATION }, + codes: { + ERR_WORKER_UNSUPPORTED_OPERATION, + }, } = require('internal/errors'); function unavailable(name) { diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 4b48d7ef9932b7..74d2e91e98b145 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -33,12 +33,11 @@ const { SymbolDispose, } = primordials; -const { codes } = require('internal/errors'); - -const { +const { codes: { ERR_INVALID_ARG_VALUE, ERR_USE_AFTER_CLOSE, -} = codes; +} } = require('internal/errors'); + const { validateAbortSignal, validateArray, diff --git a/lib/internal/readline/promises.js b/lib/internal/readline/promises.js index 1a0c7b4c809d47..7355dcfbfecd0f 100644 --- a/lib/internal/readline/promises.js +++ b/lib/internal/readline/promises.js @@ -9,7 +9,9 @@ const { const { CSI } = require('internal/readline/utils'); const { validateBoolean, validateInteger } = require('internal/validators'); const { isWritable } = require('internal/streams/utils'); -const { codes: { ERR_INVALID_ARG_TYPE } } = require('internal/errors'); +const { codes: { + ERR_INVALID_ARG_TYPE, +} } = require('internal/errors'); const { kClearToLineBeginning, diff --git a/lib/internal/streams/add-abort-signal.js b/lib/internal/streams/add-abort-signal.js index 10e5545a28345b..2fa16d697c11b6 100644 --- a/lib/internal/streams/add-abort-signal.js +++ b/lib/internal/streams/add-abort-signal.js @@ -6,7 +6,9 @@ const { const { AbortError, - codes, + codes: { + ERR_INVALID_ARG_TYPE, + }, } = require('internal/errors'); const { @@ -16,7 +18,6 @@ const { } = require('internal/streams/utils'); const eos = require('internal/streams/end-of-stream'); -const { ERR_INVALID_ARG_TYPE } = codes; let addAbortListener; // This method is inlined here for readable-stream diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 185ba6a17a235b..3119de5dd9d921 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -3,12 +3,13 @@ const { Symbol, } = primordials; + const { + AbortError, aggregateTwoErrors, codes: { ERR_MULTIPLE_CALLBACK, }, - AbortError, } = require('internal/errors'); const { kIsDestroyed, diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index fc0cbb454946ec..dd8e4e5d8a0c7e 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -11,12 +11,11 @@ const { const { AbortError, - codes, + codes: { + ERR_INVALID_ARG_TYPE, + ERR_STREAM_PREMATURE_CLOSE, + }, } = require('internal/errors'); -const { - ERR_INVALID_ARG_TYPE, - ERR_STREAM_PREMATURE_CLOSE, -} = codes; const { kEmptyObject, once, diff --git a/lib/internal/streams/operators.js b/lib/internal/streams/operators.js index 36b657a727dc41..80a0f9f731e89a 100644 --- a/lib/internal/streams/operators.js +++ b/lib/internal/streams/operators.js @@ -16,13 +16,13 @@ const { const { AbortController, AbortSignal } = require('internal/abort_controller'); const { + AbortError, codes: { - ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS, ERR_OUT_OF_RANGE, }, - AbortError, } = require('internal/errors'); const { validateAbortSignal, diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 4cf6b02766b6a8..edf1d37f9fe3bf 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -15,6 +15,7 @@ const { once } = require('internal/util'); const destroyImpl = require('internal/streams/destroy'); const Duplex = require('internal/streams/duplex'); const { + AbortError, aggregateTwoErrors, codes: { ERR_INVALID_ARG_TYPE, @@ -23,7 +24,6 @@ const { ERR_STREAM_DESTROYED, ERR_STREAM_PREMATURE_CLOSE, }, - AbortError, } = require('internal/errors'); const { diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index ff07a2fd89afe0..1cba326970d4a8 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -74,6 +74,7 @@ const { } = require('internal/streams/utils'); const { + AbortError, aggregateTwoErrors, codes: { ERR_INVALID_ARG_TYPE, @@ -83,7 +84,6 @@ const { ERR_STREAM_UNSHIFT_AFTER_END_EVENT, ERR_UNKNOWN_ENCODING, }, - AbortError, } = require('internal/errors'); const { validateObject } = require('internal/validators'); diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 9991017653ed42..35c9a1ab68aac8 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -32,7 +32,10 @@ const { } = require('internal/util'); const { AbortError, - codes: { ERR_INVALID_STATE, ERR_INVALID_ARG_VALUE }, + codes: { + ERR_INVALID_ARG_VALUE, + ERR_INVALID_STATE, + }, } = require('internal/errors'); const PriorityQueue = require('internal/priority_queue'); diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index bd86ad38023306..8310e13248898a 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -42,8 +42,8 @@ const { codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, - ERR_TEST_FAILURE, ERR_OUT_OF_RANGE, + ERR_TEST_FAILURE, }, } = require('internal/errors'); const { diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 8b1a5b3747187c..d6855b300b8674 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -33,11 +33,11 @@ const { queueMicrotask } = require('internal/process/task_queues'); const { AsyncResource } = require('async_hooks'); const { AbortController } = require('internal/abort_controller'); const { + AbortError, codes: { ERR_INVALID_ARG_TYPE, ERR_TEST_FAILURE, }, - AbortError, } = require('internal/errors'); const { MockTracker } = require('internal/test_runner/mock/mock'); const { TestsStream } = require('internal/test_runner/tests_stream'); diff --git a/lib/internal/util.js b/lib/internal/util.js index 364f85c1056fc8..7bf210afce1078 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -50,8 +50,8 @@ const { ERR_UNKNOWN_SIGNAL, }, isErrorStackTraceLimitWritable, - uvErrmapGet, overrideStackTrace, + uvErrmapGet, } = require('internal/errors'); const { signals } = internalBinding('constants').os; const { diff --git a/lib/internal/util/embedding.js b/lib/internal/util/embedding.js index 7e4cd565492843..fde209607c363a 100644 --- a/lib/internal/util/embedding.js +++ b/lib/internal/util/embedding.js @@ -1,7 +1,9 @@ 'use strict'; const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); const { Module, wrapSafe } = require('internal/modules/cjs/loader'); -const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); +const { codes: { + ERR_UNKNOWN_BUILTIN_MODULE, +} } = require('internal/errors'); const { getCodePath, isSea } = internalBinding('sea'); // This is roughly the same as: diff --git a/lib/internal/util/parse_args/parse_args.js b/lib/internal/util/parse_args/parse_args.js index 6915f256d62aab..d0a5a5569dc13b 100644 --- a/lib/internal/util/parse_args/parse_args.js +++ b/lib/internal/util/parse_args/parse_args.js @@ -45,8 +45,8 @@ const { codes: { ERR_INVALID_ARG_VALUE, ERR_PARSE_ARGS_INVALID_OPTION_VALUE, - ERR_PARSE_ARGS_UNKNOWN_OPTION, ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL, + ERR_PARSE_ARGS_UNKNOWN_OPTION, }, } = require('internal/errors'); diff --git a/lib/internal/v8/startup_snapshot.js b/lib/internal/v8/startup_snapshot.js index 5de746293d06e8..7c789577aec969 100644 --- a/lib/internal/v8/startup_snapshot.js +++ b/lib/internal/v8/startup_snapshot.js @@ -5,9 +5,9 @@ const { } = require('internal/validators'); const { codes: { + ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION, ERR_NOT_BUILDING_SNAPSHOT, ERR_NOT_SUPPORTED_IN_SNAPSHOT, - ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION, }, } = require('internal/errors'); diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 35d18a9d1fc4a4..7bda5150a97c0f 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -20,14 +20,14 @@ const { } = primordials; const { - hideStackFrames, codes: { - ERR_SOCKET_BAD_PORT: { HideStackFramesError: ERR_SOCKET_BAD_PORT }, ERR_INVALID_ARG_TYPE: { HideStackFramesError: ERR_INVALID_ARG_TYPE }, ERR_INVALID_ARG_VALUE: { HideStackFramesError: ERR_INVALID_ARG_VALUE }, ERR_OUT_OF_RANGE: { HideStackFramesError: ERR_OUT_OF_RANGE }, + ERR_SOCKET_BAD_PORT: { HideStackFramesError: ERR_SOCKET_BAD_PORT }, ERR_UNKNOWN_SIGNAL: { HideStackFramesError: ERR_UNKNOWN_SIGNAL }, }, + hideStackFrames, } = require('internal/errors'); const { normalizeEncoding } = require('internal/util'); const { diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 27abff94ca5685..34099d22696401 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -52,6 +52,7 @@ const { } = require('buffer'); const { + AbortError, ErrnoException, codes: { ERR_INVALID_ARG_TYPE, @@ -59,7 +60,6 @@ const { ERR_INVALID_STATE, ERR_STREAM_PREMATURE_CLOSE, }, - AbortError, } = require('internal/errors'); const { diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 76c690fb7a4e9c..dbf30326d1e97b 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -30,8 +30,8 @@ const { AbortError, codes: { ERR_ILLEGAL_CONSTRUCTOR, - ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_INVALID_STATE, ERR_INVALID_THIS, ERR_OUT_OF_RANGE, diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js index e862b3ffe25724..cce67d5b04fed2 100644 --- a/lib/internal/webstreams/util.js +++ b/lib/internal/webstreams/util.js @@ -19,8 +19,8 @@ const { const { codes: { ERR_INVALID_ARG_VALUE, - ERR_OPERATION_FAILED, ERR_INVALID_STATE, + ERR_OPERATION_FAILED, }, } = require('internal/errors'); diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index 339ce6718a6f06..9842f852247f67 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -18,8 +18,8 @@ const { const { codes: { ERR_ILLEGAL_CONSTRUCTOR, - ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_INVALID_STATE, ERR_INVALID_THIS, }, diff --git a/lib/net.js b/lib/net.js index d5ef6827a42bdc..1e48e79cce435e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -93,25 +93,25 @@ const { kBufferGen, } = require('internal/stream_base_commons'); const { + ErrnoException, + ExceptionWithHostPort, + NodeAggregateError, + UVExceptionWithHostPort, codes: { ERR_INVALID_ADDRESS_FAMILY, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_INVALID_FD_TYPE, - ERR_INVALID_IP_ADDRESS, ERR_INVALID_HANDLE_TYPE, + ERR_INVALID_IP_ADDRESS, + ERR_MISSING_ARGS, ERR_SERVER_ALREADY_LISTEN, ERR_SERVER_NOT_RUNNING, - ERR_SOCKET_CONNECTION_TIMEOUT, ERR_SOCKET_CLOSED, ERR_SOCKET_CLOSED_BEFORE_CONNECTION, - ERR_MISSING_ARGS, + ERR_SOCKET_CONNECTION_TIMEOUT, }, - ErrnoException, - ExceptionWithHostPort, genericNodeError, - NodeAggregateError, - UVExceptionWithHostPort, } = require('internal/errors'); const { isUint8Array } = require('internal/util/types'); const { queueMicrotask } = require('internal/process/task_queues'); diff --git a/lib/repl.js b/lib/repl.js index 45c9b2fd50e572..6e2d8120ad2167 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -143,6 +143,7 @@ let debug = require('internal/util/debuglog').debuglog('repl', (fn) => { debug = fn; }); const { + ErrorPrepareStackTrace, codes: { ERR_CANNOT_WATCH_SIGINT, ERR_INVALID_REPL_EVAL_CONFIG, @@ -152,7 +153,6 @@ const { }, isErrorStackTraceLimitWritable, overrideStackTrace, - ErrorPrepareStackTrace, } = require('internal/errors'); const { sendInspectorCommand } = require('internal/util/inspector'); const { getOptionValue } = require('internal/options'); diff --git a/lib/tty.js b/lib/tty.js index aa929cd9ba0d08..1a3fa5afe53a41 100644 --- a/lib/tty.js +++ b/lib/tty.js @@ -29,8 +29,13 @@ const { const net = require('net'); const { TTY, isTTY } = internalBinding('tty_wrap'); -const errors = require('internal/errors'); -const { ERR_INVALID_FD, ERR_TTY_INIT_FAILED } = errors.codes; +const { + ErrnoException, + codes: { + ERR_INVALID_FD, + ERR_TTY_INIT_FAILED, + }, +} = require('internal/errors'); const { getColorDepth, hasColors, @@ -74,7 +79,7 @@ ReadStream.prototype.setRawMode = function(flag) { flag = !!flag; const err = this._handle?.setRawMode(flag); if (err) { - this.emit('error', new errors.ErrnoException(err, 'setRawMode')); + this.emit('error', new ErrnoException(err, 'setRawMode')); return this; } this.isRaw = flag; @@ -129,7 +134,7 @@ WriteStream.prototype._refreshSize = function() { const winSize = new Array(2); const err = this._handle.getWindowSize(winSize); if (err) { - this.emit('error', new errors.ErrnoException(err, 'getWindowSize')); + this.emit('error', new ErrnoException(err, 'getWindowSize')); return; } const { 0: newCols, 1: newRows } = winSize; diff --git a/lib/util.js b/lib/util.js index 6f9076b5eeba51..11d69e6fa0f460 100644 --- a/lib/util.js +++ b/lib/util.js @@ -48,14 +48,14 @@ const { } = primordials; const { + ErrnoException, + ExceptionWithHostPort, codes: { ERR_FALSY_VALUE_REJECTION, ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE, }, isErrorStackTraceLimitWritable, - ErrnoException, - ExceptionWithHostPort, } = require('internal/errors'); const { format, diff --git a/test/parallel/test-eslint-alphabetize-errors.js b/test/parallel/test-eslint-alphabetize-errors.js index 770602353a757b..08382e6468d92b 100644 --- a/test/parallel/test-eslint-alphabetize-errors.js +++ b/test/parallel/test-eslint-alphabetize-errors.js @@ -9,13 +9,27 @@ common.skipIfEslintMissing(); const RuleTester = require('../../tools/node_modules/eslint').RuleTester; const rule = require('../../tools/eslint-rules/alphabetize-errors'); -new RuleTester().run('alphabetize-errors', rule, { +new RuleTester({ + parserOptions: { ecmaVersion: 6 }, + env: { es6: true } +}).run('alphabetize-errors', rule, { valid: [ - ` + { code: ` E('AAA', 'foo'); E('BBB', 'bar'); E('CCC', 'baz'); + `, options: [{ checkErrorDeclarations: true }] }, + ` + E('AAA', 'foo'); + E('CCC', 'baz'); + E('BBB', 'bar'); `, + `const { + codes: { + ERR_A, + ERR_B, + }, + } = require("internal/errors")`, ], invalid: [ { @@ -24,7 +38,29 @@ new RuleTester().run('alphabetize-errors', rule, { E('AAA', 'foo'); E('CCC', 'baz'); `, + options: [{ checkErrorDeclarations: true }], errors: [{ message: 'Out of ASCIIbetical order - BBB >= AAA', line: 3 }] }, + { + code: `const { + codes: { + ERR_B, + ERR_A, + }, + } = require("internal/errors")`, + errors: [{ message: 'Out of ASCIIbetical order - ERR_B >= ERR_A', line: 4 }] + }, + { + code: 'const internalErrors = require("internal/errors")', + errors: [{ message: /Use destructuring/ }] + }, + { + code: 'const {codes} = require("internal/errors")', + errors: [{ message: /Use destructuring/ }] + }, + { + code: 'const {codes:{ERR_A}} = require("internal/errors")', + errors: [{ message: /Use multiline destructuring/ }] + }, ] }); diff --git a/tools/eslint-rules/alphabetize-errors.js b/tools/eslint-rules/alphabetize-errors.js index 8d25f7973d62a6..26b0171490aa47 100644 --- a/tools/eslint-rules/alphabetize-errors.js +++ b/tools/eslint-rules/alphabetize-errors.js @@ -9,12 +9,29 @@ function errorForNode(node) { return node.expression.arguments[0].value; } +const requireInternalErrorsSelector = + 'VariableDeclarator' + + '[init.type="CallExpression"]' + + '[init.callee.name="require"]' + + '[init.arguments.length=1]' + + '[init.arguments.0.value="internal/errors"]'; +const codesSelector = requireInternalErrorsSelector + '>*>Property[key.name="codes"]'; + module.exports = { + meta: { + schema: [{ + type: 'object', + properties: { + checkErrorDeclarations: { type: 'boolean' }, + }, + additionalProperties: false, + }], + }, create: function(context) { let previousNode; return { ExpressionStatement: function(node) { - if (!isDefiningError(node)) return; + if (context.options.every((option) => option.checkErrorDeclarations !== true) || !isDefiningError(node)) return; if (!previousNode) { previousNode = node; return; @@ -27,6 +44,32 @@ module.exports = { context.report({ node, message }); } }, + + [requireInternalErrorsSelector](node) { + if (node.id.type !== 'ObjectPattern') { + context.report({ node, message: 'Use destructuring to define error codes used in the file' }); + } + }, + + [codesSelector](node) { + if (node.value.type !== 'ObjectPattern') { + context.report({ node, message: 'Use destructuring to define error codes used in the file' }); + return; + } + if (node.value.loc.start.line === node.value.loc.end.line) { + context.report({ node, message: 'Use multiline destructuring for error codes' }); + } + }, + + [requireInternalErrorsSelector + ' Property:not(:first-child)'](node) { + const { properties } = node.parent; + const prev = properties[properties.indexOf(node) - 1].key.name; + const curr = node.key.name; + if (prev >= curr) { + const message = [prefix, prev, opStr, curr].join(''); + context.report({ node, message }); + } + }, }; }, };