Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsdoc: add jsdoc for errors #3589

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 172 additions & 77 deletions lib/core/errors.js
Original file line number Diff line number Diff line change
@@ -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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = /** @type {string} */ 'UndiciError'
/** @type {string} */
name = 'UndiciError'

Nit: I prefer for jsdoc comments to be above the relevant line of code. This is better for readability imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer them above too. But compare it in the typescript playground.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will see the difference immediatly.

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, string|string[]>|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, string|string[]>|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, string|string[]>|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
}
}
Expand Down
Loading
Loading