Skip to content

Commit

Permalink
refactor: 500 단일 에러에서 에러 상황에 따라 400, 500 에러로 분기
Browse files Browse the repository at this point in the history
  • Loading branch information
i3kae committed Nov 13, 2024
1 parent 1fd563f commit 3fceb0c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
48 changes: 47 additions & 1 deletion backend/mainServer/src/host/host.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('HostController', () => {

const req = {
headers: {
url: 'http://example.com',
host: 'http://example.com',
'content-type': 'application/json',
},
body: {
Expand All @@ -49,4 +49,50 @@ describe('HostController', () => {
expect(res.status).toHaveBeenCalledWith(HttpStatus.OK);
expect(res.json).toHaveBeenCalledWith({ 'host-data': mockHostData });
});

it('should return 400 for invalid request with missing uuid', async () => {
jest.spyOn(service, 'generateStreamKey').mockResolvedValue(undefined);

const req = {
headers: {
host: 'http://example.com',
'content-type': 'application/json',
},
body: {
// uuid가 없는 비정상적인 요청
},
} as any;

const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as any;

await controller.generateStreamKey(req, res);

expect(res.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST);
});

it('should return 400 for invalid request with incorrect content-type', async () => {
jest.spyOn(service, 'generateStreamKey').mockResolvedValue(undefined);

const req = {
headers: {
host: 'http://example.com',
'content-type': 'text/plain',
},
body: {
uuid: 'test-uuid',
},
} as any;

const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as any;

await controller.generateStreamKey(req, res);

expect(res.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST);
});
});
19 changes: 14 additions & 5 deletions backend/mainServer/src/host/host.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ export class HostController {
const contentType = req.headers['content-type'];
const { uuid } = req.body;

if (!host || !contentType || contentType !== 'application/json' || !uuid) {
if (!host || !contentType || !uuid) {
throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST);
}
if (contentType !== 'application/json') {
throw new HttpException("Content-Type must be application/json", HttpStatus.BAD_REQUEST)
}

const hostData = await this.hostService.generateStreamKey(uuid);
res.status(HttpStatus.OK).json({ 'host-data': hostData });
} catch (error) {
res.status(HttpStatus.SERVICE_UNAVAILABLE).json({
statusCode: 503,
message: 'Server logic error',
});
if (error.status === 400) {
res.status(HttpStatus.BAD_REQUEST).json({
error: error.response
})
}
else {
res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
error: 'Server logic error',
});
}
}
}
}

0 comments on commit 3fceb0c

Please sign in to comment.