Use logger in error/warning messages (#2790)

* Use logger in error/warning messages

* Update packages/types/src/codec/BTreeSet.ts

* Update expectation

* Some additional type tests
This commit is contained in:
Jaco Greeff
2020-10-27 13:14:03 +01:00
committed by GitHub
parent be1deb1a75
commit bb7b94dc35
12 changed files with 78 additions and 21 deletions
@@ -3,9 +3,13 @@
import { Registry } from '@polkadot/types/types';
import { logger } from '@polkadot/util';
import extractTypes from './extractTypes';
import flattenUniq from './flattenUniq';
const l = logger('metadata');
/** @internal */
export default function validateTypes (registry: Registry, types: string[], throwError: boolean): void {
const missing = flattenUniq(extractTypes(types)).filter((type) => !registry.hasType(type));
@@ -16,7 +20,7 @@ export default function validateTypes (registry: Registry, types: string[], thro
if (throwError) {
throw new Error(message);
} else {
console.warn(message);
l.warn(message);
}
}
}
+2 -2
View File
@@ -388,7 +388,7 @@ export default class Rpc implements RpcInterface {
try {
return this._formatStorageData(registry, key, result);
} catch (error) {
console.error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:`, (error as Error).message);
l.error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:`, (error as Error).message);
throw error;
}
@@ -459,7 +459,7 @@ export default class Rpc implements RpcInterface {
try {
results.push(this._formatStorageSetEntry(registry, key, changes, withCache));
} catch (error) {
console.error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:`, (error as Error).message);
l.error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:`, (error as Error).message);
throw error;
}
+4 -2
View File
@@ -4,12 +4,14 @@
import { H256 } from '../interfaces/runtime';
import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types';
import { isHex, hexToU8a, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { isHex, hexToU8a, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import Compact from './Compact';
import Raw from './Raw';
import { compareSet, decodeU8a, typeToConstructor } from './utils';
const l = logger('BTreeSet');
/** @internal */
function decodeSetFromU8a<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): Set<V> {
const output = new Set<V>();
@@ -37,7 +39,7 @@ function decodeSetFromSet<V extends Codec = Codec> (registry: Registry, ValClass
try {
output.add((val instanceof ValClass) ? val : new ValClass(registry, val));
} catch (error) {
console.error('Failed to decode BTreeSet key or value:', (error as Error).message);
l.error('Failed to decode key or value:', (error as Error).message);
throw error;
}
+4 -2
View File
@@ -4,7 +4,7 @@
import { H256 } from '../interfaces/runtime';
import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types';
import { isHex, hexToU8a, isObject, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { isHex, hexToU8a, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import Compact from './Compact';
import Raw from './Raw';
@@ -12,6 +12,8 @@ import compareMap from './utils/compareMap';
import decodeU8a from './utils/decodeU8a';
import typeToConstructor from './utils/typeToConstructor';
const l = logger('Map');
/** @internal */
function decodeMapFromU8a<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): Map<K, V> {
const output = new Map<K, V>();
@@ -46,7 +48,7 @@ function decodeMapFromMap<K extends Codec = Codec, V extends Codec = Codec> (reg
: new ValClass(registry, val)
);
} catch (error) {
console.error('Failed to decode Map key or value:', (error as Error).message);
l.error('Failed to decode key or value:', (error as Error).message);
throw error;
}
+4 -2
View File
@@ -3,7 +3,7 @@
import { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { u8aToU8a, assert } from '@polkadot/util';
import { assert, logger, u8aToU8a } from '@polkadot/util';
import Compact from './Compact';
import { decodeU8a, typeToConstructor } from './utils';
@@ -11,6 +11,8 @@ import AbstractArray from './AbstractArray';
const MAX_LENGTH = 64 * 1024;
const l = logger('Vec');
/**
* @name Vec
* @description
@@ -39,7 +41,7 @@ export default class Vec<T extends Codec> extends AbstractArray<T> {
? entry
: new Type(registry, entry);
} catch (error) {
console.error(`Unable to decode Vec on index ${index}`, (error as Error).message);
l.error(`Unable to decode on index ${index}`, (error as Error).message);
throw error;
}
@@ -32,6 +32,10 @@ describe('getTypeClass', (): void => {
getTypeClass(registry, typeDef);
expect(spy).toHaveBeenCalledWith('Unable to resolve type ABC, it will fail on construction');
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
'Unable to resolve type ABC, it will fail on construction'
);
});
});
+4 -2
View File
@@ -4,10 +4,12 @@
import { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { FromReg } from './types';
import { isU8a, u8aToHex } from '@polkadot/util';
import { isU8a, logger, u8aToHex } from '@polkadot/util';
import { createClass } from './createClass';
const l = logger('registry');
function u8aHasValue (value: Uint8Array): boolean {
return value.some((v): boolean => !!v);
}
@@ -35,7 +37,7 @@ function checkInstance<T extends Codec = Codec, K extends string = string> (valu
// if the hex doesn't match and the value for both is non-empty, complain... bitterly
if (!isEqual && (u8aHasValue(value) || u8aHasValue(created.toU8a(true)))) {
console.warn(`${rawType}:: Input doesn't match output, received ${u8aToHex(value)}, created ${crHex}`);
l.warn(`${rawType}:: Input doesn't match output, received ${u8aToHex(value)}, created ${crHex}`);
}
}
+1 -5
View File
@@ -171,11 +171,7 @@ export function getTypeDef (_type: string, { displayName, name }: TypeDefOptions
const type = sanitize(_type);
const value: TypeDef = { displayName, info: TypeDefInfo.Plain, name, type };
if (++count === MAX_NESTED) {
console.warn('getTypeDef: Maximum nested limit reached');
return value;
}
assert(++count !== MAX_NESTED, 'getTypeDef: Maximum nested limit reached');
const nested = nestedExtraction.find((nested) => hasWrapper(type, nested));
+6 -4
View File
@@ -7,7 +7,7 @@ import { ChainProperties, DispatchErrorModule } from '../interfaces/types';
import { CallFunction, Codec, Constructor, InterfaceTypes, RegistryError, RegistryTypes, Registry, RegistryMetadata, RegisteredTypes, TypeDef } from '../types';
import extrinsicsFromMeta from '@polkadot/metadata/Decorated/extrinsics/fromMetadata';
import { BN_ZERO, assert, formatBalance, isFunction, isString, isU8a, isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util';
import { BN_ZERO, assert, formatBalance, isFunction, isString, isU8a, isUndefined, logger, stringCamelCase, u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Raw from '../codec/Raw';
@@ -18,6 +18,8 @@ import { createClass, getTypeClass } from './createClass';
import { createType } from './createType';
import { getTypeDef } from './getTypeDef';
const l = logger('registry');
// create error mapping from metadata
function decorateErrors (_: Registry, metadata: RegistryMetadata, metadataErrors: Record<string, RegistryError>): void {
const modules = metadata.asLatest.modules;
@@ -67,7 +69,7 @@ function decorateEvents (registry: Registry, metadata: RegistryMetadata, metadat
try {
Types = typeDef.map((typeDef): Constructor<Codec> => getTypeClass(registry, typeDef));
} catch (error) {
console.error(error);
l.error(error);
}
metadataEvents[u8aToHex(eventIndex)] = class extends EventData {
@@ -233,7 +235,7 @@ export class TypeRegistry implements Registry {
if (definition) {
BaseType = createClass(this, definition);
} else if (withUnknown) {
console.warn(`Unable to resolve type ${name}, it will fail on construction`);
l.warn(`Unable to resolve type ${name}, it will fail on construction`);
this.#unknownTypes.set(name, true);
BaseType = DoNotConstruct.with(name);
@@ -383,7 +385,7 @@ export class TypeRegistry implements Registry {
const unknown = findUnknownExtensions(this.#signedExtensions);
if (unknown.length) {
console.warn(`Unknown signed extensions ${unknown.join(', ')} found, treating them as no-effect`);
l.warn(`Unknown signed extensions ${unknown.join(', ')} found, treating them as no-effect`);
}
}
}
@@ -0,0 +1,21 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '../create';
import DoNotConstruct from './DoNotConstruct';
describe('DoNotConstruct', (): void => {
const registry = new TypeRegistry();
it('does not allow construction', (): void => {
expect(
() => new (DoNotConstruct.with())(registry)
).toThrow(/Cannot construct unknown type DoNotConstruct/);
});
it('does not allow construction (with Name)', (): void => {
expect(
() => new (DoNotConstruct.with('Something'))(registry)
).toThrow(/Cannot construct unknown type Something/);
});
});
+18
View File
@@ -18,4 +18,22 @@ describe('Null', (): void => {
it('compares against other (failed)', (): void => {
expect(new Null(registry).eq()).toBe(false);
});
it('has no hash', (): void => {
expect(
() => new Null(registry).hash
).toThrow();
});
it('isEmpty', (): void => {
expect(new Null(registry).isEmpty).toBe(true);
});
it('has an empty hex', (): void => {
expect(new Null(registry).toHex()).toEqual('0x');
});
it('has a Null type', (): void => {
expect(new Null(registry).toRawType()).toEqual('Null');
});
});
@@ -111,4 +111,8 @@ describe('Type', (): void => {
expect(decoded.encodedLength).toEqual(original.length + 1); // extra byte for length
expect(decoded.toString()).toEqual(expected);
});
it('has the correct raw', (): void => {
expect(new Type(registry).toRawType()).toEqual('Type');
});
});