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

Commit

Permalink
♻️ Add descriptions for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent 727b611 commit d0e5846
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 67 deletions.
98 changes: 48 additions & 50 deletions src/errors/errors.constants.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
export const USER_NOT_FOUND = '404001: User not found';
export const GROUP_NOT_FOUND = '404001: User not found';
export const SESSION_NOT_FOUND = '404001: User not found';
export const EMAIL_NOT_FOUND = '404001: User not found';
export const API_KEY_NOT_FOUND = '404001: User not found';
export const APPROVED_SUBNET_NOT_FOUND = 'Approved subnet not found';
export const AUDIT_LOG_NOT_FOUND = 'Approved subnet not found';
export const DOMAIN_NOT_FOUND = 'Approved subnet not found';
export const MEMBERSHIP_NOT_FOUND = 'Approved subnet not found';
export const BILLING_NOT_FOUND = 'Approved subnet not found';
export const CUSTOMER_NOT_FOUND = 'Approved subnet not found';
export const INVOICE_NOT_FOUND = 'Approved subnet not found';
export const SUBSCRIPTION_NOT_FOUND = 'Approved subnet not found';
export const SOURCE_NOT_FOUND = 'Approved subnet not found';
export const WEBHOOK_NOT_FOUND = 'Approved subnet not found';

export const UNAUTHORIZED_RESOURCE = 'Unauthorized';

export const INVALID_CREDENTIALS = 'INVALID_CREDENTIALS';
export const INVALID_MFA_CODE = 'INVALID_MFA_CODE';
export const NO_TOKEN_PROVIDED = 'No token';
export const INVALID_TOKEN = 'No token';

export const DOMAIN_TXT_NOT_FOUND = '';
export const DOMAIN_HTML_NOT_FOUND = '';

export const UNVERIFIED_EMAIL = 'UNVERIFIED_EMAIL';
export const NO_EMAILS = 'User has no email attached to it';
export const EMAIL_USER_CONFLICT = 'A user with this email already exists';
export const EMAIL_VERIFIED_CONFLICT = 'This email is already verified';

export const GROUP_NOT_FOUND = '404002: Group not found';
export const SESSION_NOT_FOUND = '404003: Session not found';
export const EMAIL_NOT_FOUND = '404004: Email not found';
export const API_KEY_NOT_FOUND = '404005: API key not found';
export const APPROVED_SUBNET_NOT_FOUND = '404006: Approved subnet not found';
export const AUDIT_LOG_NOT_FOUND = '404007: Audit log not found';
export const DOMAIN_NOT_FOUND = '404008: Domain not found';
export const MEMBERSHIP_NOT_FOUND = '404009: Membership not found';
export const BILLING_NOT_FOUND = '404010: Billing not found';
export const CUSTOMER_NOT_FOUND = '404011: Customer not found';
export const INVOICE_NOT_FOUND = '404012: Invoice not found';
export const SUBSCRIPTION_NOT_FOUND = '404013: Subscription not found';
export const SOURCE_NOT_FOUND = '404014: Source not found';
export const WEBHOOK_NOT_FOUND = '404015: Webhook not found';

export const UNAUTHORIZED_RESOURCE = '401000: Insufficient permission';
export const INVALID_CREDENTIALS = '401001: Invalid credentials';
export const INVALID_MFA_CODE = '401002: Invalid one-time code';
export const INVALID_TOKEN = '401003: Invalid token';
export const UNVERIFIED_EMAIL = '401004: Email is not verified';
export const UNVERIFIED_LOCATION = '401005: Location is not verified';
export const MFA_BACKUP_CODE_USED = '401007: Backup code is already used';

export const NO_TOKEN_PROVIDED = '400001: No token provided';
export const DOMAIN_NOT_VERIFIED = '400002: Domain not verified';
export const MFA_PHONE_NOT_FOUND = '400003: Phone number not found';
export const MFA_PHONE_OR_TOKEN_REQUIRED =
'400004: Phone number or token is required';
export const MFA_NOT_ENABLED =
'400005: Multi-factor authentication is not enabled';
export const NO_EMAILS = '400006: User has no email attached to it';

export const EMAIL_USER_CONFLICT =
'409001: User with this email already exists';
export const EMAIL_VERIFIED_CONFLICT = '409002: This email is already verified';
export const BILLING_ACCOUNT_CREATED_CONFLICT =
'409003: Billing account is already created';
export const MFA_ENABLED_CONFLICT =
'Two-factor authentication is already enabled';
export const MFA_NOT_ENABLED = 'MFA_NOT_ENABLED';
export const MFA_BACKUP_CODE_USED = 'MFA_BACKUP_CODE_USED';
export const MFA_PHONE_OR_TOKEN_REQUIRED = 'MFA_BACKUP_CODE_USED';
export const MFA_PHONE_NOT_FOUND = 'MFA_BACKUP_CODE_USED';
export const UNVERIFIED_LOCATION = 'UNVERIFIED_LOCATION';

export const CURRENT_PASSWORD_REQUIRED = 'Current password is required';
'409004: Multi-factor authentication is already enabled';
export const CURRENT_PASSWORD_REQUIRED = '409005: Current password is required';
export const COMPROMISED_PASSWORD =
'This password has been compromised in a data breach.';

export const CANNOT_DELETE_SOLE_MEMBER = '';
export const CANNOT_DELETE_SOLE_OWNER = '';

export const BILLING_ACCOUNT_CREATED_CONFLICT = '';

export const ORDER_BY_ASC_DESC = 'Order should be "ASC" or "DESC"';
export const ORDER_BY_FORMAT = 'Order should be "ASC" or "DESC"';
export const WHERE_PIPE_FORMAT = 'Order should be "ASC" or "DESC"';
export const OPTIONAL_INT_PIPE_NUMBER = '$key should be a number';
export const CURSOR_PIPE_FORMAT = `$key should be like "id 12, name Anand"`;
'409006: This password has been compromised in a data breach.';
export const CANNOT_DELETE_SOLE_MEMBER =
'409007: Cannot remove the only member';
export const CANNOT_DELETE_SOLE_OWNER = '409008: Cannot remove the only owner';
export const ORDER_BY_ASC_DESC = '409009: Invalid sorting order';
export const ORDER_BY_FORMAT = '409010: Invalid ordering format';
export const WHERE_PIPE_FORMAT = '409011: Invalid query format';
export const OPTIONAL_INT_PIPE_NUMBER = '409012: $key should be a number';
export const CURSOR_PIPE_FORMAT = '409013: Invalid cursor format';
2 changes: 1 addition & 1 deletion src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class AuthService {
});
if (!user) throw new NotFoundException(USER_NOT_FOUND);
if (user.twoFactorMethod !== 'NONE')
throw new BadRequestException(MFA_ENABLED_CONFLICT);
throw new ConflictException(MFA_ENABLED_CONFLICT);
if (!user.twoFactorSecret)
user.twoFactorSecret = this.tokensService.generateUuid();
if (!this.authenticator.check(code, user.twoFactorSecret))
Expand Down
7 changes: 3 additions & 4 deletions src/modules/domains/domains.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import {
} from '@prisma/client';
import got from 'got';
import {
DOMAIN_HTML_NOT_FOUND,
DOMAIN_NOT_VERIFIED,
DOMAIN_NOT_FOUND,
DOMAIN_TXT_NOT_FOUND,
UNAUTHORIZED_RESOURCE,
} from 'src/errors/errors.constants';
import { URL } from 'url';
Expand Down Expand Up @@ -126,7 +125,7 @@ export class DomainsService {
where: { id },
data: { isVerified: true },
});
} else throw new BadRequestException(DOMAIN_TXT_NOT_FOUND);
} else throw new BadRequestException(DOMAIN_NOT_VERIFIED);
} else {
let verified = false;
try {
Expand All @@ -142,7 +141,7 @@ export class DomainsService {
where: { id },
data: { isVerified: true },
});
} else throw new BadRequestException(DOMAIN_HTML_NOT_FOUND);
} else throw new BadRequestException(DOMAIN_NOT_VERIFIED);
}
return domain;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
BadRequestException,
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MfaMethod, users } from '@prisma/client';
import { hash } from 'bcrypt';
import {
MFA_ENABLED_CONFLICT,
ConflictException MFA_ENABLED_CONFLICT,
MFA_NOT_ENABLED,
NO_EMAILS,
USER_NOT_FOUND,
Expand Down Expand Up @@ -37,7 +38,7 @@ export class MultiFactorAuthenticationService {
});
if (!enabled) throw new NotFoundException(USER_NOT_FOUND);
if (enabled.twoFactorMethod !== 'NONE')
throw new BadRequestException(MFA_ENABLED_CONFLICT);
throw new ConflictException(MFA_ENABLED_CONFLICT);
return this.auth.getTotpQrCode(userId);
}

Expand All @@ -48,7 +49,7 @@ export class MultiFactorAuthenticationService {
});
if (!enabled) throw new NotFoundException(USER_NOT_FOUND);
if (enabled.twoFactorMethod !== 'NONE')
throw new BadRequestException(MFA_ENABLED_CONFLICT);
throw new ConflictException(MFA_ENABLED_CONFLICT);
const secret = this.tokensService.generateUuid();
await this.prisma.users.update({
where: { id: userId },
Expand All @@ -74,7 +75,7 @@ export class MultiFactorAuthenticationService {
});
if (!user) throw new NotFoundException(USER_NOT_FOUND);
if (user.twoFactorMethod !== 'NONE')
throw new BadRequestException(MFA_ENABLED_CONFLICT);
throw new ConflictException(MFA_ENABLED_CONFLICT);
const secret = this.tokensService.generateUuid();
await this.prisma.users.update({
where: { id: userId },
Expand Down
3 changes: 2 additions & 1 deletion src/modules/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BadRequestException,
ConflictException,
Injectable,
Logger,
NotFoundException,
Expand Down Expand Up @@ -42,7 +43,7 @@ export class StripeService {
if (!group) throw new NotFoundException(GROUP_NOT_FOUND);
const attributes = group.attributes as { stripeCustomerId?: string };
if (attributes?.stripeCustomerId)
throw new BadRequestException(BILLING_ACCOUNT_CREATED_CONFLICT);
throw new ConflictException(BILLING_ACCOUNT_CREATED_CONFLICT);
const result = await this.stripe.customers.create(data);
await this.prisma.groups.update({
where: { id: groupId },
Expand Down
9 changes: 2 additions & 7 deletions src/pipes/cursor.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { parseObjectLiteral } from '../helpers/parse-object-literal';
/** Convert a string like "id: 12, b: 'Anand'" to { id: 12, name: "Anand" } */
@Injectable()
export class CursorPipe implements PipeTransform {
transform(
value: string,
metadata: ArgumentMetadata,
): Record<string, number | string> | undefined {
transform(value: string): Record<string, number | string> | undefined {
if (value == null) return undefined;
try {
const rules = parseObjectLiteral(value);
Expand All @@ -25,9 +22,7 @@ export class CursorPipe implements PipeTransform {
});
return items;
} catch (_) {
throw new BadRequestException(
CURSOR_PIPE_FORMAT.replace('$key', metadata.data),
);
throw new BadRequestException(CURSOR_PIPE_FORMAT);
}
}
}

0 comments on commit d0e5846

Please sign in to comment.