mirror of
https://github.com/pyscript/pyscript.git
synced 2026-03-06 06:38:39 -05:00
Raise error if we get a non 200 status from fetch (#935)
* Raise error if we get a non 200 status from fetch * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add custom exception * Add check for TypeError as well Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -6,6 +6,13 @@ export class UserError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class FetchError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
this.name = "FetchError"
|
||||
}
|
||||
}
|
||||
|
||||
export function _createAlertBanner(message: string, level: "error" | "warning" = "error", logMessage = true) {
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
switch (`log-${level}-${logMessage}`) {
|
||||
|
||||
@@ -210,8 +210,13 @@ export class PyScriptApp {
|
||||
} catch (e) {
|
||||
// Remove the loader so users can see the banner better
|
||||
this.loader.remove()
|
||||
//Should we still export full error contents to console?
|
||||
handleFetchError(<Error>e, fetchPaths[i]);
|
||||
// The 'TypeError' here happens when running pytest
|
||||
// I'm not particularly happy with this solution.
|
||||
if (e.name === "FetchError" || e.name === "TypeError") {
|
||||
handleFetchError(<Error>e, fetchPaths[i]);
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.info('All paths fetched');
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Runtime } from './runtime';
|
||||
import { getLogger } from './logger';
|
||||
import { FetchError } from './exceptions'
|
||||
import type { loadPyodide as loadPyodideDeclaration, PyodideInterface, PyProxy } from 'pyodide';
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
@@ -110,6 +111,9 @@ export class PyodideRuntime extends Runtime {
|
||||
}
|
||||
}
|
||||
const response = await fetch(fetch_path);
|
||||
if (response.status !== 200) {
|
||||
throw new FetchError(`Unable to fetch ${fetch_path}, reason: ${response.status} - ${response.statusText}`);
|
||||
}
|
||||
const buffer = await response.arrayBuffer();
|
||||
const data = new Uint8Array(buffer);
|
||||
pathArr.push(filename);
|
||||
|
||||
Reference in New Issue
Block a user