mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
refactor: sale estimates to nestjs
This commit is contained in:
14
src/errors/service-error.ts
Normal file
14
src/errors/service-error.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
19
src/filters/service-error.filter.ts
Normal file
19
src/filters/service-error.filter.ts
Normal 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
13
src/main.ts
Normal 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();
|
||||
Reference in New Issue
Block a user