Deupe decodeU8a result format on failure (#5076)

This commit is contained in:
Jaco
2022-07-15 20:50:18 +03:00
committed by GitHub
parent c847920f86
commit c01a6001fc
+6 -10
View File
@@ -6,7 +6,7 @@ import type { Codec, CodecClass, Registry } from '../types';
import { u8aToHex } from '@polkadot/util';
/** @internal */
function formatFailure (registry: Registry, { message }: Error, u8a: Uint8Array, i: number, count: number, Type: CodecClass, key?: string): string {
function formatFailure (registry: Registry, result: unknown[], { message }: Error, u8a: Uint8Array, i: number, count: number, Type: CodecClass, key?: string): string {
let type = '';
try {
@@ -15,6 +15,8 @@ function formatFailure (registry: Registry, { message }: Error, u8a: Uint8Array,
// ignore
}
// console.error(JSON.stringify(result, null, 2));
return `decodeU8a: failed at ${u8aToHex(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i}/${count})${type}:: ${message}`;
}
@@ -42,9 +44,7 @@ export function decodeU8a <T extends Codec = Codec> (registry: Registry, result:
i++;
}
} catch (error) {
// console.error(JSON.stringify(result, null, 2));
throw new Error(formatFailure(registry, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
throw new Error(formatFailure(registry, result, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
}
return [result, offset];
@@ -70,9 +70,7 @@ export function decodeU8aStruct (registry: Registry, result: [string, Codec][],
i++;
}
} catch (error) {
// console.error(JSON.stringify(result, null, 2));
throw new Error(formatFailure(registry, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
throw new Error(formatFailure(registry, result, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
}
return [result, offset];
@@ -98,9 +96,7 @@ export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, resu
i++;
}
} catch (error) {
console.error(JSON.stringify(result, null, 2));
throw new Error(formatFailure(registry, error as Error, u8a.subarray(offset), i, count, Type));
throw new Error(formatFailure(registry, result, error as Error, u8a.subarray(offset), i, count, Type));
}
return [offset, offset - startAt];