Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logger to support errors with cause #2435

Merged
merged 12 commits into from
Jun 11, 2024
2 changes: 2 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Add missing networks for migration
- Fixed init command path issue
- Update common-ethereum to fix issue with ABI validation (#2435)

## [4.12.0] - 2024-06-05
### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@subql/common-algorand": "^3.4.0",
"@subql/common-concordium": "^3.6.0",
"@subql/common-cosmos": "^4.3.0",
"@subql/common-ethereum": "^3.6.0",
"@subql/common-ethereum": "^3.8.3",
"@subql/common-flare": "^3.6.0",
"@subql/common-near": "^3.5.0",
"@subql/common-stellar": "^3.5.0",
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/src/controller/init-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ describe('Cli can create project (mocked)', () => {
expect(projectPackage.author).toBe(project.author);

const updatedManifest = await fs.promises.readFile(`${projectPath}/${DEFAULT_TS_MANIFEST}`);
const extractedValues = extractFromTs(updatedManifest.toString(), {
endpoint: ENDPOINT_REG,
});
expect(extractedValues.endpoint).toStrictEqual(project.endpoint);
expect(originalManifest).not.toBe(updatedManifest.toString());
expect(originalPackage).not.toBe(packageData.toString());
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/controller/init-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ import path from 'path';
const mode = process.env.NODE_ENV || 'production';

// Load the appropriate .env file
const dotenvPath = path.resolve(process.cwd(), \`.env\${mode !== 'production' ? \`.$\{mode}\` : ''}\`);
const dotenvPath = path.resolve(__dirname, \`.env\${mode !== 'production' ? \`.$\{mode}\` : ''}\`);
dotenv.config({ path: dotenvPath });
`;

Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/controller/publish-controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import fs from 'fs';
import path from 'path';
import {promisify} from 'util';
import {DEFAULT_MANIFEST, mapToObject, ReaderFactory, toJsonObject} from '@subql/common';
import {mapToObject, ReaderFactory, toJsonObject} from '@subql/common';
import {parseSubstrateProjectManifest} from '@subql/common-substrate';
import rimraf from 'rimraf';
import Publish from '../commands/publish';
import {createTestProject} from '../createProject.fixtures';
import {uploadToIpfs} from './publish-controller';

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/createProject.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import childProcess from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
import fetch from 'cross-fetch';
import Build from './commands/build';
import Codegen from './commands/codegen';
import {cloneProjectTemplate, ExampleProjectInterface, prepare} from './controller/init-controller';
Expand Down Expand Up @@ -53,6 +54,8 @@ export async function createTestProject(): Promise<string> {

// Install dependencies
childProcess.execSync(`npm i`, {cwd: projectDir});
// Set test env to be develop mode, only limit to test
process.env.NODE_ENV = 'develop';

await Codegen.run(['-l', projectDir]);
await Build.run(['-f', projectDir]);
Expand Down
9 changes: 7 additions & 2 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- Admin api add query `/db_size` to check database size and upsert in metadata
- Admin api add query `/db_size` to check database size and upsert in metadata

### Fixed
- `exitWithError` logging error messages rather than the error (#2435)

### Changed
- Logging around switching dictionary (#2435)

## [10.4.1] - 2024-06-06
### Fixed
Expand Down
8 changes: 7 additions & 1 deletion packages/node-core/src/admin/admin.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {HttpException, HttpStatus} from '@nestjs/common';
import {EventEmitter2} from '@nestjs/event-emitter';
import {Test, TestingModule} from '@nestjs/testing';
import {TargetBlockPayload, RewindPayload, AdminEvent} from '../events';
import {MonitorService, PoiService, ProofOfIndex} from '../indexer';
import {MonitorService, PoiService, ProofOfIndex, StoreService} from '../indexer';
import {AdminController, AdminListener} from './admin.controller';
import {BlockRangeDto} from './blockRange';

Expand Down Expand Up @@ -45,6 +45,12 @@ describe('AdminController', () => {
emit: jest.fn(),
},
},
{
provide: StoreService,
useValue: {
syncDbSize: jest.fn(),
},
},
],
}).compile();

Expand Down
4 changes: 2 additions & 2 deletions packages/node-core/src/db/sync-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ describe('sync-helper', () => {
`DO $$
BEGIN
ALTER TABLE "test"."test-table"
ADD
ADD
CONSTRAINT test-table_transfer_id_id_fkey
FOREIGN KEY (transfer_id_id)
FOREIGN KEY (transfer_id_id)
REFERENCES "test"."transfers" (id) ON DELETE NO ACTION ON UPDATE CASCADE;
EXCEPTION
WHEN duplicate_object THEN
Expand Down
20 changes: 13 additions & 7 deletions packages/node-core/src/indexer/dictionary/dictionary.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,22 @@ export abstract class DictionaryService<DS, FB> implements IDictionaryCtrl<DS, F
}

// Only log on change of dictionary
if (previousIndex !== this._currentDictionaryIndex) {
if (this._currentDictionaryIndex === undefined) {
if (this._dictionaries.length) {
logger.warn(`No supported dictionary found`);
try {
if (previousIndex !== this._currentDictionaryIndex) {
if (this._currentDictionaryIndex === undefined) {
if (this._dictionaries.length) {
logger.warn(`No supported dictionary found`);
} else {
logger.debug(`No dictionaries available to use`);
}
} else {
logger.debug(`No dictionaries available to use`);
logger.info(
`Changed dictionray: new index: ${this._currentDictionaryIndex}, type: ${this._dictionaries[this._currentDictionaryIndex]?.constructor?.name}`
);
}
} else {
logger.debug(`Updated: current dictionary Index is ${this._currentDictionaryIndex}`);
}
} catch (e) {
// Do nothing, logging shouldn't cause errors
}

return dict;
Expand Down
2 changes: 1 addition & 1 deletion packages/node-core/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function setMonitorService(service: MonitorServiceInterface): void {

export function exitWithError(error: Error | string, logger?: Pino.Logger, code = 1): never {
const errorMessage = typeof error === 'string' ? error : error.message;
logger?.error(errorMessage);
logger?.error(error as any); /* Bad types */
monitorService?.write(`[EXIT ${code}]: ${errorMessage}`);
process.exit(code);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Support logging errors with provided cause and improve colors (#2435)

## [2.10.0] - 2024-05-08
### Changed
Expand Down
48 changes: 34 additions & 14 deletions packages/utils/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ export interface LoggerOption {
debugFilter?: string[];
}

function formatErrorString(err: unknown, stack = false): string {
if (err instanceof Error) {
let formattedError = `${ctx.red('Error:')} ${ctx.yellow(err.message)}`;

if (stack) {
formattedError += `\n${ctx.red('Stack:')} ${ctx.gray(err.stack)}`;
}

if (err.cause) {
formattedError += `\n${ctx.red('Cause:')} ${formatErrorString(err.cause, stack)}`;
}

return formattedError;
}
return String(err);
}

function formatErrorJson(err: unknown): unknown {
if (err instanceof Error) {
return {
type: 'error',
name: err.name,
message: err.message,
stack: err.stack,
cause: err.cause ? formatErrorJson(err.cause) : undefined,
};
} else {
return stringify(err);
}
}

export class Logger {
private pino: Pino.Logger;
private childLoggers: {[category: string]: Pino.Logger} = {};
Expand All @@ -41,18 +72,7 @@ export class Logger {
serializers:
outputFormat === 'json'
? {
payload: (value) => {
if (value instanceof Error) {
return {
type: 'error',
name: value.name,
message: value.message,
stack: value.stack,
};
} else {
return stringify(value);
}
},
payload: formatErrorJson,
}
: {},
prettyPrint: outputFormat !== 'json',
Expand All @@ -72,9 +92,9 @@ export class Logger {
let error = '';
if (payload instanceof Error) {
if (['debug', 'trace'].includes(logLevel)) {
error = `\n${payload.stack}`;
error = `\n${formatErrorString(payload, true)}`;
} else {
error = `${payload.name}: ${payload.message}`;
error = formatErrorString(payload);
}
}
return `${time} <${ctx.magentaBright(category)}> ${colorizeLevel(level)} ${message} ${error}\n`;
Expand Down
23 changes: 12 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3652,7 +3652,7 @@ __metadata:
languageName: node
linkType: hard

"@ethersproject/providers@npm:5.7.2":
"@ethersproject/providers@npm:5.7.2, @ethersproject/providers@npm:^5.7.2":
version: 5.7.2
resolution: "@ethersproject/providers@npm:5.7.2"
dependencies:
Expand Down Expand Up @@ -6210,7 +6210,7 @@ __metadata:
"@subql/common-algorand": ^3.4.0
"@subql/common-concordium": ^3.6.0
"@subql/common-cosmos": ^4.3.0
"@subql/common-ethereum": ^3.6.0
"@subql/common-ethereum": ^3.8.3
"@subql/common-flare": ^3.6.0
"@subql/common-near": ^3.5.0
"@subql/common-stellar": ^3.5.0
Expand Down Expand Up @@ -6319,12 +6319,12 @@ __metadata:
languageName: node
linkType: hard

"@subql/common-ethereum@npm:^3.6.0":
version: 3.6.0
resolution: "@subql/common-ethereum@npm:3.6.0"
"@subql/common-ethereum@npm:^3.8.3":
version: 3.8.3
resolution: "@subql/common-ethereum@npm:3.8.3"
dependencies:
"@subql/common": ^3.5.1
"@subql/types-ethereum": 3.6.0
"@subql/types-ethereum": 3.6.1
"@typechain/ethers-v5": ^11.1.1
"@zilliqa-js/crypto": ^3.5.0
js-yaml: ^4.1.0
Expand All @@ -6334,7 +6334,7 @@ __metadata:
peerDependencies:
class-transformer: "*"
class-validator: "*"
checksum: 46bb855bbffa53a33af95d2c1427876f873e0ebb4c6c7b4dd4be7279acb050f8f0eb8b9ab9c5e81758139b6dada6bc86b35e96dd1638b447115527cc841258c6
checksum: 12a376e9e97a15c7bc1c0317ce1ba9ef7b46caf6d40a720ce669b3d5d3bea744c9d1492e2bf328951bf8a70c2b6ae7f5ba25d9e4dafd56c9f0d5ba4f59d83a42
languageName: node
linkType: hard

Expand Down Expand Up @@ -6597,13 +6597,14 @@ __metadata:
languageName: node
linkType: hard

"@subql/types-ethereum@npm:3.6.0":
version: 3.6.0
resolution: "@subql/types-ethereum@npm:3.6.0"
"@subql/types-ethereum@npm:3.6.1":
version: 3.6.1
resolution: "@subql/types-ethereum@npm:3.6.1"
dependencies:
"@ethersproject/abstract-provider": ^5.6.1
"@ethersproject/providers": ^5.7.2
"@subql/types-core": ^0.7.0
checksum: b463e88d6dca985ea0aab68d43188aa01f185493d91d28c8d97c4b3f1232314f7ce905af90114af169c01b18da540afcc60af1aa96a64fa1fc0878fd0efe384e
checksum: dbee1ddef99840eac82a5f562e25a69491e4f95017f1f3d1e7396a69112bdac80b885b3d689867e365a87d9bf5aff44db6e2ec349f68437fadff1828333cbc33
languageName: node
linkType: hard

Expand Down
Loading