-
Notifications
You must be signed in to change notification settings - Fork 860
Handle background fetch transport failures #1014
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
PeterDaveHello
merged 1 commit into
ChatGPTBox-dev:master
from
PeterDaveHello:fix-fetch-bg-rejection
Jul 19, 2026
+88
−0
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 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 |
|---|---|---|
|
|
@@ -27,5 +27,6 @@ export function fetchBg(input, init) { | |
| ) | ||
| } | ||
| }) | ||
| .catch(reject) | ||
| }) | ||
| } | ||
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,87 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { test } from 'node:test' | ||
| import Browser from 'webextension-polyfill' | ||
| import { fetchBg } from '../../../src/utils/fetch-bg.mjs' | ||
|
|
||
| function mockSendMessage(t, implementation) { | ||
| const originalSendMessage = Browser.runtime.sendMessage | ||
| Object.defineProperty(Browser.runtime, 'sendMessage', { | ||
| configurable: true, | ||
| value: implementation, | ||
| }) | ||
| t.after(() => { | ||
| Object.defineProperty(Browser.runtime, 'sendMessage', { | ||
| configurable: true, | ||
| value: originalSendMessage, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| test('fetchBg returns the response received from the background script', async (t) => { | ||
| const calls = [] | ||
| mockSendMessage(t, async (message) => { | ||
| calls.push(message) | ||
| return [ | ||
| { | ||
| body: 'response body', | ||
| status: 201, | ||
| statusText: 'Created', | ||
| headers: { 'content-type': 'text/plain' }, | ||
| }, | ||
| null, | ||
| ] | ||
| }) | ||
|
|
||
| const response = await fetchBg('https://example.com/resource', { method: 'POST' }) | ||
|
|
||
| assert.deepEqual(calls, [ | ||
| { | ||
| type: 'FETCH', | ||
| data: { | ||
| input: 'https://example.com/resource', | ||
| init: { method: 'POST' }, | ||
| }, | ||
| }, | ||
| ]) | ||
| assert.equal(response.status, 201) | ||
| assert.equal(response.statusText, 'Created') | ||
| assert.equal(response.headers.get('content-type'), 'text/plain') | ||
| assert.equal(await response.text(), 'response body') | ||
| }) | ||
|
|
||
| test('fetchBg rejects with the error received from the background script', async (t) => { | ||
| const backgroundError = { message: 'background fetch failed' } | ||
| mockSendMessage(t, async () => [null, backgroundError]) | ||
|
|
||
| await assert.rejects(fetchBg('https://example.com'), (error) => { | ||
| assert.deepEqual(error, backgroundError) | ||
| return true | ||
| }) | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| }) | ||
|
|
||
| test('fetchBg forwards sendMessage rejections', async (t) => { | ||
| const messageError = new Error('message channel closed') | ||
| mockSendMessage(t, async () => { | ||
| throw messageError | ||
| }) | ||
|
|
||
| await assert.rejects(fetchBg('https://example.com'), (error) => { | ||
| assert.equal(error, messageError) | ||
| return true | ||
| }) | ||
| }) | ||
|
|
||
| test('fetchBg forwards errors thrown while processing the response', async (t) => { | ||
| const processingError = new Error('invalid message response') | ||
| const messageResponse = { | ||
| [Symbol.iterator]() { | ||
| throw processingError | ||
| }, | ||
| } | ||
| mockSendMessage(t, async () => messageResponse) | ||
|
|
||
| await assert.rejects(fetchBg('https://example.com'), (error) => { | ||
| assert.equal(error, processingError) | ||
| return true | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.