Skip to content

Commit

Permalink
fix(api): recapture http errors as real errors, split user messages a…
Browse files Browse the repository at this point in the history
…nd errors up
  • Loading branch information
seiyria committed Sep 20, 2023
1 parent ad11229 commit 35b09b5
Show file tree
Hide file tree
Showing 19 changed files with 323 additions and 233 deletions.
4 changes: 3 additions & 1 deletion server/src/modules/achievements/achievements.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class AchievementsService {

// mongodb duplicate
if (e.code === 11000) {
throw new BadRequestException('achievements id already in use.');
throw new BadRequestException(
`achievements id ${userId} already in use.`,
);
}

throw e;
Expand Down
2 changes: 1 addition & 1 deletion server/src/modules/crafting/crafting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class CraftingService {

// mongodb duplicate
if (e.code === 11000) {
throw new BadRequestException('crafting id already in use.');
throw new BadRequestException(`crafting id ${userId} already in use.`);
}

throw e;
Expand Down
3 changes: 2 additions & 1 deletion server/src/modules/discoveries/discoveries.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class DiscoveriesController {
const discoveries = await this.discoveriesService.getDiscoveriesForUser(
user.userId,
);
if (!discoveries) throw new NotFoundException('User not found');
if (!discoveries)
throw new NotFoundException(`User ${user.userId} not found`);

return { discoveries };
}
Expand Down
56 changes: 34 additions & 22 deletions server/src/modules/discoveries/discoveries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { InventoryService } from '@modules/inventory/inventory.service';
import { Player } from '@modules/player/player.schema';
import {
BadRequestException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { getPatchesAfterPropChanges } from '@utils/patches';
import { userError } from '@utils/usernotifications';
import { sample, sum } from 'lodash';
import { Logger } from 'nestjs-pino';

Expand Down Expand Up @@ -53,7 +53,9 @@ export class DiscoveriesService {

// mongodb duplicate
if (e.code === 11000) {
throw new BadRequestException('discoveries id already in use.');
throw new BadRequestException(
`discoveries id ${userId} already in use.`,
);
}

throw e;
Expand Down Expand Up @@ -115,19 +117,20 @@ export class DiscoveriesService {
instanceId: string,
): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('User not found');
if (!discoveries) throw new NotFoundException(`User ${userId} not found`);

const item = await this.inventoryService.getInventoryItemForUser(
userId,
instanceId,
);
if (!item) throw new NotFoundException('Collectible item not found');
if (!item)
throw new NotFoundException(`Collectible item ${instanceId} not found`);

const itemDefinition = await this.contentService.getCollectible(
item.itemId,
);
if (!itemDefinition)
throw new NotFoundException('Item definition not found');
throw new NotFoundException(`Item definition ${item.itemId} not found`);

await this.inventoryService.removeInventoryItemForUser(userId, instanceId);

Expand Down Expand Up @@ -169,21 +172,24 @@ export class DiscoveriesService {
instanceId: string,
): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('User not found');
if (!discoveries) throw new NotFoundException(`User ${userId} not found`);

const item = await this.inventoryService.getInventoryItemForUser(
userId,
instanceId,
);
if (!item) throw new NotFoundException('Equipment item not found');
if (!item)
throw new NotFoundException(`Equipment item ${instanceId} not found`);

const itemDefinition = await this.contentService.getEquipment(item.itemId);
if (!itemDefinition) {
await this.inventoryService.removeInventoryItemForUser(
userId,
instanceId,
);
throw new NotFoundException('Item definition not found');
throw new NotFoundException(
`Item definition ${item.itemId} not found; automatically removing item...`,
);
}

await this.inventoryService.removeInventoryItemForUser(userId, instanceId);
Expand Down Expand Up @@ -223,10 +229,10 @@ export class DiscoveriesService {

async discoverMonster(userId: string, monsterId: string): Promise<void> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('User not found');
if (!discoveries) throw new NotFoundException(`User ${userId} not found`);

const monster = await this.contentService.getMonster(monsterId);
if (!monster) throw new NotFoundException('Monster not found');
if (!monster) throw new NotFoundException(`Monster ${monsterId} not found`);

this.logger.verbose(`Discovered monster ${monster.name} for ${userId}.`);

Expand All @@ -240,14 +246,15 @@ export class DiscoveriesService {

async claimUniqueCollectibleReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('Discoveries not found');
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.uniqueCollectibleClaims ?? 0;
const totalCollectiblesFound = sum(Object.keys(discoveries.collectibles));
const interval = this.constants.uniqueCollectiblesRequired;

if (totalCollectiblesFound < (totalTimesClaimed + 1) * interval)
throw new BadRequestException('Not enough collectibles found');
return userError('Not enough collectibles found!');

const discoveryPatches = await getPatchesAfterPropChanges<Discoveries>(
discoveries,
Expand Down Expand Up @@ -282,14 +289,15 @@ export class DiscoveriesService {

async claimTotalCollectibleReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('Discoveries not found');
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.totalCollectibleClaims ?? 0;
const totalCollectiblesFound = sum(Object.values(discoveries.collectibles));
const interval = this.constants.totalCollectiblesRequired;

if (totalCollectiblesFound < (totalTimesClaimed + 1) * interval)
throw new BadRequestException('Not enough collectibles found');
return userError('Not enough collectibles found!');

const discoveryPatches = await getPatchesAfterPropChanges<Discoveries>(
discoveries,
Expand Down Expand Up @@ -324,14 +332,15 @@ export class DiscoveriesService {

async claimUniqueEquipmentReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('Discoveries not found');
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.uniqueEquipmentClaims ?? 0;
const totalItemsFound = sum(Object.keys(discoveries.items));
const interval = this.constants.uniqueEquipmentRequired;

if (totalItemsFound < (totalTimesClaimed + 1) * interval)
throw new BadRequestException('Not enough equipment found');
return userError('Not enough equipment found!');

const discoveryPatches = await getPatchesAfterPropChanges<Discoveries>(
discoveries,
Expand Down Expand Up @@ -366,14 +375,15 @@ export class DiscoveriesService {

async claimTotalEquipmentReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('Discoveries not found');
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.totalEquipmentClaims ?? 0;
const totalItemsFound = sum(Object.values(discoveries.items));
const interval = this.constants.totalEquipmentRequired;

if (totalItemsFound < (totalTimesClaimed + 1) * interval)
throw new ForbiddenException('Not enough equipment found');
return userError('Not enough equipment found!');

const discoveryPatches = await getPatchesAfterPropChanges<Discoveries>(
discoveries,
Expand Down Expand Up @@ -408,14 +418,15 @@ export class DiscoveriesService {

async claimUniqueMonsterReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('Discoveries not found');
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.uniqueMonsterClaims ?? 0;
const totalItemsFound = sum(Object.keys(discoveries.monsters));
const interval = this.constants.uniqueMonstersRequired;

if (totalItemsFound < (totalTimesClaimed + 1) * interval)
throw new BadRequestException('Not enough equipment found');
return userError('Not enough monsters found!');

const discoveryPatches = await getPatchesAfterPropChanges<Discoveries>(
discoveries,
Expand Down Expand Up @@ -450,14 +461,15 @@ export class DiscoveriesService {

async claimTotalMonsterReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException('Discoveries not found');
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.totalMonsterClaims ?? 0;
const totalItemsFound = sum(Object.values(discoveries.monsters));
const interval = this.constants.totalMonstersRequired;

if (totalItemsFound < (totalTimesClaimed + 1) * interval)
throw new ForbiddenException('Not enough equipment found');
return userError('Not enough monsters found!');

const discoveryPatches = await getPatchesAfterPropChanges<Discoveries>(
discoveries,
Expand Down
Loading

0 comments on commit 35b09b5

Please sign in to comment.