-
Notifications
You must be signed in to change notification settings - Fork 24
Throw WebServiceError instances and preserve error causes #1725
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { WebServiceError } from './errors.js'; | ||
|
|
||
| describe('WebServiceError', () => { | ||
| it('is an Error instance', () => { | ||
| const err = new WebServiceError({ | ||
| code: 'FETCH_ERROR', | ||
| error: 'something went wrong', | ||
| url: 'https://example.com', | ||
| }); | ||
|
|
||
| expect(err).toBeInstanceOf(Error); | ||
| expect(err).toBeInstanceOf(WebServiceError); | ||
| expect(err.name).toBe('WebServiceError'); | ||
| }); | ||
|
|
||
| it('exposes code, error, status, and url', () => { | ||
| const err = new WebServiceError({ | ||
| code: 'SERVER_ERROR', | ||
| error: 'boom', | ||
| status: 500, | ||
| url: 'https://example.com', | ||
| }); | ||
|
|
||
| expect(err.code).toBe('SERVER_ERROR'); | ||
| expect(err.error).toBe('boom'); | ||
| expect(err.status).toBe(500); | ||
| expect(err.url).toBe('https://example.com'); | ||
| }); | ||
|
|
||
| it('uses the error string as the message', () => { | ||
| const err = new WebServiceError({ | ||
| code: 'FETCH_ERROR', | ||
| error: 'the message', | ||
| url: 'https://example.com', | ||
| }); | ||
|
|
||
| expect(err.message).toBe('the message'); | ||
| expect(err.error).toBe(err.message); | ||
| }); | ||
|
|
||
| it('preserves the underlying cause', () => { | ||
| const cause = new TypeError('fetch failed'); | ||
| const err = new WebServiceError( | ||
| { | ||
| code: 'FETCH_ERROR', | ||
| error: 'TypeError - fetch failed', | ||
| url: 'https://example.com', | ||
| }, | ||
| { cause } | ||
| ); | ||
|
|
||
| expect(err.cause).toBe(cause); | ||
| }); | ||
|
|
||
| it('leaves cause undefined when not provided', () => { | ||
| const err = new WebServiceError({ | ||
| code: 'FETCH_ERROR', | ||
| error: 'something went wrong', | ||
| url: 'https://example.com', | ||
| }); | ||
|
|
||
| expect(err.cause).toBeUndefined(); | ||
| expect(JSON.parse(JSON.stringify(err))).not.toHaveProperty('cause'); | ||
| }); | ||
|
|
||
| it('omits status when not provided', () => { | ||
| const err = new WebServiceError({ | ||
| code: 'FETCH_ERROR', | ||
| error: 'something went wrong', | ||
| url: 'https://example.com', | ||
| }); | ||
|
|
||
| expect(err.status).toBeUndefined(); | ||
| expect(Object.prototype.hasOwnProperty.call(err, 'status')).toBe(false); | ||
| }); | ||
|
|
||
| it('retains code, error, status, and url as enumerable properties', () => { | ||
| const err = new WebServiceError({ | ||
| code: 'SERVER_ERROR', | ||
| error: 'boom', | ||
| status: 500, | ||
| url: 'https://example.com', | ||
| }); | ||
|
|
||
| const serialized = JSON.parse(JSON.stringify(err)); | ||
| expect(serialized).toMatchObject({ | ||
| code: 'SERVER_ERROR', | ||
| error: 'boom', | ||
| status: 500, | ||
| url: 'https://example.com', | ||
| }); | ||
| // `name` lives on the prototype, so it is not serialized. | ||
| expect(serialized).not.toHaveProperty('name'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,66 @@ | ||
| import { WebServiceClientError } from './types.js'; | ||
|
|
||
| /* tslint:disable:max-classes-per-file */ | ||
|
|
||
| /** | ||
| * An error returned by the GeoIP2 web service or encountered while | ||
| * communicating with it. | ||
| * | ||
| * In addition to the standard `Error` properties (including `cause`, which | ||
| * holds the underlying error when one exists, such as the network error | ||
| * behind a `FETCH_ERROR`), it exposes the `code`, `status`, and `url` | ||
| * associated with the failure. | ||
| */ | ||
| export class WebServiceError extends Error implements WebServiceClientError { | ||
| /** | ||
| * The error code returned by the web service or generated by this client. | ||
| */ | ||
| public readonly code: string; | ||
| /** | ||
| * A human-readable description of the error. This is an alias of `message`, | ||
| * retained for backward compatibility. | ||
| */ | ||
| public readonly error: string; | ||
| /** | ||
| * The HTTP status code, when the error originated from an HTTP response. | ||
| * | ||
| * Declared with `declare` so that no class field is emitted: the property is | ||
| * absent (rather than set to `undefined`) when no status applies, e.g. on | ||
| * network-level errors such as `FETCH_ERROR` and `NETWORK_TIMEOUT`. | ||
| */ | ||
| declare public readonly status?: number; | ||
| /** | ||
| * The URL that was being requested when the error occurred. | ||
| */ | ||
| public readonly url: string; | ||
|
|
||
| constructor( | ||
| properties: { | ||
| code: string; | ||
| error: string; | ||
| status?: number; | ||
| url: string; | ||
| }, | ||
| options?: { cause?: unknown } | ||
| ) { | ||
| super(properties.error, options); | ||
| this.code = properties.code; | ||
| this.error = properties.error; | ||
| // Only assign `status` when present so it stays genuinely optional: on | ||
| // network-level errors (e.g. FETCH_ERROR, NETWORK_TIMEOUT) the instance | ||
| // carries no `status` property at all, rather than `status: undefined`. | ||
| if (properties.status !== undefined) { | ||
| this.status = properties.status; | ||
| } | ||
| this.url = properties.url; | ||
| } | ||
| } | ||
|
|
||
| // Set `name` on the prototype rather than in the constructor. Using a string | ||
| // literal keeps the name correct under minification, and keeping it off the | ||
| // instance means it stays out of `JSON.stringify` output. | ||
| WebServiceError.prototype.name = 'WebServiceError'; | ||
|
Comment on lines
+59
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
node <<'NODE'
class AssignedNameError extends Error {}
AssignedNameError.prototype.name = 'AssignedNameError';
class DefinedNameError extends Error {}
Object.defineProperty(DefinedNameError.prototype, 'name', {
value: 'DefinedNameError',
writable: true,
configurable: true,
});
console.log('Error.prototype.name:', Object.getOwnPropertyDescriptor(Error.prototype, 'name'));
console.log('AssignedNameError.prototype.name:', Object.getOwnPropertyDescriptor(AssignedNameError.prototype, 'name'));
console.log('DefinedNameError.prototype.name:', Object.getOwnPropertyDescriptor(DefinedNameError.prototype, 'name'));
NODERepository: maxmind/GeoIP2-node Length of output: 514 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the relevant file section with line numbers.
sed -n '1,140p' src/errors.ts | cat -nRepository: maxmind/GeoIP2-node Length of output: 3515 Make Direct assignment makes 🤖 Prompt for AI Agents |
||
|
|
||
| /** | ||
| * This error is thrown when the IP address is not found in the database. | ||
| * This generally means that the address was a private or reserved address. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.