-
Notifications
You must be signed in to change notification settings - Fork 8
Throw WebServiceError instances and preserve error causes #1902
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { ArgumentError, 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'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ArgumentError', () => { | ||
| it('is an Error instance', () => { | ||
| const err = new ArgumentError('bad input'); | ||
|
|
||
| expect(err).toBeInstanceOf(Error); | ||
| expect(err.name).toBe('ArgumentError'); | ||
| expect(err.message).toBe('bad input'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,68 @@ | ||
| import { WebServiceClientError } from './types.js'; | ||
|
|
||
| /* tslint:disable:max-classes-per-file */ | ||
| export class ArgumentError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = this.constructor.name; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An error returned by the minFraud 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; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| // 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'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Expose structured fields on
ArgumentErrorbefore exporting it publicly.src/index.tsnow re-exports this class, but it still only exposesnameandmessage. That leaves library consumers without thecode/errorcontract this file is supposed to provide for errors.Proposed fix
export class ArgumentError extends Error { + public readonly code: string; + public readonly error: string; + constructor(message: string) { super(message); this.name = this.constructor.name; + this.code = 'INVALID_ARGUMENT'; + this.error = message; } }As per coding guidelines,
src/errors.ts: Ensure error objects includecode,error, and optionalurlproperties for proper error handling by library consumers.📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines