diff --git a/lib/core/errors.js b/lib/core/errors.js index 7a68cf813a5..33e7123367d 100644 --- a/lib/core/errors.js +++ b/lib/core/errors.js @@ -1,218 +1,313 @@ 'use strict' class UndiciError extends Error { - constructor (message, options) { - super(message, options) - this.name = 'UndiciError' - this.code = 'UND_ERR' - } + name = /** @type {string} */ 'UndiciError' + code = /** @type {string} */ 'UND_ERR' } +/** + * Connect timeout error. + */ class ConnectTimeoutError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('ConnectTimeoutError') + code = /** @type {const} */ ('UND_ERR_CONNECT_TIMEOUT') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'ConnectTimeoutError' this.message = message || 'Connect Timeout Error' - this.code = 'UND_ERR_CONNECT_TIMEOUT' } } - +/** + * A header exceeds the `headersTimeout` option. + */ class HeadersTimeoutError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('HeadersTimeoutError') + code = /** @type {const} */ ('UND_ERR_HEADERS_TIMEOUT') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'HeadersTimeoutError' this.message = message || 'Headers Timeout Error' - this.code = 'UND_ERR_HEADERS_TIMEOUT' } } +/** + * Headers overflow error. + */ class HeadersOverflowError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('HeadersOverflowError') + code = /** @type {const} */ ('UND_ERR_HEADERS_OVERFLOW') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'HeadersOverflowError' this.message = message || 'Headers Overflow Error' - this.code = 'UND_ERR_HEADERS_OVERFLOW' } } +/** + * A body exceeds the `bodyTimeout` option. + */ class BodyTimeoutError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('BodyTimeoutError') + code = /** @type {const} */ ('UND_ERR_BODY_TIMEOUT') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'BodyTimeoutError' this.message = message || 'Body Timeout Error' - this.code = 'UND_ERR_BODY_TIMEOUT' } } class ResponseStatusCodeError extends UndiciError { - constructor (message, statusCode, headers, body) { + name = /** @type {const} */ ('ResponseStatusCodeError') + code = /** @type {const} */ ('UND_ERR_RESPONSE_STATUS_CODE') + constructor ( + /** @type {string} */ message, + /** @type {number} */ statusCode, + /** @type {Record|string[]|null} */ headers, + /** @type {*} */ body + ) { super(message) - this.name = 'ResponseStatusCodeError' this.message = message || 'Response Status Code Error' - this.code = 'UND_ERR_RESPONSE_STATUS_CODE' - this.body = body - this.status = statusCode this.statusCode = statusCode + this.status = statusCode + this.body = body this.headers = headers } } +/** + * Passed an invalid argument. + */ class InvalidArgumentError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('InvalidArgumentError') + code = /** @type {const} */ ('UND_ERR_INVALID_ARG') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'InvalidArgumentError' this.message = message || 'Invalid Argument Error' - this.code = 'UND_ERR_INVALID_ARG' } } +/** + * Returned an invalid value. + */ class InvalidReturnValueError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('InvalidReturnValueError') + code = /** @type {const} */ ('UND_ERR_INVALID_RETURN_VALUE') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'InvalidReturnValueError' this.message = message || 'Invalid Return Value Error' - this.code = 'UND_ERR_INVALID_RETURN_VALUE' } } class AbortError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('AbortError') + code = /** @type {string} */ ('UND_ERR_ABORT') + constructor (/** @type {string} **/ message) { super(message) - this.name = 'AbortError' this.message = message || 'The operation was aborted' } } +/** + * The request has been aborted by the user. + */ class RequestAbortedError extends AbortError { - constructor (message) { + name = /** @type {const} */ ('AbortError') + code = /** @type {const} */ ('UND_ERR_ABORTED') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'AbortError' this.message = message || 'Request aborted' - this.code = 'UND_ERR_ABORTED' } } +/** + * Expected error with reason. + */ class InformationalError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('InformationalError') + code = /** @type {const} */ ('UND_ERR_INFO') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'InformationalError' this.message = message || 'Request information' - this.code = 'UND_ERR_INFO' } } +/** + * Request body length does not match content-length header. + */ class RequestContentLengthMismatchError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('RequestContentLengthMismatchError') + code = /** @type {const} */ ('UND_ERR_REQ_CONTENT_LENGTH_MISMATCH') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'RequestContentLengthMismatchError' this.message = message || 'Request body length does not match content-length header' - this.code = 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH' } } +/** + * Response body length does not match content-length header. + */ class ResponseContentLengthMismatchError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('ResponseContentLengthMismatchError') + code = /** @type {const} */ ('UND_ERR_RES_CONTENT_LENGTH_MISMATCH') + constructor (/** @type {string} **/ message) { super(message) - this.name = 'ResponseContentLengthMismatchError' this.message = message || 'Response body length does not match content-length header' - this.code = 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH' } } +/** + * Trying to use a destroyed client. + */ class ClientDestroyedError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('ClientDestroyedError') + code = /** @type {const} */ ('UND_ERR_DESTROYED') + constructor (/** @type {string} **/ message) { super(message) - this.name = 'ClientDestroyedError' this.message = message || 'The client is destroyed' - this.code = 'UND_ERR_DESTROYED' } } class ClientClosedError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('ClientClosedError') + code = /** @type {const} */ ('UND_ERR_CLOSED') + constructor (/** @type {string} **/ message) { super(message) - this.name = 'ClientClosedError' this.message = message || 'The client is closed' - this.code = 'UND_ERR_CLOSED' } } +/** + * There is an error with the socket. + */ class SocketError extends UndiciError { - constructor (message, socket) { + name = /** @type {const} */ ('SocketError') + code = /** @type {const} */ ('UND_ERR_SOCKET') + + constructor ( + /** @type {string} **/ message, + /** @type {import('net').Socket|null} */ socket + ) { super(message) - this.name = 'SocketError' this.message = message || 'Socket error' - this.code = 'UND_ERR_SOCKET' this.socket = socket } } +/** + * Encountered unsupported functionality. + */ class NotSupportedError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('NotSupportedError') + code = /** @type {const} */ ('UND_ERR_NOT_SUPPORTED') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'NotSupportedError' this.message = message || 'Not supported error' - this.code = 'UND_ERR_NOT_SUPPORTED' } } +/** + * No upstream has been added to the BalancedPool. + */ class BalancedPoolMissingUpstreamError extends UndiciError { - constructor (message) { + name = /** @type {const} */ ('MissingUpstreamError') + code = /** @type {const} */ ('UND_ERR_BPL_MISSING_UPSTREAM') + + constructor (/** @type {string} **/ message) { super(message) - this.name = 'MissingUpstreamError' this.message = message || 'No upstream has been added to the BalancedPool' - this.code = 'UND_ERR_BPL_MISSING_UPSTREAM' } } -class HTTPParserError extends Error { - constructor (message, code, data) { +class HTTPParserError extends UndiciError { + name = /** {const} */ 'HTTPParserError' + code = /** {const} */ 'UND_ERR_HTTP_PARSER' + + constructor ( + /** @type {string} */ message, + /** @type {string} */ code, + /** @type {*} */ data + ) { super(message) - this.name = 'HTTPParserError' - this.code = code ? `HPE_${code}` : undefined + + code = (/** @type {`HPE_${string}|undefined`} */ (code ? `HPE_${code}` : undefined)) this.data = data ? data.toString() : undefined } } +/** + * The response exceed the length allowed. + */ class ResponseExceededMaxSizeError extends UndiciError { - constructor (message) { + name = /** @type {const} */ 'ResponseExceededMaxSizeError' + code = /** @type {const} */ 'UND_ERR_RES_EXCEEDED_MAX_SIZE' + + constructor (/** @type {string} */ message) { super(message) - this.name = 'ResponseExceededMaxSizeError' this.message = message || 'Response content exceeded max size' - this.code = 'UND_ERR_RES_EXCEEDED_MAX_SIZE' } } +/** + * @typedef RequestRetryErrorOptions + * @type {object} + * @property {object} data + * @property {number} count + * @property {Record|string[]|null} headers + */; + class RequestRetryError extends UndiciError { - constructor (message, code, { headers, data }) { + name = /** @type {const} */ 'RequestRetryError' + code = /** @type {const} */ 'UND_ERR_REQ_RETRY' + + constructor ( + /** @type {string} */ message, + /** @type {number} */ statusCode, + /** @type {RequestRetryErrorOptions} */ { headers, data }) { super(message) - this.name = 'RequestRetryError' this.message = message || 'Request retry error' - this.code = 'UND_ERR_REQ_RETRY' - this.statusCode = code + this.statusCode = statusCode this.data = data this.headers = headers } } +/** + * @typedef ResponseErrorOptions + * @type {object} + * @property {object} data + * @property {Record|string[]|null} headers + */; + class ResponseError extends UndiciError { - constructor (message, code, { headers, data }) { + name = /** @type {const} */ 'ResponseError' + code = /** @type {const} */ ('UND_ERR_RESPONSE') + + constructor ( + /** @type {string} */ message, + /** @type {number} */ statusCode, + /** @type {ResponseErrorOptions} */ { data, headers } + ) { super(message) - this.name = 'ResponseError' this.message = message || 'Response error' - this.code = 'UND_ERR_RESPONSE' - this.statusCode = code + this.statusCode = statusCode this.data = data this.headers = headers } } class SecureProxyConnectionError extends UndiciError { - constructor (cause, message, options = {}) { - super(message, { cause, ...options }) - this.name = 'SecureProxyConnectionError' + name = /** @type {const} */ 'SecureProxyConnectionError' + code = /** @type {const} */ ('UND_ERR_PRX_TLS') + + constructor (/** @type {Error} */ cause, /** @type {string} */ message) { + super(message, { cause }) this.message = message || 'Secure Proxy Connection failed' - this.code = 'UND_ERR_PRX_TLS' this.cause = cause } } diff --git a/lib/mock/mock-errors.js b/lib/mock/mock-errors.js index ebdc786c56e..4621b01415b 100644 --- a/lib/mock/mock-errors.js +++ b/lib/mock/mock-errors.js @@ -6,11 +6,12 @@ const { UndiciError } = require('../core/errors') * The request does not match any registered mock dispatches. */ class MockNotMatchedError extends UndiciError { + name = /** @type {const} */ ('MockNotMatchedError') + code = /** @type {const} */ ('UND_MOCK_ERR_MOCK_NOT_MATCHED') + constructor (message) { super(message) - this.name = 'MockNotMatchedError' this.message = message || 'The request does not match any registered mock dispatches' - this.code = 'UND_MOCK_ERR_MOCK_NOT_MATCHED' } } diff --git a/test/errors.js b/test/errors.js index e34b406a905..1284ec8665e 100644 --- a/test/errors.js +++ b/test/errors.js @@ -71,7 +71,7 @@ describe('Default HTTPParseError Codes', () => { const error = new errors.HTTPParserError('HTTPParserError') - t.strictEqual(error.code, undefined) + t.strictEqual(error.code, 'UND_ERR_HTTP_PARSER') t.strictEqual(error.data, undefined) }) })