Skip to content

Commit

Permalink
Merge branch 'main' of github.com:domingo1021/hero-project
Browse files Browse the repository at this point in the history
  • Loading branch information
domingo1021 committed Jun 30, 2024
2 parents 907f647 + b484223 commit 0b3ee2f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cores/types/errorResnpose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum CustomErrorCodes {
UNAUTHORIZED = 40100,
FORBIDDEN = 40300,
NOT_FOUND = 40400,
HERO_NOT_FOUND = 40401,
INTERNAL_SERVER_ERROR = 50000,
THIRDPARTY_SERVER_ERROR = 50001,
THIRDPARTY_API_RESPONSE_MISMATCH = 50002,
Expand Down
15 changes: 14 additions & 1 deletion src/modules/http/http.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { HttpStatus, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';

import { AxiosError } from 'axios';
Expand All @@ -15,6 +15,7 @@ export class ExternalHttpService {
GET_HEROES: 'https://hahow-recruit.herokuapp.com/heroes',
AUTHENTICATE: 'https://hahow-recruit.herokuapp.com/auth',
};

constructor(private httpService: HttpService) {}

/**
Expand Down Expand Up @@ -55,6 +56,12 @@ export class ExternalHttpService {
return firstValueFrom(
this.httpService.get(`${this.EXTERNAL_ENDPOINT.GET_HEROES}/${id}`).pipe(
catchError((error: AxiosError) => {
if (error.response?.status === HttpStatus.NOT_FOUND) {
throw new NotFoundException({
code: CustomErrorCodes.HERO_NOT_FOUND,
message: `Hero with id ${id} not found`,
});
}
throw new InternalServerErrorException({
code: CustomErrorCodes.THIRDPARTY_SERVER_ERROR,
message: error.message,
Expand Down Expand Up @@ -84,6 +91,12 @@ export class ExternalHttpService {
return firstValueFrom(
this.httpService.get(`${this.EXTERNAL_ENDPOINT.GET_HEROES}/${id}/profile`).pipe(
catchError((error: AxiosError) => {
if (error.status === HttpStatus.NOT_FOUND) {
throw new NotFoundException({
code: CustomErrorCodes.HERO_NOT_FOUND,
message: `Hero with id ${id} not found`,
});
}
throw new InternalServerErrorException({
code: CustomErrorCodes.THIRDPARTY_SERVER_ERROR,
message: error.message,
Expand Down
23 changes: 23 additions & 0 deletions test/modules/hero/hero.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager';

import * as request from 'supertest';
import * as nock from 'nock';
import { AxiosError } from 'axios';
import { Cache } from 'cache-manager';

import { CustomErrorCodes } from '#cores/types';
Expand Down Expand Up @@ -223,6 +224,28 @@ describe('AppController (e2e)', () => {
expect(res.body.requestId).toMatch(UUID_V4_REGEX);
});
});

it("/heroes/:id, return 404 if hero doesn't exist.", () => {
nock(baseUrl)
.get(endpoint)
.reply(404, {
message: 'Hero not found',
name: 'Error',
config: {},
code: '404',
request: {},
response: { status: 404, statusText: 'Not Found' },
} as AxiosError);

return request(server)
.get('/heroes/1')
.expect(404)
.expect((res) => {
expect(res.body.code).toBe(CustomErrorCodes.HERO_NOT_FOUND);
expect(res.body.message).toBeDefined();
expect(res.body.requestId).toMatch(UUID_V4_REGEX);
});
});
});

describe('With Auth', () => {
Expand Down

0 comments on commit 0b3ee2f

Please sign in to comment.