Skip to content
Snippets Groups Projects
Verified Commit 4f51e931 authored by Adrian Paschkowski's avatar Adrian Paschkowski :thinking:
Browse files

Improve error messages

parent 64ff8a24
No related branches found
No related tags found
No related merge requests found
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; import {
CanActivate,
ExecutionContext,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql'; import { GqlExecutionContext } from '@nestjs/graphql';
import { Request } from 'express'; import { Request } from 'express';
import { PrismaService } from '../prisma/prisma.service'; import { PrismaService } from '../prisma/prisma.service';
...@@ -16,10 +21,14 @@ export class GroupAuthGuard implements CanActivate { ...@@ -16,10 +21,14 @@ export class GroupAuthGuard implements CanActivate {
if (!token) return false; if (!token) return false;
req.group = await this.prismaService.group.findFirst({ const group = await this.prismaService.group.findFirst({
where: { slug: token }, where: { slug: token },
}); });
return !!req.group; if (!group) throw new NotFoundException('That Group does not exist');
req.group = group;
return true;
} }
} }
import { import {
CanActivate, CanActivate,
ExecutionContext, ExecutionContext,
ForbiddenException,
Injectable, Injectable,
NotFoundException,
UnauthorizedException, UnauthorizedException,
} from '@nestjs/common'; } from '@nestjs/common';
import { Request } from 'express'; import { Request } from 'express';
...@@ -23,7 +23,7 @@ export class WebAuthGuard implements CanActivate { ...@@ -23,7 +23,7 @@ export class WebAuthGuard implements CanActivate {
where: { slug: token }, where: { slug: token },
}); });
if (!group) throw new ForbiddenException(); if (!group) throw new NotFoundException('That Group does not exist');
req.group = group; req.group = group;
......
...@@ -36,7 +36,7 @@ export class AgentResolver { ...@@ -36,7 +36,7 @@ export class AgentResolver {
include: { tokenCode: true }, include: { tokenCode: true },
}); });
if (!agent) throw new NotFoundException(); if (!agent) throw new NotFoundException('That Agent does not exist');
return agent; return agent;
} }
......
...@@ -52,7 +52,7 @@ export class EntryResolver { ...@@ -52,7 +52,7 @@ export class EntryResolver {
}, },
}); });
if (!unlock) throw new NotFoundException(); if (!unlock) throw new NotFoundException('That entry does not exist');
if (req.group.tokens <= 0) if (req.group.tokens <= 0)
throw new BadRequestException('No remaining tokens'); throw new BadRequestException('No remaining tokens');
...@@ -66,7 +66,8 @@ export class EntryResolver { ...@@ -66,7 +66,8 @@ export class EntryResolver {
}, },
}); });
if (count > 0) throw new BadRequestException('Already unlocked'); if (count > 0)
throw new BadRequestException('Entry is already unlocked');
const group = await this.prismaService.group.update({ const group = await this.prismaService.group.update({
where: { id: req.group.id }, where: { id: req.group.id },
......
...@@ -13,7 +13,6 @@ import { PrismaService } from '../prisma/prisma.service'; ...@@ -13,7 +13,6 @@ import { PrismaService } from '../prisma/prisma.service';
import { GraphQLString } from 'graphql'; import { GraphQLString } from 'graphql';
import * as seedWords from 'mnemonic-words'; import * as seedWords from 'mnemonic-words';
import { import {
ForbiddenException,
NotFoundException, NotFoundException,
UnauthorizedException, UnauthorizedException,
UseGuards, UseGuards,
...@@ -42,7 +41,7 @@ export class GroupResolver { ...@@ -42,7 +41,7 @@ export class GroupResolver {
}, },
}); });
if (!group) throw new ForbiddenException(); if (!group) throw new NotFoundException('That Group does not exist');
return pubSub.asyncIterator('updateToken'); return pubSub.asyncIterator('updateToken');
} }
...@@ -61,7 +60,7 @@ export class GroupResolver { ...@@ -61,7 +60,7 @@ export class GroupResolver {
}, },
}); });
if (!group) throw new NotFoundException(); if (!group) throw new NotFoundException('That Group does not exist');
await pubSub.publish('updateToken', { updateToken: group }); await pubSub.publish('updateToken', { updateToken: group });
...@@ -83,7 +82,7 @@ export class GroupResolver { ...@@ -83,7 +82,7 @@ export class GroupResolver {
}, },
}); });
if (!agent) throw new ForbiddenException(); if (!agent) throw new NotFoundException('That Agent does not exist');
function generateWord() { function generateWord() {
return seedWords[Math.round(Math.random() * seedWords.length)]; return seedWords[Math.round(Math.random() * seedWords.length)];
......
...@@ -2,7 +2,6 @@ import { ...@@ -2,7 +2,6 @@ import {
UseGuards, UseGuards,
NotFoundException, NotFoundException,
UnauthorizedException, UnauthorizedException,
ForbiddenException,
} from '@nestjs/common'; } from '@nestjs/common';
import { import {
Args, Args,
...@@ -40,7 +39,7 @@ export class TokenCodeResolver { ...@@ -40,7 +39,7 @@ export class TokenCodeResolver {
}, },
}); });
if (!agent) throw new ForbiddenException(); if (!agent) throw new NotFoundException('That Agent does not exist');
return pubSub.asyncIterator('newTokenCode'); return pubSub.asyncIterator('newTokenCode');
} }
...@@ -57,7 +56,7 @@ export class TokenCodeResolver { ...@@ -57,7 +56,7 @@ export class TokenCodeResolver {
}, },
}); });
if (!agent) throw new ForbiddenException(); if (!agent) throw new NotFoundException('That Agent does not exist');
let tokenCode = let tokenCode =
agent.tokenCodeId && agent.tokenCodeId &&
...@@ -67,24 +66,30 @@ export class TokenCodeResolver { ...@@ -67,24 +66,30 @@ export class TokenCodeResolver {
}, },
})); }));
if (!tokenCode) { if (tokenCode) {
tokenCode = await this.prismaService.tokenCode.create({ await this.prismaService.tokenCode.delete({
data: { where: {
value: 1, id: tokenCode.id,
agent: {
connect: { id: agent.id },
},
},
include: {
agent: true,
}, },
}); });
await pubSub.publish('newTokenCode', {
newTokenCode: tokenCode,
});
} }
tokenCode = await this.prismaService.tokenCode.create({
data: {
value: 1,
agent: {
connect: { id: agent.id },
},
},
include: {
agent: true,
},
});
await pubSub.publish('newTokenCode', {
newTokenCode: tokenCode,
});
return tokenCode; return tokenCode;
} }
...@@ -101,7 +106,8 @@ export class TokenCodeResolver { ...@@ -101,7 +106,8 @@ export class TokenCodeResolver {
}, },
}); });
if (!tokenCode) throw new NotFoundException(); if (!tokenCode)
throw new NotFoundException('That Token Code does not exist');
const newTokenCode = await this.prismaService.tokenCode.create({ const newTokenCode = await this.prismaService.tokenCode.create({
data: { data: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment