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);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,41 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Controller, Get, HttpCode, Res } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { HealthService } from './health.service';
|
||||
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(private readonly healthService: HealthService) {}
|
||||
|
||||
/**
|
||||
* Övergripande hälsostatus för tjänsten
|
||||
* Returnerar 200 om allt är bra, 503 om något är fel
|
||||
*/
|
||||
@Get()
|
||||
getHealth() {
|
||||
return {
|
||||
status: 'ok',
|
||||
service: 'recipe-api',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
async getHealth(@Res() res: Response) {
|
||||
const health = await this.healthService.getOverallHealth();
|
||||
res.status(health.statusCode).json({
|
||||
status: health.status,
|
||||
service: health.service,
|
||||
timestamp: health.timestamp,
|
||||
uptime: health.uptime,
|
||||
checks: health.checks,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Databasspecific hälsokontroll
|
||||
* Returnerar 200 om databasen är tillgänglig, 503 om inte
|
||||
*/
|
||||
@Get('db')
|
||||
async getDatabaseHealth() {
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
database: 'connected',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'error',
|
||||
database: 'not reachable',
|
||||
message: error instanceof Error ? error.message : 'unknown error',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
async getDatabaseHealth(@Res() res: Response) {
|
||||
const dbHealth = await this.healthService.getDatabaseHealth();
|
||||
res.status(dbHealth.statusCode).json({
|
||||
status: dbHealth.status,
|
||||
database: dbHealth.database,
|
||||
responseTime: `${dbHealth.responseTime}ms`,
|
||||
timestamp: dbHealth.timestamp,
|
||||
details: dbHealth.details,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './health.controller';
|
||||
import { HealthService } from './health.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [HealthController],
|
||||
providers: [HealthService],
|
||||
})
|
||||
export class HealthModule {}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
export interface HealthStatus {
|
||||
status: 'healthy' | 'degraded' | 'unhealthy';
|
||||
service: string;
|
||||
timestamp: string;
|
||||
uptime: number;
|
||||
checks: {
|
||||
database: {
|
||||
status: 'ok' | 'error';
|
||||
responseTime: number;
|
||||
details?: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class HealthService {
|
||||
private readonly startTime = Date.now();
|
||||
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getOverallHealth(): Promise<{
|
||||
status: 'healthy' | 'degraded' | 'unhealthy';
|
||||
statusCode: number;
|
||||
service: string;
|
||||
timestamp: string;
|
||||
uptime: number;
|
||||
checks: {
|
||||
database: {
|
||||
status: 'ok' | 'error';
|
||||
responseTime: number;
|
||||
details?: string;
|
||||
};
|
||||
};
|
||||
}> {
|
||||
const timestamp = new Date().toISOString();
|
||||
const uptime = Date.now() - this.startTime;
|
||||
|
||||
// Testa databaskopplingen
|
||||
const dbStart = Date.now();
|
||||
let dbStatus: 'ok' | 'error' = 'ok';
|
||||
let dbResponseTime = 0;
|
||||
let dbDetails: string | undefined;
|
||||
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
dbResponseTime = Date.now() - dbStart;
|
||||
} catch (error) {
|
||||
dbStatus = 'error';
|
||||
dbResponseTime = Date.now() - dbStart;
|
||||
dbDetails = error instanceof Error ? error.message : 'Unknown database error';
|
||||
}
|
||||
|
||||
// Bestäm övergripande hälsa
|
||||
let overallStatus: 'healthy' | 'degraded' | 'unhealthy' = 'healthy';
|
||||
if (dbStatus === 'error') {
|
||||
overallStatus = 'unhealthy';
|
||||
}
|
||||
|
||||
const statusCode = overallStatus === 'unhealthy' ? 503 : 200;
|
||||
|
||||
return {
|
||||
status: overallStatus,
|
||||
statusCode,
|
||||
service: 'recipe-api',
|
||||
timestamp,
|
||||
uptime,
|
||||
checks: {
|
||||
database: {
|
||||
status: dbStatus,
|
||||
responseTime: dbResponseTime,
|
||||
details: dbDetails,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async getDatabaseHealth(): Promise<{
|
||||
status: 'ok' | 'error';
|
||||
database: string;
|
||||
responseTime: number;
|
||||
timestamp: string;
|
||||
details?: string;
|
||||
statusCode: number;
|
||||
}> {
|
||||
const timestamp = new Date().toISOString();
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
database: 'connected',
|
||||
responseTime,
|
||||
timestamp,
|
||||
statusCode: 200,
|
||||
};
|
||||
} catch (error) {
|
||||
const responseTime = Date.now() - startTime;
|
||||
const details = error instanceof Error ? error.message : 'Unknown database error';
|
||||
|
||||
return {
|
||||
status: 'error',
|
||||
database: 'not reachable',
|
||||
responseTime,
|
||||
timestamp,
|
||||
details,
|
||||
statusCode: 503,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { GlobalExceptionFilter } from './common/filters/global-exception.filter';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
app.setGlobalPrefix('api');
|
||||
|
||||
// Registrera global exception filter
|
||||
app.useGlobalFilters(new GlobalExceptionFilter());
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
|
||||
Reference in New Issue
Block a user