Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Don't use native errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent cbc8034 commit 8e248ff
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/interceptors/audit-log.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadGatewayException,
CallHandler,
ExecutionContext,
Injectable,
Expand Down Expand Up @@ -41,7 +42,7 @@ export class AuditLogger implements NestInterceptor {
const request = context.switchToHttp().getRequest() as UserRequest;
const groupId = parseInt(request.params.id);
if (isNaN(groupId))
throw new Error(`Group ID is not a number: ${request.params.id}`);
throw new BadGatewayException('Group ID is not a number');
const ip = getClientIp(request);
const location = await this.geolocationService.getLocation(ip);
const userAgent = request.get('user-agent');
Expand Down
8 changes: 4 additions & 4 deletions src/modules/domains/domains.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadRequestException,
HttpException,
HttpStatus,
Injectable,
Expand All @@ -13,7 +14,6 @@ import {
domainsWhereUniqueInput,
} from '@prisma/client';
import got from 'got';
import { BadRequestError } from 'passport-headerapikey';
import { DnsService } from '../dns/dns.service';
import { Expose } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
Expand Down Expand Up @@ -121,7 +121,7 @@ export class DomainsService {
where: { id },
data: { isVerified: true },
});
} else throw new BadRequestError('TXT record not found');
} else throw new BadRequestException('TXT record not found');
} else if (method === DOMAIN_VERIFICATION_HTML) {
let verified = false;
try {
Expand All @@ -137,8 +137,8 @@ export class DomainsService {
where: { id },
data: { isVerified: true },
});
} else throw new BadRequestError('HTML file not found');
} else throw new BadRequestError('Type should be TXT or HTML');
} else throw new BadRequestException('HTML file not found');
} else throw new BadRequestException('Type should be TXT or HTML');
return domain;
}

Expand Down
1 change: 0 additions & 1 deletion src/modules/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class EmailService {
constructor(private configService: ConfigService) {
const emailConfig = this.configService.get<EmailConfig>('email');
if (emailConfig) this.config = emailConfig;
else throw new Error('Email configuration not found');
this.transport = nodemailer.createTransport(this.config);
}

Expand Down
8 changes: 8 additions & 0 deletions src/modules/errors/errors.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { ErrorsService } from './errors.service';

@Module({
providers: [ErrorsService],
exports: [ErrorsService],
})
export class ErrorsModule {}
4 changes: 4 additions & 0 deletions src/modules/errors/errors.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class ErrorsService {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadRequestException,
Body,
Controller,
Delete,
Expand All @@ -7,7 +8,6 @@ import {
Post,
} from '@nestjs/common';
import { users } from '@prisma/client';
import { BadRequestError } from 'passport-headerapikey';
import { Expose } from '../../modules/prisma/prisma.interface';
import { Scopes } from '../auth/scope.decorator';
import {
Expand Down Expand Up @@ -70,7 +70,7 @@ export class MultiFactorAuthenticationController {
userId,
body.phone,
);
throw new BadRequestError('Phone number or token is required');
throw new BadRequestException('Phone number or token is required');
}

@Post('email')
Expand Down
1 change: 0 additions & 1 deletion src/modules/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class StripeService {
const stripeApiKey = this.configService.get<string>(
'payments.stripeApiKey',
);
if (!stripeApiKey) throw new Error('Stripe API key not found');
this.stripe = new Stripe(stripeApiKey, {
apiVersion: '2020-08-27',
});
Expand Down
3 changes: 2 additions & 1 deletion src/pipes/order-by.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
HttpException,
HttpStatus,
ArgumentMetadata,
BadGatewayException,
} from '@nestjs/common';

/** Convert a string like "name asc, address desc" to { name: "asc", address: "desc" } */
Expand All @@ -20,7 +21,7 @@ export class OrderByPipe implements PipeTransform {
rules.forEach((rule) => {
const [key, order] = rule.split(' ') as [string, 'asc' | 'desc'];
if (!['asc', 'desc'].includes(order.toLocaleLowerCase()))
throw new Error('Order should be "ASC" or "DESC"');
throw new BadGatewayException('Order should be "ASC" or "DESC"');
orderBy[key] = order.toLocaleLowerCase() as 'asc' | 'desc';
});
return orderBy;
Expand Down

0 comments on commit 8e248ff

Please sign in to comment.