From e3fb5923c0a93979bc580362f32c9fe28b9b4367 Mon Sep 17 00:00:00 2001 From: Misode Date: Thu, 9 Sep 2021 22:34:22 +0200 Subject: [PATCH] Handle catching unknowns --- src/app/DataFetcher.ts | 17 +++++++++-------- src/app/Schemas.ts | 5 +++-- src/app/Utils.ts | 5 +++++ src/app/components/SourcePanel.tsx | 5 +++-- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/app/DataFetcher.ts b/src/app/DataFetcher.ts index 484183dd..d8a7d3d0 100644 --- a/src/app/DataFetcher.ts +++ b/src/app/DataFetcher.ts @@ -2,6 +2,7 @@ import type { CollectionRegistry } from '@mcschema/core' import config from '../config.json' import type { BlockStateRegistry, VersionId } from './Schemas' import { checkVersion } from './Schemas' +import { message } from './Utils' ['1.15', '1.16', '1.17'].forEach(v => localStorage.removeItem(`cache_${v}`)) @@ -85,7 +86,7 @@ async function fetchRegistries(version: Version, target: CollectionRegistry) { target.register(r.id, data[r.id] ?? []) }) } catch (e) { - console.warn('Error occurred while fetching registries:', e) + console.warn('Error occurred while fetching registries:', message(e)) } } else { await Promise.all(registries.map(async r => { @@ -95,7 +96,7 @@ async function fetchRegistries(version: Version, target: CollectionRegistry) { : `${mcdataUrl}/${version.refs.mcdata_master}/processed/reports/registries/${r.id}/data.min.json` target.register(r.id, await getData(url, v => v.values)) } catch (e) { - console.warn(`Error occurred while fetching registry ${r.id}:`, e) + console.warn(`Error occurred while fetching registry ${r.id}:`, message(e)) } })) } @@ -121,7 +122,7 @@ async function fetchBlockStateMap(version: Version, target: BlockStateRegistry) }) Object.assign(target, data) } catch (e) { - console.warn('Error occurred while fetching block state map:', e) + console.warn('Error occurred while fetching block state map:', message(e)) } } else { const url = `${mcdataUrl}/${version.refs.mcdata_master}/processed/reports/blocks/simplified/data.min.json` @@ -129,7 +130,7 @@ async function fetchBlockStateMap(version: Version, target: BlockStateRegistry) const data = await getData(url) Object.assign(target, data) } catch (e) { - console.warn('Error occurred while fetching block state map:', e) + console.warn('Error occurred while fetching block state map:', message(e)) } } } @@ -148,7 +149,7 @@ async function fetchDynamicRegistries(version: Version, target: CollectionRegist target.register(r.id, data[r.id]) }) } catch (e) { - console.warn('Error occurred while fetching dynamic registries:', e) + console.warn('Error occurred while fetching dynamic registries:', message(e)) } } } @@ -160,7 +161,7 @@ export async function fetchPreset(version: VersionId, registry: string, id: stri const res = await fetch(`${vanillaDatapackUrl}/${versionData.refs.vanilla_datapack_data}/data/minecraft/${registry}/${id}.json`) return await res.json() } catch (e) { - console.warn(`Error occurred while fetching ${registry} preset ${id}:`, e) + console.warn(`Error occurred while fetching ${registry} preset ${id}:`, message(e)) } } @@ -181,7 +182,7 @@ async function getData(url: string, fn: (v: any) => T = (v: any) => v): await cache.put(url, new Response(JSON.stringify(responseData))) return responseData } catch (e) { - console.warn(`[getData] Failed to open cache ${CACHE_NAME}: ${e.message}`) + console.warn(`[getData] Failed to open cache ${CACHE_NAME}: ${message(e)}`) console.debug(`[getData] fetching data ${url}`) const fetchResponse = await fetch(url) @@ -204,6 +205,6 @@ async function deleteMatching(matches: (url: string) => boolean) { console.debug(`[deleteMatching] Removing ${promises.length} cache objects...`) await Promise.all(promises) } catch (e) { - console.warn(`[deleteMatching] Failed to open cache ${CACHE_NAME}: ${e.message}`) + console.warn(`[deleteMatching] Failed to open cache ${CACHE_NAME}: ${message(e)}`) } } diff --git a/src/app/Schemas.ts b/src/app/Schemas.ts index 258b1cf5..51988f21 100644 --- a/src/app/Schemas.ts +++ b/src/app/Schemas.ts @@ -5,6 +5,7 @@ import * as java16 from '@mcschema/java-1.16' import * as java17 from '@mcschema/java-1.17' import config from '../config.json' import { fetchData } from './DataFetcher' +import { message } from './Utils' export const VersionIds = ['1.15', '1.16', '1.17'] as const export type VersionId = typeof VersionIds[number] @@ -55,7 +56,7 @@ async function getVersion(id: VersionId): Promise { Versions[id] = { collections, schemas, blockStates } return Versions[id] } catch (e) { - throw new Error(`Cannot get version "${id}": ${e.message}`) + throw new Error(`Cannot get version "${id}": ${message(e)}`) } })() return Versions[id] @@ -81,7 +82,7 @@ export async function getModel(version: VersionId, id: string): Promise = { '2_spaces': 2, @@ -39,7 +40,7 @@ export function SourcePanel({ lang, name, model, blockStates, doCopy, doDownload const data = model.schema.hook(transformOutput, new ModelPath(model), model.data, props) source.current.value = JSON.stringify(data, null, INDENT[indent]) + '\n' } catch (e) { - onError(`Error getting JSON output: ${e.message}`) + onError(`Error getting JSON output: ${message(e)}`) console.error(e) source.current.value = '' } @@ -62,7 +63,7 @@ export function SourcePanel({ lang, name, model, blockStates, doCopy, doDownload const data = JSON.parse(source.current.value) model?.reset(data, false) } catch (e) { - onError(`Error importing: ${e.message}`) + onError(`Error importing: ${message(e)}`) } }