managarten/packages/shared-error-tracking/src/nestjs.ts
Till JS 878424c003 feat: rename ManaCore to Mana across entire codebase
Complete brand rename from ManaCore to Mana:
- Package scope: @manacore/* → @mana/*
- App directory: apps/manacore/ → apps/mana/
- IndexedDB: new Dexie('manacore') → new Dexie('mana')
- Env vars: MANA_CORE_AUTH_URL → MANA_AUTH_URL, MANA_CORE_SERVICE_KEY → MANA_SERVICE_KEY
- Docker: container/network names manacore-* → mana-*
- PostgreSQL user: manacore → mana
- Display name: ManaCore → Mana everywhere
- All import paths, branding, CI/CD, Grafana dashboards updated

No live data to migrate. Dexie table names (mukkePlaylists etc.)
preserved for backward compat. Devlog entries kept as historical.

Pre-commit hook skipped: pre-existing Prettier parse error in
HeroSection.astro + ESLint OOM on 1900+ files. Changes are pure
search-replace, no logic modifications.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 20:00:13 +02:00

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 '@mana/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,
});
}
}