refactor: sale estimates to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-22 14:16:01 +02:00
parent 8a12caf48d
commit 336171081e
69 changed files with 4998 additions and 497 deletions

View File

@@ -0,0 +1,14 @@
import { HttpStatus } from '@nestjs/common';
export class ServiceError extends Error {
constructor(
public message: string,
private status: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR,
) {
super(message);
}
getStatus(): HttpStatus {
return this.status;
}
}

View File

@@ -0,0 +1,19 @@
import { ExceptionFilter, Catch, ArgumentsHost, HttpStatus } from '@nestjs/common';
import { Response } from 'express';
import { ServiceError } from '../errors/service-error';
@Catch(ServiceError)
export class ServiceErrorFilter implements ExceptionFilter {
catch(exception: ServiceError, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const status = exception.getStatus();
response
.status(status)
.json({
statusCode: status,
message: exception.message,
});
}
}

13
src/main.ts Normal file
View File

@@ -0,0 +1,13 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ServiceErrorFilter } from './filters/service-error.filter';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Register the ServiceErrorFilter globally
app.useGlobalFilters(new ServiceErrorFilter());
await app.listen(3000);
}
bootstrap();