From 4e4fa49b0b1fd6ec5a018e742eb9910aa32e7637 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 28 May 2022 21:55:37 -0400 Subject: Audio request errors (#2161) * Generalize _onBeforeSendHeadersAddListener * Simplify filter assignment * Use requestId rather than done * Properly support Firefox addListener without arguments * Add details to fetchAnonymous errors * Refactor * Enable support for no header modifications * Update MV3 support for error details * Expose errors in downloadTermAudio * Throw an error if audio download fails due to potential permissions reasons --- ext/js/background/backend.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'ext/js/background/backend.js') diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index b25b6033..c0f286f8 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1803,6 +1803,8 @@ class Backend { reading )); } catch (e) { + const error = this._getAudioDownloadError(e); + if (error !== null) { throw error; } // No audio return null; } @@ -1894,6 +1896,28 @@ class Backend { return {results, errors}; } + _getAudioDownloadError(error) { + if (isObject(error.data)) { + const {errors} = error.data; + if (Array.isArray(errors)) { + for (const error2 of errors) { + if (!isObject(error2.data)) { continue; } + const {details} = error2.data; + if (!isObject(details)) { continue; } + if (details.error === 'net::ERR_FAILED') { + // This is potentially an error due to the extension not having enough URL privileges. + // The message logged to the console looks like this: + // Access to fetch at '' from origin 'chrome-extension://' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. + const result = new Error('Audio download failed due to possible extension permissions error'); + result.data = {errors}; + return result; + } + } + } + } + return null; + } + _generateAnkiNoteMediaFileName(prefix, extension, timestamp, definitionDetails) { let fileName = prefix; -- cgit v1.2.3