Skip to content

Commit

Permalink
feat: print AggregateError errors stack (#98)
Browse files Browse the repository at this point in the history
closes eggjs/egg#5370

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **New Features**
- Introduced a new test case for handling `AggregateError` instances in
error formatting.

- **Bug Fixes**
- Corrected a typographical error in variable naming for improved
clarity.

- **Chores**
	- Updated the `.gitignore` file to include `package-lock.json`.
	- Removed the `git-contributor` dependency from `package.json`.

- **Documentation**
- Simplified the contributors section in the README files with a badge
linking to the contributors graph on GitHub.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Nov 30, 2024
1 parent adbc0c5 commit 03c38bd
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '14, 16, 18, 20, 22'
version: '14.18.0, 14, 16, 18, 20, 22'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tmp_file_*
*.log
.nyc_output
.idea
package-lock.json
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,12 @@ logger.info('info');

set environment NODE_CONSOLE_LOGGRE_LEVEL = 'INFO' | 'WARN' | 'ERROR'


## License

[MIT](LICENSE)
<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/2160731?v=4" width="100px;"/><br/><sub><b>mansonchor</b></sub>](https://github.com/mansonchor)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/3367820?v=4" width="100px;"/><br/><sub><b>Jeff-Tian</b></sub>](https://github.com/Jeff-Tian)<br/>|[<img src="https://avatars.githubusercontent.com/u/1763067?v=4" width="100px;"/><br/><sub><b>waitingsong</b></sub>](https://github.com/waitingsong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7581901?v=4" width="100px;"/><br/><sub><b>sjfkai</b></sub>](https://github.com/sjfkai)<br/>|[<img src="https://avatars.githubusercontent.com/u/2675419?v=4" width="100px;"/><br/><sub><b>congyuandong</b></sub>](https://github.com/congyuandong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7779883?v=4" width="100px;"/><br/><sub><b>lix059</b></sub>](https://github.com/lix059)<br/>|
[<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/30541930?v=4" width="100px;"/><br/><sub><b>duqingyu</b></sub>](https://github.com/duqingyu)<br/>|[<img src="https://avatars.githubusercontent.com/u/2764744?v=4" width="100px;"/><br/><sub><b>AmazingCaddy</b></sub>](https://github.com/AmazingCaddy)<br/>|[<img src="https://avatars.githubusercontent.com/u/8201516?v=4" width="100px;"/><br/><sub><b>linrf</b></sub>](https://github.com/linrf)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|[<img src="https://avatars.githubusercontent.com/u/1474688?v=4" width="100px;"/><br/><sub><b>luckydrq</b></sub>](https://github.com/luckydrq)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 08 2024 09:39:22 GMT+0800`.
[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-logger)](https://github.com/eggjs/egg-logger/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
11 changes: 2 additions & 9 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,8 @@ logger.info('info');
## License

[MIT](LICENSE)
<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/2160731?v=4" width="100px;"/><br/><sub><b>mansonchor</b></sub>](https://github.com/mansonchor)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/3367820?v=4" width="100px;"/><br/><sub><b>Jeff-Tian</b></sub>](https://github.com/Jeff-Tian)<br/>|[<img src="https://avatars.githubusercontent.com/u/1763067?v=4" width="100px;"/><br/><sub><b>waitingsong</b></sub>](https://github.com/waitingsong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7581901?v=4" width="100px;"/><br/><sub><b>sjfkai</b></sub>](https://github.com/sjfkai)<br/>|[<img src="https://avatars.githubusercontent.com/u/2675419?v=4" width="100px;"/><br/><sub><b>congyuandong</b></sub>](https://github.com/congyuandong)<br/>|[<img src="https://avatars.githubusercontent.com/u/7779883?v=4" width="100px;"/><br/><sub><b>lix059</b></sub>](https://github.com/lix059)<br/>|
[<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/30541930?v=4" width="100px;"/><br/><sub><b>duqingyu</b></sub>](https://github.com/duqingyu)<br/>|[<img src="https://avatars.githubusercontent.com/u/2764744?v=4" width="100px;"/><br/><sub><b>AmazingCaddy</b></sub>](https://github.com/AmazingCaddy)<br/>|[<img src="https://avatars.githubusercontent.com/u/8201516?v=4" width="100px;"/><br/><sub><b>linrf</b></sub>](https://github.com/linrf)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|[<img src="https://avatars.githubusercontent.com/u/1474688?v=4" width="100px;"/><br/><sub><b>luckydrq</b></sub>](https://github.com/luckydrq)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 08 2024 09:39:22 GMT+0800`.
[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-logger)](https://github.com/eggjs/egg-logger/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
46 changes: 30 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors');
const levels = require('./level');

const hostname = os.hostname();
const duartionRegexp = /([0-9]+ms)/g;
const durationRegexp = /([0-9]+ms)/g;
// eslint-disable-next-line no-useless-escape
const categoryRegexp = /(\[[\w\-_.:]+\])/g;
const httpMethodRegexp = /(GET|POST|PUT|PATH|HEAD|DELETE) /g;
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = {
return chalk.yellow(msg);
}

msg = msg.replace(duartionRegexp, chalk.green('$1'));
msg = msg.replace(durationRegexp, chalk.green('$1'));
msg = msg.replace(categoryRegexp, chalk.blue('$1'));
msg = msg.replace(httpMethodRegexp, chalk.cyan('$1 '));
return msg;
Expand Down Expand Up @@ -200,36 +200,50 @@ function errorToString(err, options, causeLength) {
const maxCauseChainLength = options.maxCauseChainLength || 10;
causeLength = causeLength || 0;

if (causeLength > maxCauseChainLength) return 'too long cause chain';
if (causeLength > maxCauseChainLength) {
return 'too long cause chain';
}

let errName = err.name || 'no_name';
if (err.name === 'Error' && typeof err.code === 'string') {
err.name = err.code + err.name;
errName = err.code + errName;
}

let errMessage = err.message || 'no_message';

if (err.host) {
err.message += ` (${err.host})`;
errMessage += ` (${err.host})`;
}

// name and stack could not be change on node 0.11+
const errStack = err.stack || 'no_stack';

const errProperties = Object.keys(err).map(key => inspect(key, err[key])).join('\n');
let errorString = util.format('nodejs.%s: %s\n%s\n%s',
errName,
errMessage,
errStack.substring(errStack.indexOf('\n') + 1),
errProperties
);
if (err.name === 'AggregateError' && err.errors) {
for (let i = 0; i < err.errors.length; i++) {
const subError = err.errors[i];
const subErrorMsg = errorToString(subError, options, causeLength + 1);
errorString = util.format('%s\n[error-%d]:\n\n%s',
errorString,
i,
subErrorMsg
);
}
}
if (err.cause) {
const causeMsg = errorToString(err.cause, options, causeLength + 1);
return util.format('nodejs.%s: %s\n%s\n%s\ncause:\n\n%s',
err.name,
err.message,
errStack.substring(errStack.indexOf('\n') + 1),
errProperties,
errorString = util.format('%s\ncause:\n\n%s',
errorString,
causeMsg
);
}
return util.format('nodejs.%s: %s\n%s\n%s',
err.name,
err.message,
errStack.substring(errStack.indexOf('\n') + 1),
errProperties
);
return errorString;
}

function formatError(err, options, causeLength) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"egg-bin": "6",
"eslint": "^8.29.0",
"eslint-config-egg": "^12.1.0",
"git-contributor": "^2.0.0",
"koa": "^2.14.1",
"mm": "^3.2.1",
"supertest": "^6.3.3",
Expand Down
37 changes: 37 additions & 0 deletions test/lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,42 @@ describe('test/lib/utils.test.js', () => {
assert(msg.match(/pid: /));
assert(msg.match(/hostname: /));
});

it('should format AggregateError', () => {
// eslint-disable-next-line no-undef
const rootError = new AggregateError([
new Error('error 1'),
new TypeError('error 2', { cause: new Error('error 2 cause error') }),
]);
// nodejs.AggregateError: no_message
// at Context.<anonymous> (/github.com/eggjs/egg-logger/test/lib/utils.test.js:151:25)
// at process.processImmediate (node:internal/timers:491:21)
//
// [error-0]:
//
// nodejs.Error: error 1
// at Context.<anonymous> (/github.com/eggjs/egg-logger/test/lib/utils.test.js:152:9)
// at process.processImmediate (node:internal/timers:491:21)
//
// [error-1]:
//
// nodejs.TypeError: error 2
// at Context.<anonymous> (/github.com/eggjs/egg-logger/test/lib/utils.test.js:153:9)
// at process.processImmediate (node:internal/timers:491:21)
//
// pid: 71661
// hostname: xxxx

const msg = formatError(rootError);
// console.log(msg);
assert(msg.match(/nodejs.AggregateError: no_message/));
assert(msg.match(/\[error-0]:/));
assert(msg.match(/nodejs.Error: error 1/));
assert(msg.match(/\[error-1]:/));
assert(msg.match(/nodejs.TypeError: error 2/));
assert(msg.match(/nodejs.Error: error 2 cause error/));
assert(msg.match(/pid: /));
assert(msg.match(/hostname: /));
});
});
});

0 comments on commit 03c38bd

Please sign in to comment.