Skip to content

Commit

Permalink
use global error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Oct 28, 2024
1 parent 7a9ef30 commit 8ef9e18
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"graphql-request": "^6.1.0",
"libphonenumber-js": "^1.11.12",
"rimraf": "^6.0.1",
"rxjs": "^7.8.1",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/athletes/athletes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class AthletesService {
}

async searchAthlete(name: string): Promise<AthleteSearchResult[] | null> {
try {
const data = await this.graphQLClient.request(ATHLETE_SEARCH_QUERY, {
name,
});
Expand All @@ -44,14 +43,9 @@ export class AthletesService {
return searchResults.sort(
(a, b) => a.levenshteinDistance - b.levenshteinDistance,
);
} catch (error) {
console.error(error);
}
return null;
}

async getAthlete(id: number): Promise<Athlete | null> {
try {
const data = await this.graphQLClient.request(
ATHLETE_QUERY,
{
Expand Down Expand Up @@ -195,9 +189,5 @@ export class AthletesService {
};
}),
};
} catch (error) {
console.error(error);
}
return null;
}
}
4 changes: 0 additions & 4 deletions src/athletes/results/result.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class ResultsService {
id: number,
year?: number,
): Promise<Performance[] | null> {
try {
const data = await this.graphQLClient.request(
RESULTS_QUERY,
{
Expand Down Expand Up @@ -75,8 +74,5 @@ export class ResultsService {
},
);
return results.filter((result) => result.place > 0);
} catch (error) {
console.log(error);
}
}
}
5 changes: 0 additions & 5 deletions src/countries/countries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class CountriesService {
}

async getCountries(): Promise<Country[]> {
try {
const data = await this.graphQLClient.request(COUNTRIES_QUERY);
const reponse = z
.object({
Expand All @@ -49,9 +48,5 @@ export class CountriesService {
countryName: country.countryName,
};
});
} catch (error) {
console.error(error);
}
return null;
}
}
5 changes: 0 additions & 5 deletions src/disciplines/disciplines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class DisciplinesService {
}

async findAll(): Promise<Discipline[]> {
try {
const data = await this.graphQLClient.request(COUNTRIES_QUERY);
const reponse = z
.object({
Expand All @@ -46,9 +45,5 @@ export class DisciplinesService {
shortTrack: isShortTrack(discipline.name),
};
});
} catch (error) {
console.error(error);
}
return null;
}
}
35 changes: 35 additions & 0 deletions src/error.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import {
Injectable,
NestInterceptor,
ExecutionContext,
BadGatewayException,
CallHandler,
InternalServerErrorException,
} from '@nestjs/common';
import { ClientError } from 'graphql-request';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ZodError } from 'zod';

@Injectable()
export class ErrorsInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
catchError(err => throwError(() => {
console.error(err);
if(err instanceof ClientError){
// GraphQL Request Error
return new BadGatewayException();
}
if(err instanceof ZodError){
// Schema Validation Error
return new BadGatewayException();
}
return new InternalServerErrorException();
}),
));
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ErrorsInterceptor } from './error.interceptor';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -13,6 +14,7 @@ async function bootstrap() {
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('swagger', app, document);
app.useGlobalInterceptors(new ErrorsInterceptor());
await app.listen(process.env.PORT || 3000);
}
bootstrap();
8 changes: 0 additions & 8 deletions src/records/records.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class RecordsService {

async find(category: number): Promise<Record[]> | null {
const records: Record[] = [];
try {
const data = await this.graphQLClient.request(RECORD_QUERY, {
categoryId: category,
});
Expand Down Expand Up @@ -104,15 +103,11 @@ export class RecordsService {
});
});
});
} catch (error) {
console.error(error);
}
return records;
}

async findCategories(): Promise<RecordCategory[]> {
const categories: RecordCategory[] = [];
try {
const data = await this.graphQLClient.request(RECORD_CATEGORIES_QUERY);
const response = z
.object({
Expand Down Expand Up @@ -148,9 +143,6 @@ export class RecordsService {
});
}
});
} catch (error) {
console.error(error);
}
return categories.sort((a, b) => a.id - b.id);
}
}

0 comments on commit 8ef9e18

Please sign in to comment.