From 96484c90d6c56ed7644a1968a09534f1b493db1f Mon Sep 17 00:00:00 2001 From: tsa96 Date: Thu, 18 Jan 2024 20:47:26 +0000 Subject: [PATCH] refactor(back): more appropriate status codes in leaderboard endpoints sorry ImATeapotException --- apps/backend-e2e/src/maps-2.e2e-spec.ts | 4 ++-- .../src/app/modules/maps/maps.controller.ts | 17 ++++++++++++++++- .../modules/runs/leaderboard-runs.service.ts | 16 ++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/apps/backend-e2e/src/maps-2.e2e-spec.ts b/apps/backend-e2e/src/maps-2.e2e-spec.ts index 6b941635e..7174a3bed 100644 --- a/apps/backend-e2e/src/maps-2.e2e-spec.ts +++ b/apps/backend-e2e/src/maps-2.e2e-spec.ts @@ -1621,13 +1621,13 @@ describe('Maps Part 2', () => { expect(mockSteamIDs).toContain(BigInt(run.user.steamID)); }); - it('should 418 if the user has no Steam friends', async () => { + it('should 410 if the user has no Steam friends', async () => { jest.spyOn(steamService, 'getSteamFriends').mockResolvedValueOnce([]); return req.get({ url: `maps/${map.id}/leaderboard`, query: { gamemode: Gamemode.AHOP, filter: 'friends' }, - status: 418, + status: 410, token }); }); diff --git a/apps/backend/src/app/modules/maps/maps.controller.ts b/apps/backend/src/app/modules/maps/maps.controller.ts index 39a32486f..a02b3fb08 100644 --- a/apps/backend/src/app/modules/maps/maps.controller.ts +++ b/apps/backend/src/app/modules/maps/maps.controller.ts @@ -27,11 +27,13 @@ import { ApiConsumes, ApiCreatedResponse, ApiForbiddenResponse, + ApiGoneResponse, ApiNoContentResponse, ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiParam, + ApiServiceUnavailableResponse, ApiTags } from '@nestjs/swagger'; import { @@ -649,6 +651,18 @@ export class MapsController { required: true }) @ApiOkResponse({ description: "The found leaderboard's runs" }) + @ApiNotFoundResponse({ description: "When the map doesn't exist" }) + @ApiGoneResponse({ + description: + "When the filtering by 'around', and the user doesn't have a PB" + }) + @ApiGoneResponse({ + description: + "When the filtering by 'friends', and the user doesn't have any Steam friends" + }) + @ApiServiceUnavailableResponse({ + description: "Steam fails to return the user's friends list (Tuesdays lol)" + }) getLeaderboards( @Param('mapID', ParseIntSafePipe) mapID: number, @LoggedInUser() { id, steamID }: UserJwtAccessPayload, @@ -666,7 +680,8 @@ export class MapsController { required: true }) @ApiOkResponse({ description: 'The found run' }) - @ApiNotFoundResponse({ description: 'Either map or run not found' }) + @ApiNotFoundResponse({ description: 'Map not found' }) + @ApiNotFoundResponse({ description: 'Run not found' }) getLeaderboardRun( @Param('mapID', ParseIntSafePipe) mapID: number, @LoggedInUser('id') userID: number, diff --git a/apps/backend/src/app/modules/runs/leaderboard-runs.service.ts b/apps/backend/src/app/modules/runs/leaderboard-runs.service.ts index 2a39c35c8..f4d7d7553 100644 --- a/apps/backend/src/app/modules/runs/leaderboard-runs.service.ts +++ b/apps/backend/src/app/modules/runs/leaderboard-runs.service.ts @@ -1,10 +1,11 @@ import { BadRequestException, forwardRef, - ImATeapotException, + GoneException, Inject, Injectable, - NotFoundException + NotFoundException, + ServiceUnavailableException } from '@nestjs/common'; import { Prisma } from '@prisma/client'; import { runPath } from '@momentum/constants'; @@ -113,7 +114,7 @@ export class LeaderboardRunsService { }); if (!userRun) - throw new NotFoundException('User has no runs on this leaderboard'); + throw new GoneException('User has no runs on this leaderboard'); // Start at your rank, then backtrack by half of `take`, then 1 for your rank skip = Math.max(userRun.rank - Math.floor(take / 2) - 1, 0); @@ -122,11 +123,14 @@ export class LeaderboardRunsService { } else if (query.filter?.[0] === 'friends') { // Regular skip/take should work fine here. - const steamFriends = - await this.steamService.getSteamFriends(loggedInUserSteamID); + const steamFriends = await this.steamService + .getSteamFriends(loggedInUserSteamID) + .catch(() => { + throw new ServiceUnavailableException(); + }); if (steamFriends.length === 0) - throw new ImATeapotException('No friends detected :('); + throw new GoneException('No friends detected :('); // Doing this with a window function is gonna be fun... where.user = {