diff --git a/src/modules/api-keys/api-keys.service.ts b/src/modules/api-keys/api-keys.service.ts index 84b7ac470..44b96a8dd 100644 --- a/src/modules/api-keys/api-keys.service.ts +++ b/src/modules/api-keys/api-keys.service.ts @@ -1,7 +1,6 @@ import { - HttpException, - HttpStatus, Injectable, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @@ -103,8 +102,7 @@ export class ApiKeysService { const apiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!apiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!apiKey) throw new NotFoundException('API key not found'); if (apiKey.groupId !== groupId) throw new UnauthorizedException(); return this.prisma.expose(apiKey); } @@ -112,8 +110,7 @@ export class ApiKeysService { const apiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!apiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!apiKey) throw new NotFoundException('API key not found'); if (apiKey.userId !== userId) throw new UnauthorizedException(); return this.prisma.expose(apiKey); } @@ -122,8 +119,7 @@ export class ApiKeysService { const apiKey = await this.prisma.apiKeys.findFirst({ where: { apiKey: key }, }); - if (!apiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!apiKey) throw new NotFoundException('API key not found'); if (this.lru.has(key)) return this.lru.get(key); this.lru.set(key, apiKey); return this.prisma.expose(apiKey); @@ -137,8 +133,7 @@ export class ApiKeysService { const testApiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!testApiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!testApiKey) throw new NotFoundException('API key not found'); if (testApiKey.groupId !== groupId) throw new UnauthorizedException(); data.scopes = this.cleanScopesForGroup(groupId, data.scopes); const apiKey = await this.prisma.apiKeys.update({ @@ -156,8 +151,7 @@ export class ApiKeysService { const testApiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!testApiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!testApiKey) throw new NotFoundException('API key not found'); if (testApiKey.userId !== userId) throw new UnauthorizedException(); data.scopes = this.cleanScopesForUser(userId, data.scopes); const apiKey = await this.prisma.apiKeys.update({ @@ -176,8 +170,7 @@ export class ApiKeysService { const testApiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!testApiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!testApiKey) throw new NotFoundException('API key not found'); if (testApiKey.groupId !== groupId) throw new UnauthorizedException(); data.scopes = this.cleanScopesForGroup(groupId, data.scopes); const apiKey = await this.prisma.apiKeys.update({ @@ -195,8 +188,7 @@ export class ApiKeysService { const testApiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!testApiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!testApiKey) throw new NotFoundException('API key not found'); if (testApiKey.userId !== userId) throw new UnauthorizedException(); data.scopes = this.cleanScopesForUser(userId, data.scopes); const apiKey = await this.prisma.apiKeys.update({ @@ -214,8 +206,7 @@ export class ApiKeysService { const testApiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!testApiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!testApiKey) throw new NotFoundException('API key not found'); if (testApiKey.groupId !== groupId) throw new UnauthorizedException(); const apiKey = await this.prisma.apiKeys.delete({ where: { id }, @@ -230,8 +221,7 @@ export class ApiKeysService { const testApiKey = await this.prisma.apiKeys.findOne({ where: { id }, }); - if (!testApiKey) - throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); + if (!testApiKey) throw new NotFoundException('API key not found'); if (testApiKey.userId !== userId) throw new UnauthorizedException(); const apiKey = await this.prisma.apiKeys.delete({ where: { id }, diff --git a/src/modules/approved-subnets/approved-subnets.service.ts b/src/modules/approved-subnets/approved-subnets.service.ts index 47275e291..adbe1055c 100644 --- a/src/modules/approved-subnets/approved-subnets.service.ts +++ b/src/modules/approved-subnets/approved-subnets.service.ts @@ -1,21 +1,20 @@ import { - HttpException, - HttpStatus, Injectable, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { approvedSubnets, approvedSubnetsOrderByInput, approvedSubnetsWhereInput, approvedSubnetsWhereUniqueInput, } from '@prisma/client'; -import { Expose } from '../../modules/prisma/prisma.interface'; -import { PrismaService } from '../prisma/prisma.service'; -import anonymize from 'ip-anonymize'; import { compare, hash } from 'bcrypt'; -import { ConfigService } from '@nestjs/config'; +import anonymize from 'ip-anonymize'; +import { Expose } from '../../modules/prisma/prisma.interface'; import { GeolocationService } from '../geolocation/geolocation.service'; +import { PrismaService } from '../prisma/prisma.service'; @Injectable() export class ApprovedSubnetsService { @@ -56,10 +55,10 @@ export class ApprovedSubnetsService { where: { id }, }); if (!approvedSubnet) - throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND); + throw new NotFoundException('Approved subnet not found'); if (approvedSubnet.userId !== userId) throw new UnauthorizedException(); if (!approvedSubnet) - throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND); + throw new NotFoundException('Approved subnet not found'); return this.prisma.expose(approvedSubnet); } @@ -71,7 +70,7 @@ export class ApprovedSubnetsService { where: { id }, }); if (!testApprovedSubnet) - throw new HttpException('ApprovedSubnet not found', HttpStatus.NOT_FOUND); + throw new NotFoundException('Approved subnet not found'); if (testApprovedSubnet.userId !== userId) throw new UnauthorizedException(); const approvedSubnet = await this.prisma.approvedSubnets.delete({ where: { id }, diff --git a/src/modules/audit-logs/audit-logs.service.ts b/src/modules/audit-logs/audit-logs.service.ts index 583212e2a..8e55beee3 100644 --- a/src/modules/audit-logs/audit-logs.service.ts +++ b/src/modules/audit-logs/audit-logs.service.ts @@ -1,7 +1,6 @@ import { - HttpException, - HttpStatus, Injectable, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; import { @@ -42,8 +41,7 @@ export class AuditLogsService { const auditLog = await this.prisma.auditLogs.findOne({ where: { id }, }); - if (!auditLog) - throw new HttpException('AuditLog not found', HttpStatus.NOT_FOUND); + if (!auditLog) throw new NotFoundException('Audit log not found'); if (auditLog.groupId !== groupId) throw new UnauthorizedException(); return this.prisma.expose(auditLog); } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index a841c2137..cdf8262db 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -1,9 +1,9 @@ import { BadRequestException, - HttpException, - HttpStatus, + ConflictException, Injectable, NotFoundException, + NotImplementedException, UnauthorizedException, UnprocessableEntityException, } from '@nestjs/common'; @@ -79,13 +79,12 @@ export class AuthService { prefersEmail: true, }, }); - if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND); + if (!user) throw new NotFoundException('User not found'); if (!user.emails.find((i) => i.emailSafe === emailSafe)?.isVerified) throw new UnauthorizedException('This email is not verified'); if (!password || !user.password) - throw new HttpException( + throw new NotImplementedException( 'Logging in without passwords is not supported', - HttpStatus.NOT_IMPLEMENTED, ); if (!user.prefersEmail) throw new BadRequestException('User has no email attached to it'); @@ -114,10 +113,7 @@ export class AuthService { where: { emails: { some: { emailSafe } } }, }); if (testUser) - throw new HttpException( - 'A user with this email already exists', - HttpStatus.CONFLICT, - ); + throw new ConflictException('A user with this email already exists'); const ignorePwnedPassword = !!data.ignorePwnedPassword; delete data.ignorePwnedPassword; @@ -187,15 +183,9 @@ export class AuthService { include: { user: true }, }); if (!emailDetails) - throw new HttpException( - 'There is no user for this email', - HttpStatus.NOT_FOUND, - ); + throw new NotFoundException('There is no user for this email'); if (emailDetails.isVerified) - throw new HttpException( - 'This email is already verified', - HttpStatus.BAD_REQUEST, - ); + throw new ConflictException('This email is already verified'); this.email.send({ to: `"${emailDetails.user.name}" <${email}>`, template: resend @@ -226,8 +216,7 @@ export class AuthService { where: { token }, include: { user: true }, }); - if (!session) - throw new HttpException('Session not found', HttpStatus.NOT_FOUND); + if (!session) throw new NotFoundException('Session not found'); await this.prisma.sessions.updateMany({ where: { token }, data: { ipAddress, userAgent }, @@ -244,8 +233,7 @@ export class AuthService { where: { token }, select: { id: true, user: { select: { id: true } } }, }); - if (!session) - throw new HttpException('Session not found', HttpStatus.NOT_FOUND); + if (!session) throw new NotFoundException('Session not found'); await this.prisma.sessions.delete({ where: { id: session.id }, }); @@ -348,10 +336,7 @@ export class AuthService { include: { user: true }, }); if (!emailDetails) - throw new HttpException( - 'There is no user for this email', - HttpStatus.NOT_FOUND, - ); + throw new NotFoundException('There is no user for this email'); this.email.send({ to: `"${emailDetails.user.name}" <${email}>`, template: 'auth/password-reset', @@ -596,9 +581,8 @@ export class AuthService { this.configService.get('security.saltRounds') ?? 10, ); if (!(await this.pwnedService.isPasswordSafe(password))) - throw new HttpException( + throw new BadRequestException( 'This password has been compromised in a data breach.', - HttpStatus.BAD_REQUEST, ); } return await hash( diff --git a/src/modules/domains/domains.service.ts b/src/modules/domains/domains.service.ts index cd1cb92e2..d77bb5823 100644 --- a/src/modules/domains/domains.service.ts +++ b/src/modules/domains/domains.service.ts @@ -1,8 +1,7 @@ import { BadRequestException, - HttpException, - HttpStatus, Injectable, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @@ -14,6 +13,7 @@ import { domainsWhereUniqueInput, } from '@prisma/client'; import got from 'got'; +import { URL } from 'url'; import { DnsService } from '../dns/dns.service'; import { Expose } from '../prisma/prisma.interface'; import { PrismaService } from '../prisma/prisma.service'; @@ -23,7 +23,6 @@ import { DOMAIN_VERIFICATION_TXT, } from './domains.constants'; import { DomainVerificationMethods } from './domains.interface'; -import { URL } from 'url'; @Injectable() export class DomainsService { @@ -97,8 +96,7 @@ export class DomainsService { const domain = await this.prisma.domains.findOne({ where: { id }, }); - if (!domain) - throw new HttpException('Domain not found', HttpStatus.NOT_FOUND); + if (!domain) throw new NotFoundException('Domain not found'); if (domain.groupId !== groupId) throw new UnauthorizedException(); return this.prisma.expose(domain); } @@ -111,8 +109,7 @@ export class DomainsService { const domain = await this.prisma.domains.findOne({ where: { id }, }); - if (!domain) - throw new HttpException('Domain not found', HttpStatus.NOT_FOUND); + if (!domain) throw new NotFoundException('Domain not found'); if (domain.groupId !== groupId) throw new UnauthorizedException(); if (method === DOMAIN_VERIFICATION_TXT) { const txtRecords = await this.dnsService.lookup(domain.domain, 'TXT'); @@ -146,8 +143,7 @@ export class DomainsService { const testDomain = await this.prisma.domains.findOne({ where: { id }, }); - if (!testDomain) - throw new HttpException('Domain not found', HttpStatus.NOT_FOUND); + if (!testDomain) throw new NotFoundException('Domain not found'); if (testDomain.groupId !== groupId) throw new UnauthorizedException(); const domain = await this.prisma.domains.delete({ where: { id }, diff --git a/src/modules/emails/emails.service.ts b/src/modules/emails/emails.service.ts index 33056884a..2a29bd020 100644 --- a/src/modules/emails/emails.service.ts +++ b/src/modules/emails/emails.service.ts @@ -1,7 +1,6 @@ import { - HttpException, - HttpStatus, Injectable, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; import { @@ -62,8 +61,7 @@ export class EmailsService { const email = await this.prisma.emails.findOne({ where: { id }, }); - if (!email) - throw new HttpException('Email not found', HttpStatus.NOT_FOUND); + if (!email) throw new NotFoundException('Email not found'); if (email.userId !== userId) throw new UnauthorizedException(); return this.prisma.expose(email); } @@ -72,8 +70,7 @@ export class EmailsService { const testEmail = await this.prisma.emails.findOne({ where: { id }, }); - if (!testEmail) - throw new HttpException('Email not found', HttpStatus.NOT_FOUND); + if (!testEmail) throw new NotFoundException('Email not found'); if (testEmail.userId !== userId) throw new UnauthorizedException(); const email = await this.prisma.emails.delete({ where: { id }, diff --git a/src/modules/groups/groups.service.ts b/src/modules/groups/groups.service.ts index 79515869a..83677762e 100644 --- a/src/modules/groups/groups.service.ts +++ b/src/modules/groups/groups.service.ts @@ -1,4 +1,4 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { groups, groupsCreateInput, @@ -7,9 +7,9 @@ import { groupsWhereInput, groupsWhereUniqueInput, } from '@prisma/client'; +import randomColor from 'randomcolor'; import { Expose } from '../../modules/prisma/prisma.interface'; import { PrismaService } from '../prisma/prisma.service'; -import randomColor from 'randomcolor'; @Injectable() export class GroupsService { @@ -63,8 +63,7 @@ export class GroupsService { const group = await this.prisma.groups.findOne({ where: { id }, }); - if (!group) - throw new HttpException('Group not found', HttpStatus.NOT_FOUND); + if (!group) throw new NotFoundException('Group not found'); return this.prisma.expose(group); } @@ -75,8 +74,7 @@ export class GroupsService { const testGroup = await this.prisma.groups.findOne({ where: { id }, }); - if (!testGroup) - throw new HttpException('Group not found', HttpStatus.NOT_FOUND); + if (!testGroup) throw new NotFoundException('Group not found'); const group = await this.prisma.groups.update({ where: { id }, data, @@ -91,8 +89,7 @@ export class GroupsService { const testGroup = await this.prisma.groups.findOne({ where: { id }, }); - if (!testGroup) - throw new HttpException('Group not found', HttpStatus.NOT_FOUND); + if (!testGroup) throw new NotFoundException('Group not found'); const group = await this.prisma.groups.update({ where: { id }, data, @@ -104,8 +101,7 @@ export class GroupsService { const testGroup = await this.prisma.groups.findOne({ where: { id }, }); - if (!testGroup) - throw new HttpException('Group not found', HttpStatus.NOT_FOUND); + if (!testGroup) throw new NotFoundException('Group not found'); const group = await this.prisma.groups.delete({ where: { id }, }); diff --git a/src/modules/memberships/memberships.service.ts b/src/modules/memberships/memberships.service.ts index d7fe510f3..234ca9f6d 100644 --- a/src/modules/memberships/memberships.service.ts +++ b/src/modules/memberships/memberships.service.ts @@ -1,6 +1,5 @@ import { - HttpException, - HttpStatus, + BadRequestException, Injectable, NotFoundException, UnauthorizedException, @@ -58,8 +57,7 @@ export class MembershipsService { where: { id }, include: { group: true }, }); - if (!membership) - throw new HttpException('Membership not found', HttpStatus.NOT_FOUND); + if (!membership) throw new NotFoundException('Membership not found'); if (membership.userId !== userId) throw new UnauthorizedException(); return this.prisma.expose(membership); } @@ -72,8 +70,7 @@ export class MembershipsService { where: { id }, include: { group: true }, }); - if (!membership) - throw new HttpException('Membership not found', HttpStatus.NOT_FOUND); + if (!membership) throw new NotFoundException('Membership not found'); if (membership.groupId !== groupId) throw new UnauthorizedException(); return this.prisma.expose(membership); } @@ -85,8 +82,7 @@ export class MembershipsService { const testMembership = await this.prisma.memberships.findOne({ where: { id }, }); - if (!testMembership) - throw new HttpException('Membership not found', HttpStatus.NOT_FOUND); + if (!testMembership) throw new NotFoundException('Membership not found'); if (testMembership.userId !== userId) throw new UnauthorizedException(); await this.verifyDeleteMembership(testMembership.groupId, id); const membership = await this.prisma.memberships.delete({ @@ -103,8 +99,7 @@ export class MembershipsService { const testMembership = await this.prisma.memberships.findOne({ where: { id }, }); - if (!testMembership) - throw new HttpException('Membership not found', HttpStatus.NOT_FOUND); + if (!testMembership) throw new NotFoundException('Membership not found'); if (testMembership.groupId !== groupId) throw new UnauthorizedException(); const membership = await this.prisma.memberships.update({ where: { id }, @@ -120,8 +115,7 @@ export class MembershipsService { const testMembership = await this.prisma.memberships.findOne({ where: { id }, }); - if (!testMembership) - throw new HttpException('Membership not found', HttpStatus.NOT_FOUND); + if (!testMembership) throw new NotFoundException('Membership not found'); if (testMembership.groupId !== groupId) throw new UnauthorizedException(); await this.verifyDeleteMembership(testMembership.groupId, id); const membership = await this.prisma.memberships.delete({ @@ -185,9 +179,8 @@ export class MembershipsService { where: { group: { id: groupId } }, }); if (memberships.length === 1) - throw new HttpException( - 'You cannot remove the sole member of a group', - HttpStatus.BAD_REQUEST, + throw new BadRequestException( + 'You cannot delete the sole member for a group', ); const membership = await this.prisma.memberships.findOne({ where: { id: membershipId }, @@ -197,9 +190,8 @@ export class MembershipsService { membership.role === 'OWNER' && memberships.filter((i) => i.role === 'OWNER').length === 1 ) - throw new HttpException( - 'You cannot remove the sole owner of a group', - HttpStatus.BAD_REQUEST, + throw new BadRequestException( + 'You cannot delete the sole owner for a group', ); } } diff --git a/src/modules/sessions/sessions.service.ts b/src/modules/sessions/sessions.service.ts index 07751a374..6ec50c3d2 100644 --- a/src/modules/sessions/sessions.service.ts +++ b/src/modules/sessions/sessions.service.ts @@ -1,7 +1,6 @@ import { - HttpException, - HttpStatus, Injectable, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; import { @@ -41,11 +40,9 @@ export class SessionsService { const session = await this.prisma.sessions.findOne({ where: { id }, }); - if (!session) - throw new HttpException('Session not found', HttpStatus.NOT_FOUND); + if (!session) throw new NotFoundException('Session not found'); if (session.userId !== userId) throw new UnauthorizedException(); - if (!session) - throw new HttpException('Session not found', HttpStatus.NOT_FOUND); + if (!session) throw new NotFoundException('Session not found'); return this.prisma.expose(session); } @@ -53,8 +50,7 @@ export class SessionsService { const testSession = await this.prisma.sessions.findOne({ where: { id }, }); - if (!testSession) - throw new HttpException('Session not found', HttpStatus.NOT_FOUND); + if (!testSession) throw new NotFoundException('Session not found'); if (testSession.userId !== userId) throw new UnauthorizedException(); const session = await this.prisma.sessions.delete({ where: { id }, diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 5f5685353..2322acaff 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -1,7 +1,5 @@ import { BadRequestException, - HttpException, - HttpStatus, Injectable, NotFoundException, } from '@nestjs/common'; @@ -39,7 +37,7 @@ export class UsersService { const user = await this.prisma.users.findOne({ where: { id }, }); - if (!user) throw new HttpException('User not found', HttpStatus.NOT_FOUND); + if (!user) throw new NotFoundException('User not found'); return this.prisma.expose(user); } diff --git a/src/modules/webhooks/webhooks.service.ts b/src/modules/webhooks/webhooks.service.ts index 28ea65ea6..095ca5694 100644 --- a/src/modules/webhooks/webhooks.service.ts +++ b/src/modules/webhooks/webhooks.service.ts @@ -1,8 +1,7 @@ import { - HttpException, - HttpStatus, Injectable, Logger, + NotFoundException, UnauthorizedException, } from '@nestjs/common'; import { @@ -60,8 +59,7 @@ export class WebhooksService { const webhook = await this.prisma.webhooks.findOne({ where: { id }, }); - if (!webhook) - throw new HttpException('Webhook not found', HttpStatus.NOT_FOUND); + if (!webhook) throw new NotFoundException('Webhook not found'); if (webhook.groupId !== groupId) throw new UnauthorizedException(); return this.prisma.expose(webhook); } @@ -74,8 +72,7 @@ export class WebhooksService { const testWebhook = await this.prisma.webhooks.findOne({ where: { id }, }); - if (!testWebhook) - throw new HttpException('Webhook not found', HttpStatus.NOT_FOUND); + if (!testWebhook) throw new NotFoundException('Webhook not found'); if (testWebhook.groupId !== groupId) throw new UnauthorizedException(); const webhook = await this.prisma.webhooks.update({ where: { id }, @@ -92,8 +89,7 @@ export class WebhooksService { const testWebhook = await this.prisma.webhooks.findOne({ where: { id }, }); - if (!testWebhook) - throw new HttpException('Webhook not found', HttpStatus.NOT_FOUND); + if (!testWebhook) throw new NotFoundException('Webhook not found'); if (testWebhook.groupId !== groupId) throw new UnauthorizedException(); const webhook = await this.prisma.webhooks.update({ where: { id }, @@ -106,8 +102,7 @@ export class WebhooksService { const testWebhook = await this.prisma.webhooks.findOne({ where: { id }, }); - if (!testWebhook) - throw new HttpException('Webhook not found', HttpStatus.NOT_FOUND); + if (!testWebhook) throw new NotFoundException('Webhook not found'); if (testWebhook.groupId !== groupId) throw new UnauthorizedException(); const webhook = await this.prisma.webhooks.delete({ where: { id }, diff --git a/src/pipes/cursor.pipe.ts b/src/pipes/cursor.pipe.ts index dd9974797..173279179 100644 --- a/src/pipes/cursor.pipe.ts +++ b/src/pipes/cursor.pipe.ts @@ -1,9 +1,8 @@ import { - PipeTransform, - Injectable, - HttpException, - HttpStatus, ArgumentMetadata, + BadRequestException, + Injectable, + PipeTransform, } from '@nestjs/common'; import { parseObjectLiteral } from '../helpers/parse-object-literal'; @@ -25,9 +24,8 @@ export class CursorPipe implements PipeTransform { }); return items; } catch (_) { - throw new HttpException( + throw new BadRequestException( `"${metadata.data}" should be like "id 12, name Anand", provided "${value}"`, - HttpStatus.UNPROCESSABLE_ENTITY, ); } } diff --git a/src/pipes/optional-int.pipe.ts b/src/pipes/optional-int.pipe.ts index 7809528da..ace995631 100644 --- a/src/pipes/optional-int.pipe.ts +++ b/src/pipes/optional-int.pipe.ts @@ -1,9 +1,8 @@ import { - PipeTransform, - Injectable, - HttpException, - HttpStatus, ArgumentMetadata, + BadRequestException, + Injectable, + PipeTransform, } from '@nestjs/common'; /** Convert a string like "1" to a number, but without NaN */ @@ -13,9 +12,8 @@ export class OptionalIntPipe implements PipeTransform { if (value == null) return undefined; const num = Number(value); if (isNaN(num)) - throw new HttpException( + throw new BadRequestException( `"${metadata.data}" should be a number, provided "${value}"`, - HttpStatus.UNPROCESSABLE_ENTITY, ); return num; } diff --git a/src/pipes/order-by.pipe.ts b/src/pipes/order-by.pipe.ts index 6306c2b7e..6cfe45a2d 100644 --- a/src/pipes/order-by.pipe.ts +++ b/src/pipes/order-by.pipe.ts @@ -1,10 +1,9 @@ import { - PipeTransform, - Injectable, - HttpException, - HttpStatus, ArgumentMetadata, BadGatewayException, + BadRequestException, + Injectable, + PipeTransform, } from '@nestjs/common'; /** Convert a string like "name asc, address desc" to { name: "asc", address: "desc" } */ @@ -26,9 +25,8 @@ export class OrderByPipe implements PipeTransform { }); return orderBy; } catch (_) { - throw new HttpException( + throw new BadRequestException( `"${metadata.data}" should be like "key1 asc, key2 desc", provided "${value}"`, - HttpStatus.UNPROCESSABLE_ENTITY, ); } } diff --git a/src/pipes/where.pipe.ts b/src/pipes/where.pipe.ts index db2787f93..043c049bd 100644 --- a/src/pipes/where.pipe.ts +++ b/src/pipes/where.pipe.ts @@ -1,9 +1,8 @@ import { - PipeTransform, - Injectable, - HttpException, - HttpStatus, ArgumentMetadata, + BadRequestException, + Injectable, + PipeTransform, } from '@nestjs/common'; import { parseObjectLiteral } from '../helpers/parse-object-literal'; @@ -25,9 +24,8 @@ export class WherePipe implements PipeTransform { }); return items; } catch (_) { - throw new HttpException( + throw new BadRequestException( `"${metadata.data}" should be like "id 12, name Anand", provided "${value}"`, - HttpStatus.UNPROCESSABLE_ENTITY, ); } }