Skip to content

Commit

Permalink
Improve cde-config error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed Apr 17, 2024
1 parent e5bf668 commit f1f4b90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
12 changes: 7 additions & 5 deletions packages/engine/paima-runtime/src/cde-config/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function loadChainDataExtensions(
);
return [instantiatedExtensions, true];
} catch (err) {
doLog(`[cde-config] Invalid config file: ${err}`);
doLog(`[cde-config] Invalid config file:`, err);
return [[], false];
}
}
Expand Down Expand Up @@ -177,16 +177,18 @@ function checkOrError<T extends TSchema>(
): Static<T> {
// 1) Check if there are any errors since Value.Decode doesn't give error messages
{
const skippableErrors: ValueErrorType[] = [ValueErrorType.Intersect, ValueErrorType.Union];
const lowPriorityErrors = new Set([ValueErrorType.Intersect, ValueErrorType.Union]);

const errors = Array.from(Value.Errors(structure, config));
const allErrorsLowPriority = errors.every(e => lowPriorityErrors.has(e.type));
for (const error of errors) {
// there are many useless errors in this library
// ex: 1st error: "foo" should be "bar" in struct Foo
// 2nd error: struct Foo is invalid inside struct Config
// in this case, the 2nd error is useless as we only care about the 1st error
// However, we always want to show the error if for some reason it's the only error
if (errors.length !== 1 && skippableErrors.find(val => val === error.type) != null) continue;
if (!allErrorsLowPriority && lowPriorityErrors.has(error.type))
continue;
console.error({
name: name ?? 'Configuration root',
path: error.path,
Expand Down Expand Up @@ -228,7 +230,7 @@ async function instantiateExtension(
contract: getErc20Contract(config.contractAddress, web3s[network]),
};
case CdeEntryTypeName.ERC721:
if (await isPaimaErc721(config, web3s[config.network || defaultEvmMainNetworkName])) {
if (await isPaimaErc721(config, web3s[network])) {
return {
...config,
network,
Expand Down Expand Up @@ -378,7 +380,7 @@ async function instantiateCdeGeneric(
eventSignatureHash,
};
} catch (err) {
doLog(`[cde-config]: Fail to initialize Web3 contract with ABI ${config.abiPath}`);
doLog(`[cde-config] Failed to initialize Web3 contract ${config.name} with ABI ${config.abiPath}`);
throw err;
}
}
10 changes: 5 additions & 5 deletions packages/engine/paima-runtime/src/cde-config/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export function getEarliestStartSlot(config: ChainDataExtension[]): number {
}

// returns pair [rawAbiFileData, artifactObject.abi]
export async function loadAbi(abiPath: string): Promise<any> {
let abiFileData: string = '';
export async function loadAbi(abiPath: string): Promise<any[]> {
let abiFileData: string;
try {
abiFileData = await fs.readFile(abiPath, 'utf8');
} catch (err) {
doLog(`[cde-config] ABI file not found: ${abiPath}`);
return [abiFileData, []];
return [];
}
try {
let abiJson = JSON.parse(abiFileData);
Expand All @@ -47,7 +47,7 @@ export async function loadAbi(abiPath: string): Promise<any> {
}
}
} catch (err) {
doLog(`[cde-config] ABI file at ${abiPath} has invalid structure`);
doLog(`[cde-config] ABI file at ${abiPath} has invalid structure`, err);
}
return [abiFileData, []];
return [];
}
2 changes: 1 addition & 1 deletion packages/paima-sdk/paima-utils/src/config/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export async function loadConfig(): Promise<Static<typeof BaseConfigWithDefaults

return config;
} catch (err) {
doLog(`Invalid config file: ${err}.`);
doLog(`Invalid config file:`, err);
return undefined;
}
}
Expand Down

0 comments on commit f1f4b90

Please sign in to comment.