diff --git a/docs/api/Errors.md b/docs/api/Errors.md index 230cd69b77c..0cde2b32a4f 100644 --- a/docs/api/Errors.md +++ b/docs/api/Errors.md @@ -7,15 +7,15 @@ You can find all the error objects inside the `errors` key. const { errors } = require('undici') ``` -| Error | Error Codes | Description | -| -----------------------------|-----------------------------------|------------------------------------------------| -| `InvalidArgumentError` | `UND_ERR_INVALID_ARG` | passed an invalid argument. | -| `InvalidReturnValueError` | `UND_ERR_INVALID_RETURN_VALUE` | returned an invalid value. | -| `RequestAbortedError` | `UND_ERR_ABORTED` | the request has been aborted by the user | -| `ClientDestroyedError` | `UND_ERR_DESTROYED` | trying to use a destroyed client. | -| `ClientClosedError` | `UND_ERR_CLOSED` | trying to use a closed client. | -| `SocketError` | `UND_ERR_SOCKET` | there is an error with the socket. | -| `NotSupportedError` | `UND_ERR_NOT_SUPPORTED` | encountered unsupported functionality. | -| `ContentLengthMismatchError` | `UND_ERR_CONTENT_LENGTH_MISMATCH`| body does not match content-length header | -| `InformationalError` | `UND_ERR_INFO` | expected error with reason | -| `TrailerMismatchError` | `UND_ERR_TRAILER_MISMATCH` | trailers did not match specification | \ No newline at end of file +| Error | Error Codes | Description | +| ------------------------------------|---------------------------------------|---------------------------------------------------| +| `InvalidArgumentError` | `UND_ERR_INVALID_ARG` | passed an invalid argument. | +| `InvalidReturnValueError` | `UND_ERR_INVALID_RETURN_VALUE` | returned an invalid value. | +| `RequestAbortedError` | `UND_ERR_ABORTED` | the request has been aborted by the user | +| `ClientDestroyedError` | `UND_ERR_DESTROYED` | trying to use a destroyed client. | +| `ClientClosedError` | `UND_ERR_CLOSED` | trying to use a closed client. | +| `SocketError` | `UND_ERR_SOCKET` | there is an error with the socket. | +| `NotSupportedError` | `UND_ERR_NOT_SUPPORTED` | encountered unsupported functionality. | +| `RequestContentLengthMismatchError` | `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH`| request body does not match content-length header | +| `InformationalError` | `UND_ERR_INFO` | expected error with reason | +| `TrailerMismatchError` | `UND_ERR_TRAILER_MISMATCH` | trailers did not match specification | diff --git a/lib/client.js b/lib/client.js index 5e9b73d0a15..a389d400698 100644 --- a/lib/client.js +++ b/lib/client.js @@ -9,7 +9,7 @@ const util = require('./core/util') const Request = require('./core/request') const Dispatcher = require('./dispatcher') const { - ContentLengthMismatchError, + RequestContentLengthMismatchError, TrailerMismatchError, InvalidArgumentError, RequestAbortedError, @@ -1231,12 +1231,12 @@ function write (client, request) { if (request.contentLength !== null && request.contentLength !== contentLength) { if (client[kStrictContentLength]) { - request.onError(new ContentLengthMismatchError()) + request.onError(new RequestContentLengthMismatchError()) assert(request.aborted) return false } - process.emitWarning(new ContentLengthMismatchError()) + process.emitWarning(new RequestContentLengthMismatchError()) } const socket = client[kSocket] @@ -1342,11 +1342,11 @@ function write (client, request) { // We should defer writing chunks. if (contentLength !== null && bytesWritten + len > contentLength) { if (client[kStrictContentLength]) { - util.destroy(socket, new ContentLengthMismatchError()) + util.destroy(socket, new RequestContentLengthMismatchError()) return } - process.emitWarning(new ContentLengthMismatchError()) + process.emitWarning(new RequestContentLengthMismatchError()) } if (bytesWritten === 0) { @@ -1396,9 +1396,9 @@ function write (client, request) { if (!err && contentLength !== null && bytesWritten !== contentLength) { if (client[kStrictContentLength]) { - err = new ContentLengthMismatchError() + err = new RequestContentLengthMismatchError() } else { - process.emitWarning(new ContentLengthMismatchError()) + process.emitWarning(new RequestContentLengthMismatchError()) } } diff --git a/lib/core/errors.js b/lib/core/errors.js index 56460eccb8e..5db37fde05c 100644 --- a/lib/core/errors.js +++ b/lib/core/errors.js @@ -88,11 +88,11 @@ class InformationalError extends UndiciError { } } -class ContentLengthMismatchError extends UndiciError { +class RequestContentLengthMismatchError extends UndiciError { constructor (message) { super(message) - Error.captureStackTrace(this, ContentLengthMismatchError) - this.name = 'ContentLengthMismatchError' + Error.captureStackTrace(this, RequestContentLengthMismatchError) + this.name = 'RequestContentLengthMismatchError' this.message = message || 'Request body length does not match content-length header' this.code = 'UND_ERR_CONTENT_LENGTH_MISMATCH' } @@ -153,7 +153,7 @@ module.exports = { HeadersTimeoutError, HeadersOverflowError, BodyTimeoutError, - ContentLengthMismatchError, + RequestContentLengthMismatchError, ConnectTimeoutError, TrailerMismatchError, InvalidArgumentError, diff --git a/test/content-length.js b/test/content-length.js index f664cfaeb11..3329765d2e2 100644 --- a/test/content-length.js +++ b/test/content-length.js @@ -24,7 +24,7 @@ test('request invalid content-length', (t) => { }, body: 'asd' }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -35,7 +35,7 @@ test('request invalid content-length', (t) => { }, body: 'asdasdasdasdasdasda' }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -46,7 +46,7 @@ test('request invalid content-length', (t) => { }, body: Buffer.alloc(9) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -57,7 +57,7 @@ test('request invalid content-length', (t) => { }, body: Buffer.alloc(11) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -67,7 +67,7 @@ test('request invalid content-length', (t) => { 'content-length': 10 } }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -77,7 +77,7 @@ test('request invalid content-length', (t) => { 'content-length': 0 } }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -93,7 +93,7 @@ test('request invalid content-length', (t) => { } }) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -109,7 +109,7 @@ test('request invalid content-length', (t) => { } }) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) }) }) @@ -147,7 +147,7 @@ test('request streaming invalid content-length', (t) => { } }) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) client.request({ @@ -165,7 +165,7 @@ test('request streaming invalid content-length', (t) => { } }) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) }) }) @@ -196,7 +196,7 @@ test('request streaming data when content-length=0', (t) => { } }) }, (err, data) => { - t.ok(err instanceof errors.ContentLengthMismatchError) + t.ok(err instanceof errors.RequestContentLengthMismatchError) }) }) }) diff --git a/test/errors.js b/test/errors.js index a55e9f92d9a..a9850ad179e 100644 --- a/test/errors.js +++ b/test/errors.js @@ -21,7 +21,7 @@ const scenarios = [ createScenario(errors.InvalidReturnValueError, 'Invalid Return Value Error', 'InvalidReturnValueError', 'UND_ERR_INVALID_RETURN_VALUE'), createScenario(errors.RequestAbortedError, 'Request aborted', 'RequestAbortedError', 'UND_ERR_ABORTED'), createScenario(errors.InformationalError, 'Request information', 'InformationalError', 'UND_ERR_INFO'), - createScenario(errors.ContentLengthMismatchError, 'Request body length does not match content-length header', 'ContentLengthMismatchError', 'UND_ERR_CONTENT_LENGTH_MISMATCH'), + createScenario(errors.RequestContentLengthMismatchError, 'Request body length does not match content-length header', 'RequestContentLengthMismatchError', 'UND_ERR_CONTENT_LENGTH_MISMATCH'), createScenario(errors.TrailerMismatchError, 'Trailers does not match trailer header', 'TrailerMismatchError', 'UND_ERR_TRAILER_MISMATCH'), createScenario(errors.ClientDestroyedError, 'The client is destroyed', 'ClientDestroyedError', 'UND_ERR_DESTROYED'), createScenario(errors.ClientClosedError, 'The client is closed', 'ClientClosedError', 'UND_ERR_CLOSED'), diff --git a/test/types/errors.test-d.ts b/test/types/errors.test-d.ts index d0eebf7b462..2d7082ea781 100644 --- a/test/types/errors.test-d.ts +++ b/test/types/errors.test-d.ts @@ -33,10 +33,10 @@ expectAssignable(new errors.InformationalError()) expectAssignable<'InformationalError'>(new errors.InformationalError().name) expectAssignable<'UND_ERR_INFO'>(new errors.InformationalError().code) -expectAssignable(new errors.ContentLengthMismatchError()) -expectAssignable(new errors.ContentLengthMismatchError()) -expectAssignable<'ContentLengthMismatchError'>(new errors.ContentLengthMismatchError().name) -expectAssignable<'UND_ERR_CONTENT_LENGTH_MISMATCH'>(new errors.ContentLengthMismatchError().code) +expectAssignable(new errors.RequestContentLengthMismatchError()) +expectAssignable(new errors.RequestContentLengthMismatchError()) +expectAssignable<'RequestContentLengthMismatchError'>(new errors.RequestContentLengthMismatchError().name) +expectAssignable<'UND_ERR_CONTENT_LENGTH_MISMATCH'>(new errors.RequestContentLengthMismatchError().code) expectAssignable(new errors.ClientDestroyedError()) expectAssignable(new errors.ClientDestroyedError()) diff --git a/types/errors.d.ts b/types/errors.d.ts index 1d9fc3a16eb..705796dc1b2 100644 --- a/types/errors.d.ts +++ b/types/errors.d.ts @@ -46,8 +46,8 @@ declare namespace Errors { } /** Body does not match content-length header. */ - export class ContentLengthMismatchError extends UndiciError { - name: 'ContentLengthMismatchError'; + export class RequestContentLengthMismatchError extends UndiciError { + name: 'RequestContentLengthMismatchError'; code: 'UND_ERR_CONTENT_LENGTH_MISMATCH'; }