fix(#412): wrap BetterAuth handler in try/catch with error logging
All checks were successful
ci/woodpecker/push/api Pipeline was successful

Refs #412

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-16 11:08:47 -06:00
parent 976d14d94b
commit 9ae21c4c15
2 changed files with 103 additions and 3 deletions

View File

@@ -1,4 +1,15 @@
import { Controller, All, Req, Res, Get, UseGuards, Request, Logger } from "@nestjs/common";
import {
Controller,
All,
Req,
Res,
Get,
UseGuards,
Request,
Logger,
HttpException,
HttpStatus,
} from "@nestjs/common";
import { Throttle } from "@nestjs/throttler";
import type { Request as ExpressRequest, Response as ExpressResponse } from "express";
import type { AuthUser, AuthSession } from "@mosaic/shared";
@@ -105,7 +116,26 @@ export class AuthController {
this.logger.debug(`Auth catch-all: ${req.method} ${req.url} from ${clientIp}`);
const handler = this.authService.getNodeHandler();
return handler(req, res);
try {
await handler(req, res);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
const stack = error instanceof Error ? error.stack : undefined;
this.logger.error(
`BetterAuth handler error: ${req.method} ${req.url} from ${clientIp} - ${message}`,
stack
);
if (!res.headersSent) {
throw new HttpException("Internal auth error", HttpStatus.INTERNAL_SERVER_ERROR);
}
this.logger.warn(
`Cannot send error response for ${req.method} ${req.url} - headers already sent`
);
}
}
/**