Skip to content

Commit 2a57090

Browse files
committedJun 22, 2021
Added node plugins
·
v3.1.0v2.0.0-beta1
1 parent f1aca22 commit 2a57090

File tree

5 files changed

+156
-83
lines changed

5 files changed

+156
-83
lines changed
 

‎packages/node/src/NodeExceptionlessClient.ts

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {
44
} from "@exceptionless/core";
55

66
import { NodeConfiguration } from "./configuration/NodeConfiguration.js";
7+
import { NodeGlobalHandlerPlugin } from "./plugins/NodeGlobalHandlerPlugin.js";
8+
import { NodeLifeCyclePlugin } from "./plugins/NodeLifeCyclePlugin.js";
9+
import { NodeWrapFunctions } from "./plugins/NodeWrapFunctions.js";
710
import { NodeEnvironmentInfoCollector } from "./services/NodeEnvironmentInfoCollector.js";
811
import { NodeErrorParser } from "./services/NodeErrorParser.js";
912
import { NodeRequestInfoCollector } from "./services/NodeRequestInfoCollector.js";
@@ -15,92 +18,19 @@ export class NodeExceptionlessClient extends ExceptionlessClient {
1518
}
1619

1720
public async startup(configurationOrApiKey?: (config: NodeConfiguration) => void | string): Promise<void> {
18-
if (configurationOrApiKey) {
19-
this.config.services.environmentInfoCollector = new NodeEnvironmentInfoCollector();
20-
this.config.services.errorParser = new NodeErrorParser();
21-
this.config.services.requestInfoCollector = new NodeRequestInfoCollector();
22-
this.config.services.submissionClient = new NodeFetchSubmissionClient(this.config);
21+
const config = this.config;
2322

24-
// TODO: Register platform specific plugins.
23+
if (configurationOrApiKey) {
24+
config.services.environmentInfoCollector = new NodeEnvironmentInfoCollector();
25+
config.services.errorParser = new NodeErrorParser();
26+
config.services.requestInfoCollector = new NodeRequestInfoCollector();
27+
config.services.submissionClient = new NodeFetchSubmissionClient(config);
28+
29+
config.addPlugin(new NodeGlobalHandlerPlugin());
30+
config.addPlugin(new NodeLifeCyclePlugin());
31+
config.addPlugin(new NodeWrapFunctions());
2532
}
2633

2734
await super.startup(configurationOrApiKey);
2835
}
2936
}
30-
/*
31-
32-
33-
import {
34-
addListener,
35-
on
36-
} from "process";
37-
38-
addListener("uncaughtException", (error: Error) => {
39-
//ExceptionlessClient.default.submitUnhandledException(error, "uncaughtException");
40-
});
41-
42-
// TODO: Handle submission https://stackoverflow.com/questions/40574218/how-to-perform-an-async-operation-on-exit
43-
44-
on("exit", (code: number) => {
45-
/**
46-
* exit codes: https://nodejs.org/api/process.html#process_event_exit
47-
* From now on, only synchronous code may run. As soon as this method
48-
* ends, the application inevitably will exit.
49-
*/
50-
/*
51-
function getExitCodeReason(exitCode: number): string {
52-
if (exitCode === 1) {
53-
return "Uncaught Fatal Exception";
54-
}
55-
56-
if (exitCode === 3) {
57-
return "Internal JavaScript Parse Error";
58-
}
59-
60-
if (exitCode === 4) {
61-
return "Internal JavaScript Evaluation Failure";
62-
}
63-
64-
if (exitCode === 5) {
65-
return "Fatal Exception";
66-
}
67-
68-
if (exitCode === 6) {
69-
return "Non-function Internal Exception Handler ";
70-
}
71-
72-
if (exitCode === 7) {
73-
return "Internal Exception Handler Run-Time Failure";
74-
}
75-
76-
if (exitCode === 8) {
77-
return "Uncaught Exception";
78-
}
79-
80-
if (exitCode === 9) {
81-
return "Invalid Argument";
82-
}
83-
84-
if (exitCode === 10) {
85-
return "Internal JavaScript Run-Time Failure";
86-
}
87-
88-
if (exitCode === 12) {
89-
return "Invalid Debug Argument";
90-
}
91-
92-
return null;
93-
}
94-
95-
const message = getExitCodeReason(code);
96-
97-
if (message !== null) {
98-
Exceptionless.submitLog("exit", message, "Error");
99-
}
100-
101-
Exceptionless.config.queue.process();
102-
// Application will now exit.
103-
});
104-
105-
//(Error as any).stackTraceLimit = Infinity;
106-
*/

‎packages/node/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export { NodeConfiguration } from "./configuration/NodeConfiguration.js";
2+
export { NodeGlobalHandlerPlugin } from "./plugins/NodeGlobalHandlerPlugin.js";
3+
export { NodeLifeCyclePlugin } from "./plugins/NodeLifeCyclePlugin.js";
4+
export { NodeWrapFunctions } from "./plugins/NodeWrapFunctions.js";
25
export { NodeEnvironmentInfoCollector } from "./services/NodeEnvironmentInfoCollector.js";
36
export { NodeErrorParser } from "./services/NodeErrorParser.js";
47
export { NodeRequestInfoCollector } from "./services/NodeRequestInfoCollector.js";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
ExceptionlessClient,
3+
IEventPlugin,
4+
PluginContext
5+
} from "@exceptionless/core";
6+
7+
import { addListener } from "process";
8+
9+
export class NodeGlobalHandlerPlugin implements IEventPlugin {
10+
public priority: number = 100;
11+
public name: string = "NodeGlobalHandlerPlugin";
12+
13+
private _client: ExceptionlessClient = null;
14+
15+
public startup(context: PluginContext): Promise<void> {
16+
if (this._client) {
17+
return;
18+
}
19+
20+
this._client = context.client;
21+
Error.stackTraceLimit = 50;
22+
23+
addListener("uncaughtException", async (error: Error) => {
24+
await this._client.submitUnhandledException(error, "uncaughtException");
25+
});
26+
27+
addListener("unhandledRejection", async (error: Error) => {
28+
await this._client.submitUnhandledException(error, "unhandledRejection");
29+
});
30+
31+
return Promise.resolve();
32+
}
33+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
ExceptionlessClient,
3+
IEventPlugin,
4+
PluginContext
5+
} from "@exceptionless/core";
6+
7+
import { on } from "process";
8+
9+
export class NodeLifeCyclePlugin implements IEventPlugin {
10+
public priority: number = 105;
11+
public name: string = "NodeLifeCyclePlugin";
12+
13+
private _client: ExceptionlessClient = null;
14+
15+
public startup(context: PluginContext): Promise<void> {
16+
if (this._client) {
17+
return;
18+
}
19+
20+
this._client = context.client;
21+
22+
on("beforeExit", async (code: number) => {
23+
const message: string = this.getExitCodeReason(code);
24+
if (message !== null) {
25+
await this._client.submitLog("beforeExit", message, "Error");
26+
}
27+
28+
await this._client.suspend();
29+
// Application will now exit.
30+
});
31+
32+
return Promise.resolve();
33+
}
34+
35+
/**
36+
* exit codes: https://nodejs.org/api/process.html#process_event_exit
37+
* From now on, only synchronous code may run. As soon as this method
38+
* ends, the application inevitably will exit.
39+
*/
40+
private getExitCodeReason(exitCode: number): string {
41+
if (exitCode === 1) {
42+
return "Uncaught Fatal Exception";
43+
}
44+
45+
if (exitCode === 3) {
46+
return "Internal JavaScript Parse Error";
47+
}
48+
49+
if (exitCode === 4) {
50+
return "Internal JavaScript Evaluation Failure";
51+
}
52+
53+
if (exitCode === 5) {
54+
return "Fatal Exception";
55+
}
56+
57+
if (exitCode === 6) {
58+
return "Non-function Internal Exception Handler ";
59+
}
60+
61+
if (exitCode === 7) {
62+
return "Internal Exception Handler Run-Time Failure";
63+
}
64+
65+
if (exitCode === 8) {
66+
return "Uncaught Exception";
67+
}
68+
69+
if (exitCode === 9) {
70+
return "Invalid Argument";
71+
}
72+
73+
if (exitCode === 10) {
74+
return "Internal JavaScript Run-Time Failure";
75+
}
76+
77+
if (exitCode === 12) {
78+
return "Invalid Debug Argument";
79+
}
80+
81+
return null;
82+
}
83+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
ExceptionlessClient,
3+
IEventPlugin,
4+
PluginContext
5+
} from "@exceptionless/core";
6+
7+
export class NodeWrapFunctions implements IEventPlugin {
8+
public priority: number = 110;
9+
public name: string = "NodeWrapFunctions";
10+
11+
private _client: ExceptionlessClient = null;
12+
13+
public startup(context: PluginContext): Promise<void> {
14+
if (this._client) {
15+
return;
16+
}
17+
18+
this._client = context.client;
19+
20+
// TODO: TraceKit.extendToAsynchronousCallbacks();
21+
22+
return Promise.resolve();
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.