Skip to content

Commit db6f498

Browse files
committed
added platform information to incident object
1 parent 9282d45 commit db6f498

5 files changed

Lines changed: 147 additions & 129 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "klepper",
3-
"version": "0.0.60-alpha",
3+
"version": "0.0.61-alpha",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/node/helpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { BaseObject } from "../transport/base";
2+
import { Platform } from "../transport/events";
23
import { KlepperIncomingMessage, KlepperRequest } from "../transport/http";
4+
import * as os from "os";
35

46
export const mapRequestData = (req: BaseObject): KlepperRequest => {
57
const headersData = req.headers || req.header || {};
@@ -49,3 +51,12 @@ export const getIp = (
4951
export const getProtocol = (req: KlepperIncomingMessage): string => {
5052
return req.protocol === "https" || req.secure ? "https" : "http";
5153
};
54+
55+
export const getOsPlatform = (): Platform => {
56+
return {
57+
arch: os.arch(),
58+
platform: os.platform(),
59+
release: os.release(),
60+
version: os.version(),
61+
}
62+
}

src/node/parse.ts

Lines changed: 124 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,151 @@
11
import { Trace } from "../transport/trace";
22
import { promises, existsSync } from "fs";
33
import { KlepperError } from "../transport/base";
4-
import { CatchExceptionsOptions, KlepperOptions } from "../transport/options";
4+
import { CatchExceptionsOptions } from "../transport/options";
55
import { KlepperEvent } from "../transport/events";
66
import { KlepperIncomingMessage } from "../transport/http";
7-
import { mapRequestData } from "./helpers";
7+
import { getOsPlatform, mapRequestData } from "./helpers";
8+
import * as os from "os";
89

910
const FULL_MATCH =
10-
/at (?:async )?(?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
11+
/at (?:async )?(?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
1112

1213
export const parseStackTraces = async (stack: string): Promise<Trace[]> => {
13-
const frames: Trace[] = [];
14+
const frames: Trace[] = [];
1415

15-
if (!stack.length) {
16-
return [];
17-
}
16+
if (!stack.length) {
17+
return [];
18+
}
1819

19-
for (const line of stack.split("\n").slice(1)) {
20-
const frame = await createTrace(line);
21-
if (frame) {
22-
frames.push(frame);
23-
}
20+
for (const line of stack.split("\n").slice(1)) {
21+
const frame = await createTrace(line);
22+
if (frame) {
23+
frames.push(frame);
2424
}
25+
}
2526

26-
return (
27-
frames.slice(0, 25).map((frame) => ({
28-
...frame,
29-
})) || []
30-
);
27+
return (
28+
frames.slice(0, 25).map((frame) => ({
29+
...frame,
30+
})) || []
31+
);
3132
};
3233

3334
export const createTrace = async (line: string): Promise<Trace | undefined> => {
34-
const lineMatch = line.match(FULL_MATCH);
35-
if (!lineMatch || lineMatch[0].includes("<anonymous>")) {
36-
return undefined;
37-
}
38-
39-
let functionName: string | undefined;
40-
let typeName: string | undefined;
41-
let methodName: string | undefined;
42-
let splitedPath: string[] | undefined;
43-
44-
if (lineMatch[1]) {
45-
functionName = lineMatch[1];
46-
}
47-
48-
if (functionName === undefined) {
49-
functionName = typeName ? `${typeName}.${methodName}` : "<anonymous>";
50-
}
51-
52-
const path = lineMatch[2]?.startsWith("file://")
53-
? lineMatch[2].substr(7)
54-
: lineMatch[2];
55-
const internal =
56-
path !== undefined &&
57-
!path.includes("node_modules/") &&
58-
!path.includes("node_modules\\") &&
59-
!path.includes("internal/");
60-
61-
const isNodeProcess = path?.includes("internal") || path?.includes("process");
62-
isNodeProcess
63-
? (splitedPath = path?.split("/"))
64-
: (splitedPath = path?.split("\\"));
65-
66-
const fileName = splitedPath[splitedPath?.length - 1];
67-
68-
const splitedFilename = fileName.split(".");
69-
const extension = splitedFilename[splitedFilename.length - 1];
70-
const lineNo = parseInt(lineMatch[3], 10);
71-
72-
const { code, preCode, postCode } = await getCodeFromFs(path, lineNo);
73-
74-
return {
75-
filename: fileName,
76-
function: functionName,
77-
absPath: path,
78-
lineNo,
79-
columnNo: parseInt(lineMatch[4], 10) || undefined,
80-
internal,
81-
extension,
82-
code,
83-
postCode,
84-
preCode,
85-
};
35+
const lineMatch = line.match(FULL_MATCH);
36+
if (!lineMatch || lineMatch[0].includes("<anonymous>")) {
37+
return undefined;
38+
}
39+
40+
let functionName: string | undefined;
41+
let typeName: string | undefined;
42+
let methodName: string | undefined;
43+
let splitedPath: string[] | undefined;
44+
45+
if (lineMatch[1]) {
46+
functionName = lineMatch[1];
47+
}
48+
49+
if (functionName === undefined) {
50+
functionName = typeName ? `${typeName}.${methodName}` : "<anonymous>";
51+
}
52+
53+
const path = lineMatch[2]?.startsWith("file://")
54+
? lineMatch[2].substr(7)
55+
: lineMatch[2];
56+
const internal =
57+
path !== undefined &&
58+
!path.includes("node_modules/") &&
59+
!path.includes("node_modules\\") &&
60+
!path.includes("internal/");
61+
62+
const isNodeProcess = path?.includes("internal") || path?.includes("process");
63+
isNodeProcess
64+
? (splitedPath = path?.split("/"))
65+
: (splitedPath = path?.split("\\"));
66+
67+
const fileName = splitedPath[splitedPath?.length - 1];
68+
69+
const splitedFilename = fileName.split(".");
70+
const extension = splitedFilename[splitedFilename.length - 1];
71+
const lineNo = parseInt(lineMatch[3], 10);
72+
73+
const { code, preCode, postCode } = await getCodeFromFs(path, lineNo);
74+
75+
return {
76+
filename: fileName,
77+
function: functionName,
78+
absPath: path,
79+
lineNo,
80+
columnNo: parseInt(lineMatch[4], 10) || undefined,
81+
internal,
82+
extension,
83+
code,
84+
postCode,
85+
preCode,
86+
};
8687
};
8788

8889
const getCodeFromFs = async (
89-
path: string,
90-
codeLine: number
90+
path: string,
91+
codeLine: number
9192
): Promise<{ code: string; preCode: string[]; postCode: string[] }> => {
92-
let code: string = "";
93-
let preCode: string[] = [];
94-
let postCode: string[] = [];
95-
let linesOfCode: string[] = [];
96-
97-
const context = await readFileAsync(path);
98-
99-
if (context) {
100-
linesOfCode = context?.split("\n");
101-
102-
code = linesOfCode[codeLine - 1];
103-
preCode = linesOfCode.slice(codeLine - 6, codeLine - 1);
104-
postCode = linesOfCode.slice(codeLine + 1, codeLine + 6);
105-
}
106-
107-
return {
108-
code,
109-
preCode,
110-
postCode,
111-
};
93+
let code: string = "";
94+
let preCode: string[] = [];
95+
let postCode: string[] = [];
96+
let linesOfCode: string[] = [];
97+
98+
const context = await readFileAsync(path);
99+
100+
if (context) {
101+
linesOfCode = context?.split("\n");
102+
103+
code = linesOfCode[codeLine - 1];
104+
preCode = linesOfCode.slice(codeLine - 6, codeLine - 1);
105+
postCode = linesOfCode.slice(codeLine + 1, codeLine + 6);
106+
}
107+
108+
return {
109+
code,
110+
preCode,
111+
postCode,
112+
};
112113
};
113114

114115
const readFileAsync = async (path: string): Promise<string> => {
115-
let context = "";
116+
let context = "";
116117

117-
//check if file with this path exist
118-
const isFileExists = existsSync(path);
119-
if (isFileExists) {
120-
context = await promises.readFile(path, "utf8");
121-
}
118+
//check if file with this path exist
119+
const isFileExists = existsSync(path);
120+
if (isFileExists) {
121+
context = await promises.readFile(path, "utf8");
122+
}
122123

123-
return context;
124+
return context;
124125
};
125126

126-
export const prepareException = async (error: KlepperError, options?: CatchExceptionsOptions, req?: KlepperIncomingMessage): Promise<KlepperEvent> => {
127-
const { message, name } = error;
128-
129-
const traces = await parseStackTraces(String(error?.stack));
130-
const event: KlepperEvent = {
131-
type: name,
132-
message,
133-
traces,
134-
stack: String(error.stack),
135-
options
136-
};
137-
138-
if (req !== undefined) {
139-
event.requestData = mapRequestData(req);
140-
}
141-
142-
return event;
143-
}
127+
export const prepareException = async (
128+
error: KlepperError,
129+
options?: CatchExceptionsOptions,
130+
req?: KlepperIncomingMessage
131+
): Promise<KlepperEvent> => {
132+
const { message, name } = error;
133+
134+
const platform = getOsPlatform();
135+
136+
const traces = await parseStackTraces(String(error?.stack));
137+
const event: KlepperEvent = {
138+
type: name,
139+
message,
140+
traces,
141+
stack: String(error.stack),
142+
options,
143+
platform
144+
};
145+
146+
if (req !== undefined) {
147+
event.requestData = mapRequestData(req);
148+
}
149+
150+
return event;
151+
};

src/node/sdk.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isClientConnected } from "../core/is";
44
import { KlepperReleaseEvent } from "../transport/events";
55
import { KlepperOptions } from "../transport/options";
66
import * as os from "os";
7+
import { getOsPlatform } from "./helpers";
78

89
const defaultBooleanCallback = () => true;
910

@@ -53,12 +54,7 @@ export const init = (
5354
const conn: KlepperReleaseEvent = {
5455
env: options?.environment,
5556
version: options?.version,
56-
os: {
57-
arch: os.arch(),
58-
platform: os.platform(),
59-
release: os.release(),
60-
version: os.version(),
61-
},
57+
os: getOsPlatform()
6258
};
6359

6460
sendConnection(conn);

src/transport/events.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ export interface KlepperEvent {
3737
sdk?: string;
3838
env?: Environment;
3939
version?: string;
40+
platform: Platform;
4041
}
4142

4243
export interface KlepperReleaseEvent {
4344
version?: string;
4445
env: Environment;
45-
os: {
46-
arch: string;
47-
platform: string;
48-
release: string;
49-
version: string;
50-
};
46+
os: Platform;
47+
}
48+
49+
export interface Platform {
50+
arch: string;
51+
platform: string;
52+
release: string;
53+
version: string;
5154
}

0 commit comments

Comments
 (0)