mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-15 10:01:08 +02:00
Infrastructure: - Add GlitchTip (web + worker) to docker-compose.macmini.yml (port 8020) - Add glitchtip.mana.how to Cloudflare Tunnel config - Add glitchtip database to init-db SQL - Add GLITCHTIP_DSN to .env.development Shared Package (@manacore/shared-error-tracking): - initErrorTracking() - Sentry-compatible init with GlitchTip DSN - captureException(), captureMessage(), setUser(), setTag(), flush() - SentryExceptionFilter for NestJS (captures 5xx errors only) - Graceful no-op when DSN is not configured Integration: - Add instrument.ts to calendar, contacts, todo backends - Import instrument.ts before app bootstrap in all 3 main.ts files - Error tracking auto-initializes when GLITCHTIP_DSN env var is set Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
/**
|
|
* NestJS-specific error tracking integration
|
|
*
|
|
* Provides an exception filter that automatically captures errors
|
|
* and a middleware that sets user context from JWT.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* // app.module.ts
|
|
* import { SentryExceptionFilter } from '@manacore/shared-error-tracking/nestjs';
|
|
*
|
|
* @Module({
|
|
* providers: [
|
|
* { provide: APP_FILTER, useClass: SentryExceptionFilter },
|
|
* ],
|
|
* })
|
|
* ```
|
|
*/
|
|
|
|
import {
|
|
type ExceptionFilter,
|
|
Catch,
|
|
type ArgumentsHost,
|
|
HttpException,
|
|
HttpStatus,
|
|
Logger,
|
|
} from '@nestjs/common';
|
|
import { captureException, setUser } from './index';
|
|
|
|
/**
|
|
* NestJS exception filter that captures errors to GlitchTip/Sentry.
|
|
* Use alongside your existing HttpExceptionFilter or as a replacement.
|
|
*/
|
|
@Catch()
|
|
export class SentryExceptionFilter implements ExceptionFilter {
|
|
private readonly logger = new Logger(SentryExceptionFilter.name);
|
|
|
|
catch(exception: unknown, host: ArgumentsHost) {
|
|
const ctx = host.switchToHttp();
|
|
const request = ctx.getRequest();
|
|
const response = ctx.getResponse();
|
|
|
|
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
let message = 'Internal server error';
|
|
|
|
if (exception instanceof HttpException) {
|
|
status = exception.getStatus();
|
|
const exceptionResponse = exception.getResponse();
|
|
message =
|
|
typeof exceptionResponse === 'string'
|
|
? exceptionResponse
|
|
: (exceptionResponse as Record<string, unknown>).message?.toString() || message;
|
|
}
|
|
|
|
// Only capture 5xx errors to GlitchTip (not 4xx client errors)
|
|
if (status >= 500) {
|
|
captureException(exception, {
|
|
url: request.url,
|
|
method: request.method,
|
|
statusCode: status,
|
|
userId: request.user?.userId || request.user?.sub,
|
|
});
|
|
this.logger.error(
|
|
`[${request.method}] ${request.url} - ${status}: ${message}`,
|
|
exception instanceof Error ? exception.stack : undefined
|
|
);
|
|
} else {
|
|
this.logger.warn(`[${request.method}] ${request.url} - ${status}: ${message}`);
|
|
}
|
|
|
|
response.status(status).json({
|
|
statusCode: status,
|
|
message,
|
|
timestamp: new Date().toISOString(),
|
|
path: request.url,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set Sentry user context from a NestJS request.
|
|
* Call this in a middleware or interceptor after JWT validation.
|
|
*/
|
|
export function setUserFromRequest(request: {
|
|
user?: { userId?: string; sub?: string; email?: string };
|
|
}): void {
|
|
if (request.user) {
|
|
setUser({
|
|
id: request.user.userId || request.user.sub || 'unknown',
|
|
email: request.user.email,
|
|
});
|
|
}
|
|
}
|