Skip to content

Commit

Permalink
Merge pull request #3 from chvarkov/develop
Browse files Browse the repository at this point in the history
Reworked error handling.
  • Loading branch information
chvarkov authored Oct 23, 2020
2 parents 42cab35 + 1582166 commit a05ec9d
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestlab/google-recaptcha",
"version": "1.0.13",
"version": "1.0.14",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nest",
Expand Down
8 changes: 8 additions & 0 deletions src/exceptions/google-recaptcha.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ForbiddenException } from '@nestjs/common';
import { ErrorCode } from '../enums/error-code';

export class GoogleRecaptchaException extends ForbiddenException {
constructor(public readonly errorCodes: ErrorCode[]) {
super();
}
}
50 changes: 3 additions & 47 deletions src/guards/google-recaptcha.guard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import {
BadGatewayException,
BadRequestException,
CanActivate,
ExecutionContext,
ForbiddenException, HttpException,
Inject,
Injectable
} from '@nestjs/common';
import { CanActivate, ExecutionContext, Inject, Injectable } from '@nestjs/common';
import { GoogleRecaptchaValidator } from '../services/google-recaptcha.validator';
import { GoogleRecaptchaGuardOptions } from '../interfaces/google-recaptcha-guard-options';
import { RECAPTCHA_OPTIONS } from '../provider.declarations';
import { ErrorCode } from '../enums/error-code';
import { GoogleRecaptchaException } from '../exceptions/google-recaptcha.exception';

@Injectable()
export class GoogleRecaptchaGuard implements CanActivate {
Expand All @@ -35,42 +27,6 @@ export class GoogleRecaptchaGuard implements CanActivate {
return true;
}

const error = this.options.onError
? this.options.onError(result.errors)
: this.errorHandler(result.errors);

if (error instanceof Error) {
throw error;
}

throw new BadRequestException(error);
}

errorHandler(errorCodes: ErrorCode[]): string | HttpException {
const first = errorCodes.shift();

switch (first) {
case ErrorCode.MissingInputSecret:
return 'The secret parameter is missing.';

case ErrorCode.InvalidInputSecret:
return 'The secret parameter is invalid or malformed.';

case ErrorCode.MissingInputResponse:
return 'The response parameter is missing.';

case ErrorCode.InvalidInputResponse:
return 'The response parameter is invalid or malformed.';

case ErrorCode.BadRequest:
return 'The request is invalid or malformed.';

case ErrorCode.TimeoutOrDuplicate:
return 'The response is no longer valid: either is too old or has been used previously.';

case ErrorCode.UnknownError:
default:
return new BadGatewayException('Unknown error when checking captcha.');
}
throw new GoogleRecaptchaException(result.errors);
}
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export { GoogleRecaptchaGuard } from './guards/google-recaptcha.guard';
export { GoogleRecaptchaModule } from './google-recaptcha.module';
export { GoogleRecaptchaModuleOptions } from './interfaces/google-recaptcha-module-options';
export { ErrorCode } from './enums/error-code';
export { ErrorHandler } from './types';
3 changes: 0 additions & 3 deletions src/interfaces/google-recaptcha-guard-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { ErrorHandler } from '../types';

export interface GoogleRecaptchaGuardOptions {
response: (req) => string | Promise<string>;
skipIf?: () => boolean | Promise<boolean>;
onError?: ErrorHandler;
}
3 changes: 0 additions & 3 deletions src/interfaces/google-recaptcha-validator-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { ErrorHandler } from '../types';

export interface GoogleRecaptchaValidatorOptions {
secretKey: string;
onError?: ErrorHandler,
}
4 changes: 0 additions & 4 deletions src/types.ts

This file was deleted.

7 changes: 2 additions & 5 deletions test/google-recaptcha-module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test } from '@nestjs/testing';
import { BadRequestException, INestApplication } from '@nestjs/common';
import { INestApplication } from '@nestjs/common';
import { GoogleRecaptchaValidator } from '../src/services/google-recaptcha.validator';
import { GoogleRecaptchaGuard } from '../src/guards/google-recaptcha.guard';
import { GoogleRecaptchaModule } from '../src/google-recaptcha.module';
Expand All @@ -13,10 +13,7 @@ describe('Google recaptcha module', () => {
GoogleRecaptchaModule.forRoot({
secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
response: req => req.headers.authorization,
skipIf: req => process.env.NODE_ENV !== 'production',
onError: () => {
throw new BadRequestException('Invalid recaptcha.')
}
skipIf: () => process.env.NODE_ENV !== 'production',
})
],
}).compile();
Expand Down

0 comments on commit a05ec9d

Please sign in to comment.