-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
fix: support throwing Response.error()
#563
Merged
Merged
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -4,7 +4,10 @@ import { XMLHttpRequestEmitter } from '.' | |
import { toInteractiveRequest } from '../../utils/toInteractiveRequest' | ||
import { emitAsync } from '../../utils/emitAsync' | ||
import { XMLHttpRequestController } from './XMLHttpRequestController' | ||
import { createServerErrorResponse } from '../../utils/responseUtils' | ||
import { | ||
createServerErrorResponse, | ||
isResponseError, | ||
} from '../../utils/responseUtils' | ||
|
||
export interface XMLHttpRequestProxyOptions { | ||
emitter: XMLHttpRequestEmitter | ||
|
@@ -97,7 +100,12 @@ export function createXMLHttpRequestProxy({ | |
|
||
// Treat thrown Responses as mocked responses. | ||
if (resolverResult.error instanceof Response) { | ||
this.respondWith(resolverResult.error) | ||
if (isResponseError(resolverResult.error)) { | ||
xhrRequestController.errorWith(new TypeError('Network error')) | ||
} else { | ||
this.respondWith(resolverResult.error) | ||
} | ||
|
||
return | ||
} | ||
// Unhandled exceptions in the request listeners are | ||
|
@@ -119,7 +127,7 @@ export function createXMLHttpRequestProxy({ | |
mockedResponse.statusText | ||
) | ||
|
||
if (mockedResponse.type === 'error') { | ||
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. Oops! Inaccessible way to check for |
||
if (isResponseError(mockedResponse)) { | ||
this.logger.info( | ||
'received a network error response, rejecting the request promise...' | ||
) | ||
|
This file contains 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 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 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,6 +1,8 @@ | ||
/** | ||
* Determines if a given value is an instance of object. | ||
*/ | ||
export function isObject<T>(value: any): value is T { | ||
return Object.prototype.toString.call(value) === '[object Object]' | ||
export function isObject<T>(value: any, loose = false): value is T { | ||
return loose | ||
? Object.prototype.toString.call(value).startsWith('[object ') | ||
: Object.prototype.toString.call(value) === '[object Object]' | ||
} |
This file contains 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 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 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
File renamed without changes.
This file contains 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,32 +1,63 @@ | ||
import { it, expect, beforeAll, afterAll } from 'vitest' | ||
import http from 'http' | ||
import { vi, it, expect, beforeAll, afterAll, afterEach } from 'vitest' | ||
import http from 'node:http' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
import { DeferredPromise } from '@open-draft/deferred-promise' | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
interceptor.on('request', ({ request }) => { | ||
request.respondWith(Response.error()) | ||
}) | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('treats "Response.error()" as a request error', async () => { | ||
const requestErrorPromise = new DeferredPromise<Error>() | ||
it('treats "Response.error()" as a network error', async () => { | ||
const requestErrorListener = vi.fn() | ||
const responseListener = vi.fn() | ||
|
||
interceptor.on('request', ({ request }) => { | ||
request.respondWith(Response.error()) | ||
}) | ||
interceptor.on('response', responseListener) | ||
|
||
const request = http.get('http://localhost:3001/resource') | ||
request.on('error', requestErrorListener) | ||
|
||
// Must handle Response.error() as a network error. | ||
await vi.waitFor(() => { | ||
expect(requestErrorListener).toHaveBeenNthCalledWith( | ||
1, | ||
new TypeError('Network error') | ||
) | ||
}) | ||
|
||
expect(responseListener).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('treats a thrown Response.error() as a network error', async () => { | ||
const requestErrorListener = vi.fn() | ||
const responseListener = vi.fn() | ||
|
||
interceptor.on('request', () => { | ||
throw Response.error() | ||
}) | ||
interceptor.on('response', responseListener) | ||
|
||
const request = http.get('http://localhost:3001/resource') | ||
request.on('error', requestErrorListener) | ||
|
||
// In ClientRequest, network errors are forwarded as | ||
// request errors. | ||
request.on('error', requestErrorPromise.resolve) | ||
const requestError = await requestErrorPromise | ||
// Must handle Response.error() as a request error. | ||
await vi.waitFor(() => { | ||
expect(requestErrorListener).toHaveBeenNthCalledWith( | ||
1, | ||
new TypeError('Network error') | ||
) | ||
}) | ||
|
||
expect(requestError).toBeInstanceOf(TypeError) | ||
expect(requestError.message).toBe('Network error') | ||
expect(responseListener).not.toHaveBeenCalled() | ||
}) |
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.
This is the only difference with the
RequestError
. Response.error supports no error message and so it's translated to a genericNetwork error
error.