Add error codes to our custom errors (#959)

This commit is contained in:
Fábio Rosado
2022-11-25 17:04:10 +00:00
committed by GitHub
parent 30e31a86ef
commit b062efcf17
20 changed files with 306 additions and 67 deletions

View File

@@ -2,7 +2,8 @@ import { htmlDecode, ensureUniqueId } from '../utils';
import type { Runtime } from '../runtime';
import { getLogger } from '../logger';
import { pyExec } from '../pyexec';
import { FetchError, _createAlertBanner } from '../exceptions';
import { _createAlertBanner } from '../exceptions';
import { robustFetch } from '../fetch';
const logger = getLogger('py-script');
@@ -18,17 +19,14 @@ export function make_PyScript(runtime: Runtime) {
async getPySrc(): Promise<string> {
if (this.hasAttribute('src')) {
const url = this.getAttribute('src');
const response = await fetch(url);
if (response.status !== 200) {
const errorMessage = (
`Failed to fetch '${url}' - Reason: ` +
`${response.status} ${response.statusText}`
);
_createAlertBanner(errorMessage);
try {
const response = await robustFetch(url);
return await response.text();
} catch(e) {
_createAlertBanner(e.message);
this.innerHTML = '';
throw new FetchError(errorMessage);
throw e
}
return await response.text();
} else {
return htmlDecode(this.innerHTML);
}