-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from chvarkov/develop
Updated GoogleRecaptchaException.
- Loading branch information
Showing
3 changed files
with
36 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |