Skip to content

Commit

Permalink
Merge pull request #5 from chvarkov/develop
Browse files Browse the repository at this point in the history
Updated GoogleRecaptchaException.
  • Loading branch information
chvarkov authored Oct 23, 2020
2 parents 2de48d0 + 599c695 commit d0d98e0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 52 deletions.
50 changes: 2 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Google recaptcha guard will throw GoogleRecaptchaException on error.

`GoogleRecaptchaException` has data with google recaptcha error codes.

`GoogleRecaptchaException``BadRequestException``HttpException``Error`.
`GoogleRecaptchaException``HttpException``Error`.

You can handle it via [ExceptionFilter](https://docs.nestjs.com/exception-filters).

Expand All @@ -81,53 +81,7 @@ Example exception filter implementation.
@Catch(GoogleRecaptchaException)
export class GoogleRecaptchaFilter implements ExceptionFilter {
catch(exception: GoogleRecaptchaException, host: ArgumentsHost): any {
const res: Response = host.switchToHttp().getResponse();

const firstErrorCode = exception.errorCodes.shift();

const transformedError = this.transformError(firstErrorCode);

res.status(transformedError.status).send({
type: 'GoogleRecaptchaError',
message: transformedError.message,
});
}

transformError(errorCode: ErrorCode): {status: number, message: string} {
switch (errorCode) {
case ErrorCode.InvalidInputResponse:
return {
status: HttpStatus.BAD_REQUEST,
message: 'The response parameter is invalid or malformed.',
};

case ErrorCode.MissingInputResponse:
return {
status: HttpStatus.BAD_REQUEST,
message: 'The response parameter is missing.',
};

case ErrorCode.TimeoutOrDuplicate:
return {
status: HttpStatus.BAD_REQUEST,
message: 'The response is no longer valid: either is too old or has been used previously.',
};

case ErrorCode.InvalidInputSecret:
case ErrorCode.MissingInputSecret:
return {
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: 'Invalid module configuration. Please check public-secret keys.',
};

case ErrorCode.UnknownError:
case ErrorCode.BadRequest:
default:
return {
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: 'Unexpected error. Please submit issue to @nestlab/google-recaptcha.',
};
}
// TODO: Your exception filter implementation
}
}

Expand Down
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.1.0",
"version": "1.1.1",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nest",
Expand Down
36 changes: 33 additions & 3 deletions src/exceptions/google-recaptcha.exception.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { BadRequestException } from '@nestjs/common';
import { HttpException, HttpStatus } from '@nestjs/common';
import { ErrorCode } from '../enums/error-code';

export class GoogleRecaptchaException extends BadRequestException {
export class GoogleRecaptchaException extends HttpException {

constructor(public readonly errorCodes: ErrorCode[]) {
super(`Google recaptcha errors: ${errorCodes.join(', ')}.`);
super(GoogleRecaptchaException.getErrorMessage(errorCodes[0]), GoogleRecaptchaException.getErrorStatus(errorCodes[0]));
}

private static getErrorMessage(errorCode: ErrorCode): string {
switch (errorCode) {
case ErrorCode.InvalidInputResponse:
return 'The response parameter is invalid or malformed.';

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

case ErrorCode.InvalidInputSecret:
case ErrorCode.MissingInputSecret:
return 'Invalid module configuration. Please check public-secret keys.';

case ErrorCode.UnknownError:
case ErrorCode.BadRequest:
default:
return 'Unexpected error. Please submit issue to @nestlab/google-recaptcha.';
}
}

private static getErrorStatus(errorCode: ErrorCode): number {
return errorCode === ErrorCode.InvalidInputResponse ||
errorCode === ErrorCode.MissingInputResponse ||
errorCode === ErrorCode.TimeoutOrDuplicate
? HttpStatus.BAD_REQUEST
: HttpStatus.INTERNAL_SERVER_ERROR;
}
}

0 comments on commit d0d98e0

Please sign in to comment.