Prepare IDE beta 6 with Turkish native dialogs and map errors
This commit is contained in:
1 parent
7724d65e92
commit
75afd01b4b
15 files changed
+265
-57
No files matched your search
@@ -183,7 +183,15 @@ function checksum16(buf: Uint8Array): number {
|
||||
return (h >>> 0) & 0xffff
|
||||
}
|
||||
|
||||
export class LoadoutError extends Error {}
|
||||
export type LoadoutErrorInfo =
|
||||
| { kind: 'invalid' | 'short' | 'corrupted' | 'malformed' }
|
||||
| { kind: 'version'; actual: number; expected: number }
|
||||
|
||||
export class LoadoutError extends Error {
|
||||
constructor(message: string, readonly info?: LoadoutErrorInfo) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
export interface Loadout<T> {
|
||||
decode(code: string): T
|
||||
@@ -202,7 +210,7 @@ export interface LoadoutSpec<T> {
|
||||
/** Noun for user-facing error messages, e.g. 'map code'. Default: 'code'. */
|
||||
noun?: string
|
||||
/** Error subclass to throw, so callers can `instanceof` their own type. */
|
||||
error?: new (message: string) => LoadoutError
|
||||
error?: new (message: string, info?: LoadoutErrorInfo) => LoadoutError
|
||||
}
|
||||
|
||||
const HEAD_BYTES = 3 // 8-bit version + 16-bit checksum
|
||||
@@ -239,7 +247,7 @@ export function createLoadout<T>(spec: LoadoutSpec<T>): Loadout<T> {
|
||||
const raw = cleaned.startsWith(spec.prefix) ? cleaned.slice(spec.prefix.length) : cleaned
|
||||
|
||||
if (!raw) {
|
||||
throw new Err(`That doesn't look like a ${noun}.`)
|
||||
throw new Err(`That doesn't look like a ${noun}.`, { kind: 'invalid' })
|
||||
}
|
||||
|
||||
let framed: Uint8Array
|
||||
@@ -247,11 +255,11 @@ export function createLoadout<T>(spec: LoadoutSpec<T>): Loadout<T> {
|
||||
try {
|
||||
framed = fromBase64Url(raw)
|
||||
} catch {
|
||||
throw new Err(`That doesn't look like a ${noun}.`)
|
||||
throw new Err(`That doesn't look like a ${noun}.`, { kind: 'invalid' })
|
||||
}
|
||||
|
||||
if (framed.length <= HEAD_BYTES) {
|
||||
throw new Err(`${Noun} is too short to be valid.`)
|
||||
throw new Err(`${Noun} is too short to be valid.`, { kind: 'short' })
|
||||
}
|
||||
|
||||
const head = new BitReader(framed.subarray(0, HEAD_BYTES))
|
||||
@@ -259,19 +267,21 @@ export function createLoadout<T>(spec: LoadoutSpec<T>): Loadout<T> {
|
||||
const storedSum = head.uint(16)
|
||||
|
||||
if (version !== spec.version) {
|
||||
throw new Err(`${Noun} is version ${version}; this build reads version ${spec.version}.`)
|
||||
throw new Err(`${Noun} is version ${version}; this build reads version ${spec.version}.`, {
|
||||
kind: 'version', actual: version, expected: spec.version
|
||||
})
|
||||
}
|
||||
|
||||
const payload = framed.subarray(HEAD_BYTES)
|
||||
|
||||
if (checksum16(payload) !== storedSum) {
|
||||
throw new Err(`${Noun} looks corrupted (checksum mismatch).`)
|
||||
throw new Err(`${Noun} looks corrupted (checksum mismatch).`, { kind: 'corrupted' })
|
||||
}
|
||||
|
||||
try {
|
||||
return spec.read(new BitReader(inflateSync(payload)))
|
||||
} catch (err) {
|
||||
throw new Err(err instanceof Error ? `${Noun} is malformed: ${err.message}` : `${Noun} is malformed.`)
|
||||
throw new Err(err instanceof Error ? `${Noun} is malformed: ${err.message}` : `${Noun} is malformed.`, { kind: 'malformed' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user