Handle catching unknowns

This commit is contained in:
Misode
2021-09-09 22:34:22 +02:00
parent fd503f1d03
commit e3fb5923c0
4 changed files with 20 additions and 12 deletions

View File

@@ -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<T = any>(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)}`)
}
}

View File

@@ -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<VersionData> {
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<DataMode
}
Models[id] = { model, version }
} catch (e) {
throw new Error(`Cannot get generator "${id}" for version "${version}": ${e.message}`)
throw new Error(`Cannot get generator "${id}" for version "${version}": ${message(e)}`)
}
}
return Models[id].model

View File

@@ -67,3 +67,8 @@ export function lerp3(a: number, b: number, c: number, d: number, e: number, f:
export function smoothstep(x: number): number {
return x * x * x * (x * (x * 6 - 15) + 10)
}
export function message(e: unknown): string {
if (e instanceof Error) return e.message
return `${e}`
}

View File

@@ -7,6 +7,7 @@ import { locale } from '../Locales'
import { transformOutput } from '../schema/transformOutput'
import type { BlockStateRegistry } from '../Schemas'
import { Store } from '../Store'
import { message } from '../Utils'
const INDENT: Record<string, number | string> = {
'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)}`)
}
}