Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling - exception filters #17

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
} from '@nestjs/common';

@Catch()
export class GlobalExceptionsFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();

let status = HttpStatus.INTERNAL_SERVER_ERROR;
let message: any = 'Internal Server Error';

if (exception instanceof HttpException) {
status = exception.getStatus();
message = exception.getResponse();
} else if (exception.response) {
status = exception.response.data.statusCode;
message = exception.response.data.message;
}

response.status(status).json(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { lastValueFrom } from 'rxjs';
import { InjectMapper } from '@automapper/nestjs';
Expand All @@ -16,12 +16,11 @@ import { GatewayConfig } from '../../common/interfaces/endpoint.interface';
import { ExternalApiName } from '../../common/enums/external-api-name';
import { EndpointName } from '../../common/enums/endpoint-name';
import { AxiosRequestConfig } from 'axios';
import {
RequestDataType
} from './reputation-oracle.interface';
import { RequestDataType } from './reputation-oracle.interface';
import {
SigninWorkerCommand,
SigninWorkerData, SigninWorkerResponse,
SigninWorkerData,
SigninWorkerResponse,
} from '../../modules/user-worker/interfaces/worker-signin.interface';

@Injectable()
Expand Down Expand Up @@ -57,14 +56,7 @@ export class ReputationOracleGateway {
const response = await lastValueFrom(this.httpService.request(options));
return response.data;
} catch (error) {
if (error.response) {
throw new HttpException(error.response.data, error.response.status);
} else {
throw new HttpException(
'Error occurred while redirecting request.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw error;
}
}
async sendWorkerSignup(command: SignupWorkerCommand): Promise<void> {
Expand Down
3 changes: 3 additions & 0 deletions packages/apps/human-app/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { Logger } from '@nestjs/common';
import { EnvironmentConfigService } from './common/config/environment-config.service';
import { GlobalExceptionsFilter } from './common/filter/global-exceptions.filter';

async function bootstrap() {
const logger = new Logger('bootstrap');
Expand All @@ -24,6 +25,8 @@ async function bootstrap() {
const host = envConfigService.host;
const port = envConfigService.port;

app.useGlobalFilters(new GlobalExceptionsFilter());

await app.listen(port, host, async () => {
logger.log(`Human APP server is running on http://${host}:${port}`);
});
Expand Down
Loading