@extn/segi-nestjs 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -7
- package/dist/index.d.mts +40 -5
- package/dist/index.d.ts +40 -5
- package/dist/index.js +110 -19
- package/dist/index.mjs +112 -20
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[NestJS](https://nestjs.com) integration for [segi](https://segi.extn.ai).
|
|
4
4
|
|
|
5
|
+
Supports **NestJS 10, 11 and 12** (`@nestjs/common` / `@nestjs/core`
|
|
6
|
+
`>=10.0.0 <13`) on both the **Express** and the **Fastify** adapter
|
|
7
|
+
(0.3.0+). Requires Node.js 18+ (global `fetch`).
|
|
8
|
+
|
|
5
9
|
## Install
|
|
6
10
|
|
|
7
11
|
```sh
|
|
12
|
+
npm install @extn/segi-nestjs
|
|
13
|
+
# or
|
|
8
14
|
pnpm add @extn/segi-nestjs
|
|
9
15
|
```
|
|
10
16
|
|
|
@@ -19,9 +25,10 @@ import { SegiModule } from "@extn/segi-nestjs";
|
|
|
19
25
|
@Module({
|
|
20
26
|
imports: [
|
|
21
27
|
SegiModule.forRoot({
|
|
22
|
-
|
|
28
|
+
projectKey: process.env.SEGI_PROJECT_KEY!,
|
|
29
|
+
enabled: process.env.NODE_ENV === "production", // optional, default true
|
|
23
30
|
environment: process.env.NODE_ENV,
|
|
24
|
-
release: process.env.
|
|
31
|
+
release: process.env.APP_VERSION,
|
|
25
32
|
}),
|
|
26
33
|
],
|
|
27
34
|
})
|
|
@@ -31,20 +38,125 @@ export class AppModule {}
|
|
|
31
38
|
Register the global filter in `main.ts`:
|
|
32
39
|
|
|
33
40
|
```ts
|
|
41
|
+
import { NestFactory } from "@nestjs/core";
|
|
42
|
+
import { AppModule } from "./app.module";
|
|
43
|
+
import { SegiExceptionFilter, SegiService } from "@extn/segi-nestjs";
|
|
44
|
+
|
|
45
|
+
async function bootstrap() {
|
|
46
|
+
const app = await NestFactory.create(AppModule);
|
|
47
|
+
app.useGlobalFilters(new SegiExceptionFilter(app.get(SegiService)));
|
|
48
|
+
await app.listen(process.env.PORT ?? 3000);
|
|
49
|
+
}
|
|
50
|
+
bootstrap();
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Or register it through DI instead of `main.ts` (pick one):
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { APP_FILTER } from "@nestjs/core";
|
|
34
57
|
import { SegiExceptionFilter } from "@extn/segi-nestjs";
|
|
35
58
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
59
|
+
@Module({
|
|
60
|
+
imports: [SegiModule.forRoot({ projectKey: process.env.SEGI_PROJECT_KEY! })],
|
|
61
|
+
providers: [{ provide: APP_FILTER, useClass: SegiExceptionFilter }],
|
|
62
|
+
})
|
|
63
|
+
export class AppModule {}
|
|
39
64
|
```
|
|
40
65
|
|
|
41
|
-
|
|
66
|
+
Fastify works the same way
|
|
67
|
+
(`NestFactory.create(AppModule, new FastifyAdapter())`).
|
|
68
|
+
|
|
69
|
+
The filter reports unhandled exceptions that are not an `HttpException`, and
|
|
70
|
+
`HttpException`s with status 5xx. 4xx `HttpException`s are not reported. After
|
|
71
|
+
reporting, it hands the exception to Nest's own `BaseExceptionFilter`, so the
|
|
72
|
+
HTTP response (status and body, e.g.
|
|
73
|
+
`{"statusCode":500,"message":"Internal server error"}`) is exactly what Nest
|
|
74
|
+
sends without the filter. Each reported event carries basic request context:
|
|
75
|
+
`request.method`, `request.url` (path only, query string removed),
|
|
76
|
+
`transaction` (e.g. `GET /users/:id`) and the user agent. Bodies, query values,
|
|
77
|
+
cookies and authorization headers are never read.
|
|
78
|
+
|
|
79
|
+
`enabled: false` turns every capture into a no-op; the filter still produces
|
|
80
|
+
Nest's normal responses.
|
|
81
|
+
|
|
82
|
+
## Without the SDK
|
|
83
|
+
|
|
84
|
+
If you cannot add the package, a minimal filter can post to the ingest API
|
|
85
|
+
directly. `message` is the only required field; send the project key in the
|
|
86
|
+
`X-Segi-Project-Key` header. The API answers `202`.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { ArgumentsHost, Catch, HttpException } from "@nestjs/common";
|
|
90
|
+
import { BaseExceptionFilter } from "@nestjs/core";
|
|
91
|
+
|
|
92
|
+
@Catch()
|
|
93
|
+
export class SegiFallbackFilter extends BaseExceptionFilter {
|
|
94
|
+
catch(exception: unknown, host: ArgumentsHost) {
|
|
95
|
+
const status = exception instanceof HttpException ? exception.getStatus() : 500;
|
|
96
|
+
if (status >= 500) {
|
|
97
|
+
const err = exception instanceof Error ? exception : new Error(String(exception));
|
|
98
|
+
const req = host.switchToHttp().getRequest();
|
|
99
|
+
void fetch("https://segiapi.extn.ai/api/ingest/events", {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
"X-Segi-Project-Key": process.env.SEGI_PROJECT_KEY!,
|
|
104
|
+
},
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
type: "error",
|
|
107
|
+
level: "error",
|
|
108
|
+
message: err.message,
|
|
109
|
+
errorName: err.name,
|
|
110
|
+
stack: err.stack,
|
|
111
|
+
platform: "nestjs",
|
|
112
|
+
runtime: "node",
|
|
113
|
+
environment: process.env.NODE_ENV,
|
|
114
|
+
request: { method: req?.method, url: String(req?.url ?? "").split("?")[0] },
|
|
115
|
+
timestamp: new Date().toISOString(),
|
|
116
|
+
}),
|
|
117
|
+
}).catch(() => undefined);
|
|
118
|
+
}
|
|
119
|
+
super.catch(exception, host);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// main.ts (inside bootstrap):
|
|
124
|
+
// const { httpAdapter } = app.get(HttpAdapterHost);
|
|
125
|
+
// app.useGlobalFilters(new SegiFallbackFilter(httpAdapter));
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Optional fields accepted by the API: `type` (`error` | `message` | `log`),
|
|
129
|
+
`level` (`debug` | `info` | `warning` | `error` | `fatal`), `errorName`,
|
|
130
|
+
`stack`, `platform`, `runtime`, `environment`, `release`, `transaction`, `url`,
|
|
131
|
+
`userAgent`, `user`, `tags`, `extra`, `breadcrumbs`, `request`, `timestamp`.
|
|
132
|
+
|
|
133
|
+
## Session recording
|
|
134
|
+
|
|
135
|
+
Session recording is a browser-side feature — wire it in with [`@extn/segi-js`](../segi-js) or [`@extn/segi-nextjs`](../segi-nextjs). The NestJS package is server-only and does not expose a recordings admin API.
|
|
136
|
+
|
|
137
|
+
## Server option support
|
|
138
|
+
|
|
139
|
+
`SegiModule.forRoot` uses `projectKey`, `enabled`, `environment`, `release`,
|
|
140
|
+
`endpoint` and (0.3.0+) `beforeSend` (return `null` to drop an event).
|
|
141
|
+
`maxBreadcrumbs` is browser-only. The second argument to
|
|
142
|
+
`captureException`/`captureMessage` is stored as `extra`; it does not override
|
|
143
|
+
level or user. `captureMessage` uses info. setUser, setTag and addBreadcrumb
|
|
144
|
+
belong to the browser SDK.
|
|
145
|
+
|
|
146
|
+
## Changes in 0.3.0
|
|
147
|
+
|
|
148
|
+
- Peer range widened to NestJS 10–12.
|
|
149
|
+
- The filter now delegates the response to Nest's `BaseExceptionFilter`:
|
|
150
|
+
requests no longer hang on Fastify, and Express responses keep Nest's
|
|
151
|
+
default body (including `statusCode`).
|
|
152
|
+
- `{ provide: APP_FILTER, useClass: SegiExceptionFilter }` now injects
|
|
153
|
+
`SegiService` correctly (earlier versions captured nothing in that style).
|
|
154
|
+
- Request context on events; `enabled` and `beforeSend` options.
|
|
42
155
|
|
|
43
156
|
## Docs
|
|
44
157
|
|
|
45
158
|
<https://segi.extn.ai/docs>
|
|
46
159
|
|
|
47
|
-
|
|
48
160
|
## Licence
|
|
49
161
|
|
|
50
162
|
Copyright (c) 2026 Extension Co., Ltd. All rights reserved. Use of this package
|
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,53 @@
|
|
|
1
1
|
import { ExceptionFilter, ArgumentsHost, DynamicModule } from '@nestjs/common';
|
|
2
|
-
import {
|
|
2
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
3
|
+
import { SegiClientOptions, SegiEventDto } from '@extn/segi-shared';
|
|
3
4
|
export { SegiClientOptions, SegiEventDto } from '@extn/segi-shared';
|
|
4
5
|
|
|
5
6
|
declare class SegiService {
|
|
6
7
|
private readonly options;
|
|
8
|
+
/**
|
|
9
|
+
* Resolved by Nest DI (InternalCoreModule exports it globally). Lets a
|
|
10
|
+
* filter built with `new SegiExceptionFilter(app.get(SegiService))`
|
|
11
|
+
* reply through the active HTTP adapter (Express or Fastify).
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
readonly httpAdapterHost?: HttpAdapterHost | undefined;
|
|
7
15
|
private readonly logger;
|
|
8
|
-
constructor(options: SegiClientOptions
|
|
9
|
-
|
|
16
|
+
constructor(options: SegiClientOptions,
|
|
17
|
+
/**
|
|
18
|
+
* Resolved by Nest DI (InternalCoreModule exports it globally). Lets a
|
|
19
|
+
* filter built with `new SegiExceptionFilter(app.get(SegiService))`
|
|
20
|
+
* reply through the active HTTP adapter (Express or Fastify).
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
httpAdapterHost?: HttpAdapterHost | undefined);
|
|
24
|
+
/** False when `SegiModule.forRoot({ enabled: false })` — every capture is a no-op. */
|
|
25
|
+
get enabled(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Report an exception. `context` is stored as `extra`. `fields` lets
|
|
28
|
+
* callers (e.g. SegiExceptionFilter) set top-level event fields such as
|
|
29
|
+
* `request` or `transaction`.
|
|
30
|
+
*/
|
|
31
|
+
captureException(exception: unknown, context?: Record<string, unknown>, fields?: Partial<SegiEventDto>): Promise<void>;
|
|
10
32
|
captureMessage(message: string, context?: Record<string, unknown>): Promise<void>;
|
|
11
33
|
private send;
|
|
12
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Global exception filter. Reports 5xx / non-HttpException errors to segi,
|
|
37
|
+
* then delegates to Nest's own BaseExceptionFilter so the HTTP response is
|
|
38
|
+
* exactly what Nest would have sent without this filter, on both the Express
|
|
39
|
+
* and the Fastify adapter.
|
|
40
|
+
*
|
|
41
|
+
* Both registration styles are supported:
|
|
42
|
+
* app.useGlobalFilters(new SegiExceptionFilter(app.get(SegiService)));
|
|
43
|
+
* { provide: APP_FILTER, useClass: SegiExceptionFilter }
|
|
44
|
+
*/
|
|
13
45
|
declare class SegiExceptionFilter implements ExceptionFilter {
|
|
14
|
-
private readonly segi
|
|
15
|
-
|
|
46
|
+
private readonly segi?;
|
|
47
|
+
private readonly httpAdapterHost?;
|
|
48
|
+
private delegate;
|
|
49
|
+
private delegateAdapter;
|
|
50
|
+
constructor(segi?: SegiService | undefined, httpAdapterHost?: HttpAdapterHost | undefined);
|
|
16
51
|
catch(exception: unknown, host: ArgumentsHost): void;
|
|
17
52
|
}
|
|
18
53
|
declare class SegiModule {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,53 @@
|
|
|
1
1
|
import { ExceptionFilter, ArgumentsHost, DynamicModule } from '@nestjs/common';
|
|
2
|
-
import {
|
|
2
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
3
|
+
import { SegiClientOptions, SegiEventDto } from '@extn/segi-shared';
|
|
3
4
|
export { SegiClientOptions, SegiEventDto } from '@extn/segi-shared';
|
|
4
5
|
|
|
5
6
|
declare class SegiService {
|
|
6
7
|
private readonly options;
|
|
8
|
+
/**
|
|
9
|
+
* Resolved by Nest DI (InternalCoreModule exports it globally). Lets a
|
|
10
|
+
* filter built with `new SegiExceptionFilter(app.get(SegiService))`
|
|
11
|
+
* reply through the active HTTP adapter (Express or Fastify).
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
readonly httpAdapterHost?: HttpAdapterHost | undefined;
|
|
7
15
|
private readonly logger;
|
|
8
|
-
constructor(options: SegiClientOptions
|
|
9
|
-
|
|
16
|
+
constructor(options: SegiClientOptions,
|
|
17
|
+
/**
|
|
18
|
+
* Resolved by Nest DI (InternalCoreModule exports it globally). Lets a
|
|
19
|
+
* filter built with `new SegiExceptionFilter(app.get(SegiService))`
|
|
20
|
+
* reply through the active HTTP adapter (Express or Fastify).
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
httpAdapterHost?: HttpAdapterHost | undefined);
|
|
24
|
+
/** False when `SegiModule.forRoot({ enabled: false })` — every capture is a no-op. */
|
|
25
|
+
get enabled(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Report an exception. `context` is stored as `extra`. `fields` lets
|
|
28
|
+
* callers (e.g. SegiExceptionFilter) set top-level event fields such as
|
|
29
|
+
* `request` or `transaction`.
|
|
30
|
+
*/
|
|
31
|
+
captureException(exception: unknown, context?: Record<string, unknown>, fields?: Partial<SegiEventDto>): Promise<void>;
|
|
10
32
|
captureMessage(message: string, context?: Record<string, unknown>): Promise<void>;
|
|
11
33
|
private send;
|
|
12
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Global exception filter. Reports 5xx / non-HttpException errors to segi,
|
|
37
|
+
* then delegates to Nest's own BaseExceptionFilter so the HTTP response is
|
|
38
|
+
* exactly what Nest would have sent without this filter, on both the Express
|
|
39
|
+
* and the Fastify adapter.
|
|
40
|
+
*
|
|
41
|
+
* Both registration styles are supported:
|
|
42
|
+
* app.useGlobalFilters(new SegiExceptionFilter(app.get(SegiService)));
|
|
43
|
+
* { provide: APP_FILTER, useClass: SegiExceptionFilter }
|
|
44
|
+
*/
|
|
13
45
|
declare class SegiExceptionFilter implements ExceptionFilter {
|
|
14
|
-
private readonly segi
|
|
15
|
-
|
|
46
|
+
private readonly segi?;
|
|
47
|
+
private readonly httpAdapterHost?;
|
|
48
|
+
private delegate;
|
|
49
|
+
private delegateAdapter;
|
|
50
|
+
constructor(segi?: SegiService | undefined, httpAdapterHost?: HttpAdapterHost | undefined);
|
|
16
51
|
catch(exception: unknown, host: ArgumentsHost): void;
|
|
17
52
|
}
|
|
18
53
|
declare class SegiModule {
|
package/dist/index.js
CHANGED
|
@@ -35,15 +35,36 @@ __export(index_exports, {
|
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
37
|
var import_common = require("@nestjs/common");
|
|
38
|
+
var import_core = require("@nestjs/core");
|
|
38
39
|
var import_segi_shared = require("@extn/segi-shared");
|
|
39
40
|
var SEGI_OPTIONS = "SEGI_OPTIONS";
|
|
41
|
+
function toError(exception) {
|
|
42
|
+
if (exception instanceof Error) return exception;
|
|
43
|
+
if (typeof exception === "string") return new Error(exception);
|
|
44
|
+
try {
|
|
45
|
+
return new Error(JSON.stringify(exception) ?? String(exception));
|
|
46
|
+
} catch {
|
|
47
|
+
return new Error(String(exception));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
40
50
|
var SegiService = class {
|
|
41
|
-
constructor(options) {
|
|
51
|
+
constructor(options, httpAdapterHost) {
|
|
42
52
|
this.options = options;
|
|
53
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
43
54
|
this.logger = new import_common.Logger("SegiService");
|
|
44
55
|
}
|
|
45
|
-
|
|
46
|
-
|
|
56
|
+
/** False when `SegiModule.forRoot({ enabled: false })` — every capture is a no-op. */
|
|
57
|
+
get enabled() {
|
|
58
|
+
return this.options.enabled !== false;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Report an exception. `context` is stored as `extra`. `fields` lets
|
|
62
|
+
* callers (e.g. SegiExceptionFilter) set top-level event fields such as
|
|
63
|
+
* `request` or `transaction`.
|
|
64
|
+
*/
|
|
65
|
+
async captureException(exception, context = {}, fields = {}) {
|
|
66
|
+
if (!this.enabled) return;
|
|
67
|
+
const err = toError(exception);
|
|
47
68
|
const event = {
|
|
48
69
|
type: "error",
|
|
49
70
|
level: "error",
|
|
@@ -54,12 +75,14 @@ var SegiService = class {
|
|
|
54
75
|
runtime: "node",
|
|
55
76
|
environment: this.options.environment,
|
|
56
77
|
release: this.options.release,
|
|
78
|
+
...fields,
|
|
57
79
|
extra: context,
|
|
58
80
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
59
81
|
};
|
|
60
82
|
await this.send(event);
|
|
61
83
|
}
|
|
62
84
|
async captureMessage(message, context = {}) {
|
|
85
|
+
if (!this.enabled) return;
|
|
63
86
|
const event = {
|
|
64
87
|
type: "message",
|
|
65
88
|
level: "info",
|
|
@@ -73,9 +96,18 @@ var SegiService = class {
|
|
|
73
96
|
};
|
|
74
97
|
await this.send(event);
|
|
75
98
|
}
|
|
76
|
-
async send(
|
|
99
|
+
async send(input) {
|
|
77
100
|
try {
|
|
78
|
-
if (typeof fetch !== "function") return;
|
|
101
|
+
if (!this.enabled || typeof fetch !== "function") return;
|
|
102
|
+
let event = input;
|
|
103
|
+
if (this.options.beforeSend) {
|
|
104
|
+
try {
|
|
105
|
+
event = this.options.beforeSend(input);
|
|
106
|
+
} catch {
|
|
107
|
+
event = input;
|
|
108
|
+
}
|
|
109
|
+
if (!event) return;
|
|
110
|
+
}
|
|
79
111
|
await fetch(this.options.endpoint || import_segi_shared.SEGI_DEFAULT_INGEST_ENDPOINT, {
|
|
80
112
|
method: "POST",
|
|
81
113
|
headers: {
|
|
@@ -91,32 +123,91 @@ var SegiService = class {
|
|
|
91
123
|
};
|
|
92
124
|
SegiService = __decorateClass([
|
|
93
125
|
(0, import_common.Injectable)(),
|
|
94
|
-
__decorateParam(0, (0, import_common.Inject)(SEGI_OPTIONS))
|
|
126
|
+
__decorateParam(0, (0, import_common.Inject)(SEGI_OPTIONS)),
|
|
127
|
+
__decorateParam(1, (0, import_common.Optional)()),
|
|
128
|
+
__decorateParam(1, (0, import_common.Inject)(import_core.HttpAdapterHost))
|
|
95
129
|
], SegiService);
|
|
130
|
+
function requestFields(req) {
|
|
131
|
+
try {
|
|
132
|
+
if (!req || typeof req !== "object") return {};
|
|
133
|
+
const r = req;
|
|
134
|
+
const method = typeof r.method === "string" ? r.method.toUpperCase() : void 0;
|
|
135
|
+
const rawUrl = r.originalUrl ?? r.raw?.originalUrl ?? r.url ?? r.raw?.url;
|
|
136
|
+
const url = typeof rawUrl === "string" ? rawUrl.split("?")[0] : void 0;
|
|
137
|
+
const route = typeof r.route?.path === "string" ? r.route.path : typeof r.routeOptions?.url === "string" ? r.routeOptions.url : typeof r.routerPath === "string" ? r.routerPath : void 0;
|
|
138
|
+
const ua = r.headers?.["user-agent"];
|
|
139
|
+
const request = {};
|
|
140
|
+
if (method) request.method = method;
|
|
141
|
+
if (url) request.url = url;
|
|
142
|
+
const out = {};
|
|
143
|
+
if (request.method || request.url) out.request = request;
|
|
144
|
+
if (route) out.transaction = method ? `${method} ${route}` : route;
|
|
145
|
+
if (typeof ua === "string") out.userAgent = ua.slice(0, 512);
|
|
146
|
+
return out;
|
|
147
|
+
} catch {
|
|
148
|
+
return {};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
96
151
|
var SegiExceptionFilter = class {
|
|
97
|
-
|
|
152
|
+
// Explicit @Inject tokens: the package is built with esbuild, which does
|
|
153
|
+
// not emit `design:paramtypes`, so type-based DI would silently inject
|
|
154
|
+
// nothing (that is why APP_FILTER registration never captured in <=0.2.0).
|
|
155
|
+
constructor(segi, httpAdapterHost) {
|
|
98
156
|
this.segi = segi;
|
|
157
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
158
|
+
this.delegate = null;
|
|
159
|
+
this.delegateAdapter = null;
|
|
99
160
|
}
|
|
100
161
|
catch(exception, host) {
|
|
101
162
|
const status = exception instanceof import_common.HttpException ? exception.getStatus() : 500;
|
|
102
|
-
|
|
103
|
-
|
|
163
|
+
const isHttp = host.getType() === "http";
|
|
164
|
+
if (status >= 500 && this.segi) {
|
|
165
|
+
const fields = isHttp ? requestFields(host.switchToHttp().getRequest()) : {};
|
|
166
|
+
void this.segi.captureException(exception, {}, fields);
|
|
104
167
|
}
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
168
|
+
if (!isHttp) return;
|
|
169
|
+
const adapter = this.httpAdapterHost?.httpAdapter ?? this.segi?.httpAdapterHost?.httpAdapter;
|
|
170
|
+
if (adapter) {
|
|
171
|
+
if (!this.delegate || this.delegateAdapter !== adapter) {
|
|
172
|
+
this.delegate = new import_core.BaseExceptionFilter(adapter);
|
|
173
|
+
this.delegateAdapter = adapter;
|
|
174
|
+
}
|
|
175
|
+
return this.delegate.catch(exception, host);
|
|
111
176
|
}
|
|
112
|
-
|
|
113
|
-
const res = ctx.getResponse();
|
|
114
|
-
res?.status?.(500).json?.({ message: "Internal server error" });
|
|
177
|
+
replyLikeNest(exception, host);
|
|
115
178
|
}
|
|
116
179
|
};
|
|
117
180
|
SegiExceptionFilter = __decorateClass([
|
|
118
|
-
(0, import_common.Catch)()
|
|
181
|
+
(0, import_common.Catch)(),
|
|
182
|
+
__decorateParam(0, (0, import_common.Optional)()),
|
|
183
|
+
__decorateParam(0, (0, import_common.Inject)(SegiService)),
|
|
184
|
+
__decorateParam(1, (0, import_common.Optional)()),
|
|
185
|
+
__decorateParam(1, (0, import_common.Inject)(import_core.HttpAdapterHost))
|
|
119
186
|
], SegiExceptionFilter);
|
|
187
|
+
function replyLikeNest(exception, host) {
|
|
188
|
+
try {
|
|
189
|
+
const res = host.switchToHttp().getResponse();
|
|
190
|
+
let status = 500;
|
|
191
|
+
let body = { statusCode: 500, message: "Internal server error" };
|
|
192
|
+
if (exception instanceof import_common.HttpException) {
|
|
193
|
+
status = exception.getStatus();
|
|
194
|
+
const r = exception.getResponse();
|
|
195
|
+
body = typeof r === "string" ? { statusCode: status, message: r } : r;
|
|
196
|
+
} else {
|
|
197
|
+
new import_common.Logger("ExceptionsHandler").error(
|
|
198
|
+
exception instanceof Error ? exception.message : String(exception),
|
|
199
|
+
exception instanceof Error ? exception.stack : void 0
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (res?.headersSent || res?.sent) return;
|
|
203
|
+
if (typeof res?.code === "function" && typeof res?.send === "function") {
|
|
204
|
+
res.code(status).send(body);
|
|
205
|
+
} else if (typeof res?.status === "function") {
|
|
206
|
+
res.status(status).json(body);
|
|
207
|
+
}
|
|
208
|
+
} catch {
|
|
209
|
+
}
|
|
210
|
+
}
|
|
120
211
|
var SegiModule = class {
|
|
121
212
|
static forRoot(options) {
|
|
122
213
|
return {
|
package/dist/index.mjs
CHANGED
|
@@ -17,19 +17,41 @@ import {
|
|
|
17
17
|
Inject,
|
|
18
18
|
Injectable,
|
|
19
19
|
Logger,
|
|
20
|
-
Module
|
|
20
|
+
Module,
|
|
21
|
+
Optional
|
|
21
22
|
} from "@nestjs/common";
|
|
23
|
+
import { BaseExceptionFilter, HttpAdapterHost } from "@nestjs/core";
|
|
22
24
|
import {
|
|
23
25
|
SEGI_DEFAULT_INGEST_ENDPOINT
|
|
24
26
|
} from "@extn/segi-shared";
|
|
25
27
|
var SEGI_OPTIONS = "SEGI_OPTIONS";
|
|
28
|
+
function toError(exception) {
|
|
29
|
+
if (exception instanceof Error) return exception;
|
|
30
|
+
if (typeof exception === "string") return new Error(exception);
|
|
31
|
+
try {
|
|
32
|
+
return new Error(JSON.stringify(exception) ?? String(exception));
|
|
33
|
+
} catch {
|
|
34
|
+
return new Error(String(exception));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
26
37
|
var SegiService = class {
|
|
27
|
-
constructor(options) {
|
|
38
|
+
constructor(options, httpAdapterHost) {
|
|
28
39
|
this.options = options;
|
|
40
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
29
41
|
this.logger = new Logger("SegiService");
|
|
30
42
|
}
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
/** False when `SegiModule.forRoot({ enabled: false })` — every capture is a no-op. */
|
|
44
|
+
get enabled() {
|
|
45
|
+
return this.options.enabled !== false;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Report an exception. `context` is stored as `extra`. `fields` lets
|
|
49
|
+
* callers (e.g. SegiExceptionFilter) set top-level event fields such as
|
|
50
|
+
* `request` or `transaction`.
|
|
51
|
+
*/
|
|
52
|
+
async captureException(exception, context = {}, fields = {}) {
|
|
53
|
+
if (!this.enabled) return;
|
|
54
|
+
const err = toError(exception);
|
|
33
55
|
const event = {
|
|
34
56
|
type: "error",
|
|
35
57
|
level: "error",
|
|
@@ -40,12 +62,14 @@ var SegiService = class {
|
|
|
40
62
|
runtime: "node",
|
|
41
63
|
environment: this.options.environment,
|
|
42
64
|
release: this.options.release,
|
|
65
|
+
...fields,
|
|
43
66
|
extra: context,
|
|
44
67
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
45
68
|
};
|
|
46
69
|
await this.send(event);
|
|
47
70
|
}
|
|
48
71
|
async captureMessage(message, context = {}) {
|
|
72
|
+
if (!this.enabled) return;
|
|
49
73
|
const event = {
|
|
50
74
|
type: "message",
|
|
51
75
|
level: "info",
|
|
@@ -59,9 +83,18 @@ var SegiService = class {
|
|
|
59
83
|
};
|
|
60
84
|
await this.send(event);
|
|
61
85
|
}
|
|
62
|
-
async send(
|
|
86
|
+
async send(input) {
|
|
63
87
|
try {
|
|
64
|
-
if (typeof fetch !== "function") return;
|
|
88
|
+
if (!this.enabled || typeof fetch !== "function") return;
|
|
89
|
+
let event = input;
|
|
90
|
+
if (this.options.beforeSend) {
|
|
91
|
+
try {
|
|
92
|
+
event = this.options.beforeSend(input);
|
|
93
|
+
} catch {
|
|
94
|
+
event = input;
|
|
95
|
+
}
|
|
96
|
+
if (!event) return;
|
|
97
|
+
}
|
|
65
98
|
await fetch(this.options.endpoint || SEGI_DEFAULT_INGEST_ENDPOINT, {
|
|
66
99
|
method: "POST",
|
|
67
100
|
headers: {
|
|
@@ -77,32 +110,91 @@ var SegiService = class {
|
|
|
77
110
|
};
|
|
78
111
|
SegiService = __decorateClass([
|
|
79
112
|
Injectable(),
|
|
80
|
-
__decorateParam(0, Inject(SEGI_OPTIONS))
|
|
113
|
+
__decorateParam(0, Inject(SEGI_OPTIONS)),
|
|
114
|
+
__decorateParam(1, Optional()),
|
|
115
|
+
__decorateParam(1, Inject(HttpAdapterHost))
|
|
81
116
|
], SegiService);
|
|
117
|
+
function requestFields(req) {
|
|
118
|
+
try {
|
|
119
|
+
if (!req || typeof req !== "object") return {};
|
|
120
|
+
const r = req;
|
|
121
|
+
const method = typeof r.method === "string" ? r.method.toUpperCase() : void 0;
|
|
122
|
+
const rawUrl = r.originalUrl ?? r.raw?.originalUrl ?? r.url ?? r.raw?.url;
|
|
123
|
+
const url = typeof rawUrl === "string" ? rawUrl.split("?")[0] : void 0;
|
|
124
|
+
const route = typeof r.route?.path === "string" ? r.route.path : typeof r.routeOptions?.url === "string" ? r.routeOptions.url : typeof r.routerPath === "string" ? r.routerPath : void 0;
|
|
125
|
+
const ua = r.headers?.["user-agent"];
|
|
126
|
+
const request = {};
|
|
127
|
+
if (method) request.method = method;
|
|
128
|
+
if (url) request.url = url;
|
|
129
|
+
const out = {};
|
|
130
|
+
if (request.method || request.url) out.request = request;
|
|
131
|
+
if (route) out.transaction = method ? `${method} ${route}` : route;
|
|
132
|
+
if (typeof ua === "string") out.userAgent = ua.slice(0, 512);
|
|
133
|
+
return out;
|
|
134
|
+
} catch {
|
|
135
|
+
return {};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
82
138
|
var SegiExceptionFilter = class {
|
|
83
|
-
|
|
139
|
+
// Explicit @Inject tokens: the package is built with esbuild, which does
|
|
140
|
+
// not emit `design:paramtypes`, so type-based DI would silently inject
|
|
141
|
+
// nothing (that is why APP_FILTER registration never captured in <=0.2.0).
|
|
142
|
+
constructor(segi, httpAdapterHost) {
|
|
84
143
|
this.segi = segi;
|
|
144
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
145
|
+
this.delegate = null;
|
|
146
|
+
this.delegateAdapter = null;
|
|
85
147
|
}
|
|
86
148
|
catch(exception, host) {
|
|
87
149
|
const status = exception instanceof HttpException ? exception.getStatus() : 500;
|
|
88
|
-
|
|
89
|
-
|
|
150
|
+
const isHttp = host.getType() === "http";
|
|
151
|
+
if (status >= 500 && this.segi) {
|
|
152
|
+
const fields = isHttp ? requestFields(host.switchToHttp().getRequest()) : {};
|
|
153
|
+
void this.segi.captureException(exception, {}, fields);
|
|
90
154
|
}
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
155
|
+
if (!isHttp) return;
|
|
156
|
+
const adapter = this.httpAdapterHost?.httpAdapter ?? this.segi?.httpAdapterHost?.httpAdapter;
|
|
157
|
+
if (adapter) {
|
|
158
|
+
if (!this.delegate || this.delegateAdapter !== adapter) {
|
|
159
|
+
this.delegate = new BaseExceptionFilter(adapter);
|
|
160
|
+
this.delegateAdapter = adapter;
|
|
161
|
+
}
|
|
162
|
+
return this.delegate.catch(exception, host);
|
|
97
163
|
}
|
|
98
|
-
|
|
99
|
-
const res = ctx.getResponse();
|
|
100
|
-
res?.status?.(500).json?.({ message: "Internal server error" });
|
|
164
|
+
replyLikeNest(exception, host);
|
|
101
165
|
}
|
|
102
166
|
};
|
|
103
167
|
SegiExceptionFilter = __decorateClass([
|
|
104
|
-
Catch()
|
|
168
|
+
Catch(),
|
|
169
|
+
__decorateParam(0, Optional()),
|
|
170
|
+
__decorateParam(0, Inject(SegiService)),
|
|
171
|
+
__decorateParam(1, Optional()),
|
|
172
|
+
__decorateParam(1, Inject(HttpAdapterHost))
|
|
105
173
|
], SegiExceptionFilter);
|
|
174
|
+
function replyLikeNest(exception, host) {
|
|
175
|
+
try {
|
|
176
|
+
const res = host.switchToHttp().getResponse();
|
|
177
|
+
let status = 500;
|
|
178
|
+
let body = { statusCode: 500, message: "Internal server error" };
|
|
179
|
+
if (exception instanceof HttpException) {
|
|
180
|
+
status = exception.getStatus();
|
|
181
|
+
const r = exception.getResponse();
|
|
182
|
+
body = typeof r === "string" ? { statusCode: status, message: r } : r;
|
|
183
|
+
} else {
|
|
184
|
+
new Logger("ExceptionsHandler").error(
|
|
185
|
+
exception instanceof Error ? exception.message : String(exception),
|
|
186
|
+
exception instanceof Error ? exception.stack : void 0
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (res?.headersSent || res?.sent) return;
|
|
190
|
+
if (typeof res?.code === "function" && typeof res?.send === "function") {
|
|
191
|
+
res.code(status).send(body);
|
|
192
|
+
} else if (typeof res?.status === "function") {
|
|
193
|
+
res.status(status).json(body);
|
|
194
|
+
}
|
|
195
|
+
} catch {
|
|
196
|
+
}
|
|
197
|
+
}
|
|
106
198
|
var SegiModule = class {
|
|
107
199
|
static forRoot(options) {
|
|
108
200
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@extn/segi-nestjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "NestJS integration for the segi error monitoring service",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@nestjs/common": "
|
|
26
|
-
"@nestjs/core": "
|
|
25
|
+
"@nestjs/common": ">=10.0.0 <13",
|
|
26
|
+
"@nestjs/core": ">=10.0.0 <13"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@extn/segi-shared": "0.
|
|
29
|
+
"@extn/segi-shared": "0.2.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@nestjs/common": "^10.4.0",
|