Skip to content

Commit a127c2f

Browse files
committed
extract parser exception parser to exernal library on npm, code cleanup and improvements
1 parent f00623a commit a127c2f

11 files changed

Lines changed: 44 additions & 252 deletions

File tree

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "traceo-sdk",
3-
"version": "0.0.82-alpha",
3+
"version": "0.0.84",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -23,8 +23,8 @@
2323
"@rollup/plugin-typescript": "^8.3.0",
2424
"eslint": "^8.8.0",
2525
"jest": "^26.5.5",
26-
"os": "^0.1.2",
2726
"npm-run-all": "^4.1.2",
27+
"os": "^0.1.2",
2828
"prettier": "^2.5.1",
2929
"prettier-check": "^2.0.0",
3030
"rimraf": "^3.0.2",
@@ -48,5 +48,8 @@
4848
"testMatch": [
4949
"**/*.test.ts"
5050
]
51+
},
52+
"dependencies": {
53+
"stacktrace-parser-node": "^1.0.4"
5154
}
5255
}

src/core/http.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import * as http from "http";
22
import { sanitizeDsn } from "../node/helpers";
3-
import { RequestStatus } from "../transport/enums";
43
import { EventResponse, TraceoEvent } from "../transport/events";
54
import { TraceoIncomingMessage, RequestOptions } from "../transport/http";
65
import { getGlobalClientData } from "./global";
76
import { isClientConnected } from "./is";
87
import { TRACEO_SDK_VERSION } from "./version";
98

9+
enum RequestStatus {
10+
SUCCESS = "success",
11+
ERROR = "error",
12+
}
13+
1014
const createHttpOptions = ({
1115
event,
1216
}: {

src/core/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const TRACEO_SDK_VERSION = "0.0.82-alpha";
1+
export const TRACEO_SDK_VERSION = "0.0.84";

src/node/helpers.ts

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
1-
import { BaseObject } from "../transport/base";
21
import { Platform } from "../transport/events";
3-
import { TraceoIncomingMessage, TraceoRequest } from "../transport/http";
2+
import { TraceoIncomingMessage } from "../transport/http";
43
import * as os from "os";
54

6-
export const mapRequestData = (req: BaseObject): TraceoRequest => {
7-
const headersData = req.headers || req.header || {};
8-
9-
const method = req.method;
10-
const host = headersData["host"] || "<no host>";
11-
12-
const protocol = getProtocol(req.protocol);
13-
14-
const originalUrl = (req.originalUrl || req.url) as string;
15-
const absoluteUrl = `${protocol}://${host}${originalUrl}`;
16-
const origin = headersData["origin"];
17-
const query = req.query;
18-
const payload = req.body || {};
19-
const ip = getIp(req as TraceoIncomingMessage);
20-
21-
const connections = {
22-
absoluteUrl,
23-
origin,
24-
protocol,
25-
};
26-
27-
const headers = {
28-
host,
29-
connection: headersData["connection"],
30-
origin: headersData["origin"],
31-
};
32-
33-
const request = {
34-
payload,
35-
headers,
36-
method,
37-
query,
38-
ip,
39-
url: connections,
40-
};
41-
42-
return request;
43-
};
44-
455
export const getIp = (
466
req: TraceoIncomingMessage
477
): string | string[] | undefined => {
@@ -62,8 +22,10 @@ export const getOsPlatform = (): Platform => {
6222
};
6323

6424
export const sanitizeDsn = (dsn: string) => {
65-
//TODO: check for https
66-
const [secretKey, rest] = dsn.replace("http://", "").split(":");
25+
const [secretKey, rest] = dsn
26+
.replace("http://", "")
27+
.replace("https://", "")
28+
.split(":");
6729
const [host, appId] = rest.split("/");
6830

6931
return {

src/node/middlewares.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { stacktrace } from "stacktrace-parser-node";
12
import { sendEvent } from "../core/http";
23
import { isClientConnected, isLocalhost } from "../core/is";
34
import { TraceoError } from "../transport/base";
45
import { TraceoEvent } from "../transport/events";
56
import { TraceoIncomingMessage, TraceoServerResponse } from "../transport/http";
67
import { ErrorMiddlewareOptions } from "../transport/options";
7-
import { getIp, getProtocol } from "./helpers";
8-
import { prepareException } from "./parse";
8+
import { getIp, getOsPlatform, getProtocol } from "./helpers";
99

1010
/**
1111
* Base middleware to catch and intercept error across the express app.
@@ -43,7 +43,7 @@ const errorMiddleware = (options: ErrorMiddlewareOptions = {}) => {
4343
}
4444

4545
if (isToCatch(req, options)) {
46-
await handleException(error, req);
46+
await handleException(error);
4747
}
4848

4949
next(error);
@@ -86,7 +86,7 @@ interface Catch {
8686
* }
8787
* ```
8888
*
89-
* Usage with optional parameter `shouldBeCatched` as a callback function:
89+
* Usage with `shouldBeCatched`:
9090
* @example
9191
*
9292
* ```
@@ -106,24 +106,40 @@ export const catchException = async (error: any, catchOptions?: Catch) => {
106106
}
107107

108108
if (isClientConnected()) {
109-
await handleException(error, undefined);
109+
await handleException(error);
110110
}
111111

112112
return;
113113
};
114114

115-
const handleException = async (
116-
error: TraceoError,
117-
req?: TraceoIncomingMessage
118-
) => {
115+
const handleException = async (error: TraceoError) => {
119116
try {
120-
const event: TraceoEvent = await prepareException(error, req);
117+
const event: TraceoEvent = await prepareException(error);
121118
await sendEvent(event);
122119
} catch (err) {
123120
//
124121
}
125122
};
126123

124+
125+
const prepareException = async (
126+
error: TraceoError
127+
): Promise<TraceoEvent> => {
128+
const { stack } = error;
129+
const platform = getOsPlatform();
130+
131+
const { message, name, traces } = await stacktrace.parse(error);
132+
const event: TraceoEvent = {
133+
type: name,
134+
message,
135+
traces,
136+
stack: String(stack),
137+
platform,
138+
};
139+
140+
return event;
141+
};
142+
127143
export const Middleware = {
128144
errorMiddleware,
129145
};

src/node/parse.ts

Lines changed: 0 additions & 147 deletions
This file was deleted.

src/node/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TraceoOptions } from "../transport/options";
44

55
/**
66
*
7-
* Function to connect client with Traceo Instance.
7+
* Function to connect client with Traceo.
88
*
99
* @param options
1010
*
@@ -17,7 +17,7 @@ export const init = (options: TraceoOptions): void => {
1717

1818
if (!options.environment) {
1919
console.warn(
20-
"Traceo SDK: Empty environment property. Please set current env or use use offline mode."
20+
"Traceo SDK: Empty environment property. Please set current env or use offline mode."
2121
);
2222
return;
2323
}

src/transport/base.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { Environment } from "./types";
22

3-
export interface BaseObject {
4-
[key: string]: any;
5-
}
6-
73
export interface TraceoGlobal {
84
dsn: string;
95
appId?: string;

src/transport/enums.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)