Skip to content

Commit

Permalink
refactor: make request errors explicit
Browse files Browse the repository at this point in the history
Make request related errors explicit in name in case we want to
add errors on response side later.

Refs: #742
  • Loading branch information
ronag committed Apr 26, 2021
1 parent e8af211 commit bb76d01
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
24 changes: 12 additions & 12 deletions docs/api/Errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| 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 |
14 changes: 7 additions & 7 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const util = require('./core/util')
const Request = require('./core/request')
const Dispatcher = require('./dispatcher')
const {
ContentLengthMismatchError,
RequestContentLengthMismatchError,
TrailerMismatchError,
InvalidArgumentError,
RequestAbortedError,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/core/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -153,7 +153,7 @@ module.exports = {
HeadersTimeoutError,
HeadersOverflowError,
BodyTimeoutError,
ContentLengthMismatchError,
RequestContentLengthMismatchError,
ConnectTimeoutError,
TrailerMismatchError,
InvalidArgumentError,
Expand Down
22 changes: 11 additions & 11 deletions test/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -109,7 +109,7 @@ test('request invalid content-length', (t) => {
}
})
}, (err, data) => {
t.ok(err instanceof errors.ContentLengthMismatchError)
t.ok(err instanceof errors.RequestContentLengthMismatchError)
})
})
})
Expand Down Expand Up @@ -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({
Expand All @@ -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)
})
})
})
Expand Down Expand Up @@ -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)
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
8 changes: 4 additions & 4 deletions test/types/errors.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ expectAssignable<errors.InformationalError>(new errors.InformationalError())
expectAssignable<'InformationalError'>(new errors.InformationalError().name)
expectAssignable<'UND_ERR_INFO'>(new errors.InformationalError().code)

expectAssignable<errors.UndiciError>(new errors.ContentLengthMismatchError())
expectAssignable<errors.ContentLengthMismatchError>(new errors.ContentLengthMismatchError())
expectAssignable<'ContentLengthMismatchError'>(new errors.ContentLengthMismatchError().name)
expectAssignable<'UND_ERR_CONTENT_LENGTH_MISMATCH'>(new errors.ContentLengthMismatchError().code)
expectAssignable<errors.UndiciError>(new errors.RequestContentLengthMismatchError())
expectAssignable<errors.RequestContentLengthMismatchError>(new errors.RequestContentLengthMismatchError())
expectAssignable<'RequestContentLengthMismatchError'>(new errors.RequestContentLengthMismatchError().name)
expectAssignable<'UND_ERR_CONTENT_LENGTH_MISMATCH'>(new errors.RequestContentLengthMismatchError().code)

expectAssignable<errors.UndiciError>(new errors.ClientDestroyedError())
expectAssignable<errors.ClientDestroyedError>(new errors.ClientDestroyedError())
Expand Down
4 changes: 2 additions & 2 deletions types/errors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down

0 comments on commit bb76d01

Please sign in to comment.