-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle uncatch exectipn on messager event
- Loading branch information
Showing
11 changed files
with
123 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { EventEmitter, captureRejectionSymbol } from 'node:events'; | ||
import { MessageUnhandledRejectionError } from '../../error/index.js'; | ||
import { EggApplicationCore } from '../../egg.js'; | ||
|
||
export class BaseMessenger extends EventEmitter { | ||
protected readonly egg: EggApplicationCore; | ||
|
||
constructor(egg: EggApplicationCore) { | ||
super({ captureRejections: true }); | ||
this.egg = egg; | ||
} | ||
|
||
[captureRejectionSymbol](err: Error, event: string | symbol, ...args: any[]) { | ||
this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, event, args)); | ||
} | ||
|
||
emit(eventName: string | symbol, ...args: any[]): boolean { | ||
const hasListeners = this.listenerCount(eventName) > 0; | ||
try { | ||
return super.emit(eventName, ...args); | ||
} catch (e: unknown) { | ||
let err = e as Error; | ||
if (!(err instanceof Error)) { | ||
err = new Error(String(err)); | ||
} | ||
this.egg.coreLogger.error(new MessageUnhandledRejectionError(err, eventName, args)); | ||
return hasListeners; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export class MessageUnhandledRejectionError extends Error { | ||
event: string | symbol; | ||
args: any[]; | ||
|
||
constructor(err: Error, event: string | symbol, ...args: any[]) { | ||
super(`event: ${String(event)}, error: ${err.message}`, { cause: err }); | ||
this.name = this.constructor.name; | ||
this.event = event; | ||
this.args = args; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './CookieLimitExceedError.js'; | ||
export * from './MessageUnhandledRejectionError.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { mm } from '@eggjs/mock'; | ||
import { getFilepath, MockApplication, cluster } from '../utils.js'; | ||
|
||
describe('test/lib/agent.test.ts', () => { | ||
afterEach(mm.restore); | ||
|
||
describe('agent throw', () => { | ||
const baseDir = getFilepath('apps/agent-throw'); | ||
let app: MockApplication; | ||
before(() => { | ||
app = cluster('apps/agent-throw'); | ||
return app.ready(); | ||
}); | ||
after(() => app.close()); | ||
|
||
it('should catch unhandled exception', done => { | ||
app.httpRequest() | ||
.get('/agent-throw-async') | ||
.expect(200, err => { | ||
assert(!err); | ||
setTimeout(() => { | ||
const body = fs.readFileSync(path.join(baseDir, 'logs/agent-throw/common-error.log'), 'utf8'); | ||
assert.match(body, /nodejs\.MessageUnhandledRejectionError: event: agent-throw-async, error: agent error in async function/); | ||
app.notExpect('stderr', /nodejs.AgentWorkerDiedError/); | ||
done(); | ||
}, 1000); | ||
}); | ||
}); | ||
|
||
it('should exit on sync error throw', done => { | ||
app.httpRequest() | ||
.get('/agent-throw') | ||
.expect(200, err => { | ||
assert(!err); | ||
setTimeout(() => { | ||
const body = fs.readFileSync(path.join(baseDir, 'logs/agent-throw/common-error.log'), 'utf8'); | ||
assert.match(body, /nodejs\.MessageUnhandledRejectionError: event: agent-throw, error: agent error in sync function/); | ||
app.notExpect('stderr', /nodejs.AgentWorkerDiedError/); | ||
done(); | ||
}, 1000); | ||
}); | ||
}); | ||
|
||
it('should catch uncaughtException string error', done => { | ||
app.httpRequest() | ||
.get('/agent-throw-string') | ||
.expect(200, err => { | ||
assert(!err); | ||
setTimeout(() => { | ||
const body = fs.readFileSync(path.join(baseDir, 'logs/agent-throw/common-error.log'), 'utf8'); | ||
assert.match(body, /nodejs\.MessageUnhandledRejectionError: event: agent-throw-string, error: agent error string/); | ||
app.notExpect('stderr', /nodejs.AgentWorkerDiedError/); | ||
done(); | ||
}, 1000); | ||
}); | ||
}); | ||
}); | ||
}); |