Skip to content

Commit be33ae5

Browse files
committed
Removed usage of an easier fetch due bad error reporting
1 parent 2f800a9 commit be33ae5

File tree

12 files changed

+140
-142
lines changed

12 files changed

+140
-142
lines changed

docs/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-PO8erqJO.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-PO8erqJO.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/interpreter/_utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fetch from '@webreflection/fetch';
2-
31
import { absoluteURL, all, entries, importCSS, importJS, isArray, isCSS } from '../utils.js';
42

53
export const RUNNING_IN_WORKER = !globalThis.window;
@@ -91,8 +89,10 @@ const joinPaths = (parts) => {
9189
return parts[0].startsWith('/') ? `/${res}` : res;
9290
};
9391

94-
const fetchBuffer = (url, baseURL) =>
95-
fetch(absoluteURL(url, baseURL)).arrayBuffer();
92+
const fetchBuffer = (url, baseURL) => {
93+
const absolute = absoluteURL(url, baseURL);
94+
return fetch(absolute).then(r => r.ok ? r.arrayBuffer() : Promise.reject(new Error(`Unable to fetch ${absolute}`)));
95+
};
9696

9797
export const fetchPaths = (module, interpreter, config_fetch, baseURL) =>
9898
all(

esm/interpreter/micropython.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fetch from '@webreflection/fetch';
2-
31
import { createProgress, writeFile } from './_utils.js';
42
import { getFormat, loader, loadProgress, registerJSModule, run, runAsync, runEvent } from './_python.js';
53
import { stdio, buffered } from './_io.js';
@@ -122,7 +120,7 @@ async function importPackages(interpreter, baseURL, packages) {
122120
for (const mpyPackage of packages) {
123121
if (mpyPackage.endsWith('.whl')) {
124122
const url = absoluteURL(mpyPackage, baseURL);
125-
const buffer = await fetch(url).arrayBuffer();
123+
const buffer = await fetch(url).then(r => r.ok ? r.arrayBuffer() : Promise.reject(new Error(`Unable to fetch ${url}`)));
126124
await this.writeFile(interpreter, './*', buffer, url);
127125
}
128126
else {

esm/interpreter/ruby-wasm-wasi.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fetch from '@webreflection/fetch';
2-
31
import { dedent } from '../utils.js';
42
import { fetchFiles, fetchJSModules, fetchPaths } from './_utils.js';
53

@@ -20,7 +18,7 @@ export default {
2018
`https://cdn.jsdelivr.net/npm/@ruby/3.2-wasm-wasi@${version}/dist/browser/+esm`,
2119
async engine({ DefaultRubyVM }, config, url, baseURL) {
2220
url = url.replace(/\/browser\/\+esm$/, '/ruby.wasm');
23-
const buffer = await fetch(url).arrayBuffer();
21+
const buffer = await fetch(url).then(r => r.ok ? r.arrayBuffer() : Promise.reject(new Error(`Unable to fetch ${url}`)));
2422
const module = await WebAssembly.compile(buffer);
2523
const { vm: interpreter } = await DefaultRubyVM(module);
2624
if (config.files) await fetchFiles(this, interpreter, config.files, baseURL);

esm/loader.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fetch from '@webreflection/fetch';
2-
31
import { interpreter } from './interpreters.js';
42
import { absoluteURL, resolve } from './utils.js';
53
import { toml } from './3rd-party.js';
@@ -17,12 +15,23 @@ export const getConfigURLAndType = (config, configURL = './config.txt') => {
1715
return [absoluteURL(config), type];
1816
};
1917

18+
const onFetchError = absolute => () => {
19+
throw new Error(`Unable to fetch cofig ${absolute}`);
20+
};
21+
2022
export const resolveConfig = (config, configURL, options = {}) => {
2123
const [absolute, type] = getConfigURLAndType(config, configURL);
24+
const onError = onFetchError(absolute);
2225
if (type === 'json') {
23-
options = fetch(absolute).json();
26+
options = fetch(absolute).then(
27+
r => r.ok ? r.json() : onError(),
28+
onError
29+
);
2430
} else if (type === 'toml') {
25-
options = fetch(absolute).text().then(toml);
31+
options = fetch(absolute).then(
32+
r => r.ok ? r.text().then(toml) : onError(),
33+
onError
34+
);
2635
} else if (type === 'string') {
2736
options = parseString(config);
2837
} else if (type === 'object' && config) {
@@ -38,8 +47,7 @@ const parseString = config => {
3847
try {
3948
return parse(config);
4049
}
41-
// eslint-disable-next-line no-unused-vars
42-
catch (_) {
50+
catch {
4351
return toml(config);
4452
}
4553
};

esm/script-handler.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fetch from '@webreflection/fetch';
21
import { $ } from 'basic-devtools';
32

43
import IDBMap from '@webreflection/idb-map';
@@ -181,7 +180,7 @@ export const handle = async (script) => {
181180
if (targetValue) targets.set(script, queryTarget(script, targetValue));
182181

183182
// start fetching external resources ASAP
184-
const source = src ? fetch(src).text() : script.textContent;
183+
const source = src ? fetch(src).then(r => r.ok ? r.text() : Promise.reject(new Error(`Unable to fetch ${src}`))) : script.textContent;
185184
details.queue = details.queue.then(() =>
186185
execute(script, source, details.XWorker, isAsync),
187186
);

esm/worker/class.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import withResolvers from '@webreflection/utils/with-resolvers';
2-
import fetch from '@webreflection/fetch';
32
import xworker from './xworker.js';
43
import { getConfigURLAndType } from '../loader.js';
54
import { assign, create, defineProperties, importCSS, importJS } from '../utils.js';
@@ -51,16 +50,17 @@ export default (...args) =>
5150

5251
const resolver = withResolvers();
5352

54-
let bootstrap = fetch(url)
55-
.text()
56-
.then(code => {
53+
let bootstrap = fetch(url).then(
54+
async r => {
55+
if (!r.ok) throw new Error(`Unable to fetch ${url}`);
56+
const code = await r.text();
5757
const hooks = isHook ? this.toJSON() : void 0;
5858
postMessage.call(worker, { options, config, code, hooks });
59-
})
60-
.then(() => {
61-
// boost postMessage performance
62-
bootstrap = { then: fn => fn() };
63-
});
59+
},
60+
).then(() => {
61+
// boost postMessage performance
62+
bootstrap = { then: fn => fn() };
63+
});
6464

6565
defineProperties(worker, {
6666
sync: { value: sync },

0 commit comments

Comments
 (0)