Implement health check service and global exception handling
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
|
||||
interface ErrorResponse {
|
||||
statusCode: number;
|
||||
message: string;
|
||||
timestamp: string;
|
||||
path?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Global exception filter som formaterar alla errors konsistent
|
||||
* Returnerar tydliga, svenska felmeddelanden utan att läcka känslig info
|
||||
*/
|
||||
@Catch()
|
||||
export class GlobalExceptionFilter implements ExceptionFilter {
|
||||
private readonly logger = new Logger(GlobalExceptionFilter.name);
|
||||
|
||||
catch(exception: any, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const request = ctx.getRequest();
|
||||
const path = request.url;
|
||||
|
||||
let statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
let message = 'Ett internt serverfel inträffade.';
|
||||
|
||||
// Hantera HTTP-exceptions (t.ex. NotFoundException)
|
||||
if (exception instanceof HttpException) {
|
||||
statusCode = exception.getStatus();
|
||||
const exceptionResponse = exception.getResponse();
|
||||
|
||||
if (typeof exceptionResponse === 'object' && 'message' in exceptionResponse) {
|
||||
message = (exceptionResponse as any).message || message;
|
||||
} else if (typeof exceptionResponse === 'string') {
|
||||
message = exceptionResponse;
|
||||
}
|
||||
} else if (exception instanceof Error) {
|
||||
// Hantera vanliga Error-instanser
|
||||
message = exception.message || message;
|
||||
statusCode = HttpStatus.BAD_REQUEST;
|
||||
|
||||
// Log interna fel för debugging
|
||||
this.logger.error(`Error: ${exception.message}`, exception.stack);
|
||||
}
|
||||
|
||||
// Säkerställ att felmeddelanden är användarvänliga (på svenska)
|
||||
if (message.includes('Unknown unit')) {
|
||||
message = 'Okänd enhet. Kontrollera enheten och försök igen.';
|
||||
} else if (message.includes('Cannot convert between incompatible unit types')) {
|
||||
message = 'Kan inte konvertera mellan dessa enhetstyper.';
|
||||
} else if (message.includes('Inventory item with id') && message.includes('not found')) {
|
||||
message = 'Hemmavarorna hittades inte.';
|
||||
} else if (message.includes('Product with id') && message.includes('not found')) {
|
||||
message = 'Produkten hittades inte.';
|
||||
} else if (message.includes('Recipe with id') && message.includes('not found')) {
|
||||
message = 'Receptet hittades inte.';
|
||||
} else if (message.includes('sourceProductId och targetProductId kan inte vara samma')) {
|
||||
message = 'Du kan inte slå samman en produkt med sig själv.';
|
||||
}
|
||||
|
||||
const errorResponse: ErrorResponse = {
|
||||
statusCode,
|
||||
message,
|
||||
timestamp: new Date().toISOString(),
|
||||
path,
|
||||
};
|
||||
|
||||
this.logger.warn(
|
||||
`${request.method} ${path} - Status: ${statusCode}, Message: ${message}`,
|
||||
);
|
||||
|
||||
response.status(statusCode).json(errorResponse);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user