@nestlab/google-recaptcha
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,6 +0,8 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { HttpException } from '@nestjs/common'; | ||
import { ErrorCode } from '../enums/error-code'; | ||
export declare class GoogleRecaptchaException extends BadRequestException { | ||
export declare class GoogleRecaptchaException extends HttpException { | ||
readonly errorCodes: ErrorCode[]; | ||
constructor(errorCodes: ErrorCode[]); | ||
private static getErrorMessage; | ||
private static getErrorStatus; | ||
} |
@@ -5,9 +5,34 @@ "use strict"; | ||
const common_1 = require("@nestjs/common"); | ||
class GoogleRecaptchaException extends common_1.BadRequestException { | ||
const error_code_1 = require("../enums/error-code"); | ||
class GoogleRecaptchaException extends common_1.HttpException { | ||
constructor(errorCodes) { | ||
super(`Google recaptcha errors: ${errorCodes.join(', ')}.`); | ||
super(GoogleRecaptchaException.getErrorMessage(errorCodes[0]), GoogleRecaptchaException.getErrorStatus(errorCodes[0])); | ||
this.errorCodes = errorCodes; | ||
} | ||
static getErrorMessage(errorCode) { | ||
switch (errorCode) { | ||
case error_code_1.ErrorCode.InvalidInputResponse: | ||
return 'The response parameter is invalid or malformed.'; | ||
case error_code_1.ErrorCode.MissingInputResponse: | ||
return 'The response parameter is missing.'; | ||
case error_code_1.ErrorCode.TimeoutOrDuplicate: | ||
return 'The response is no longer valid: either is too old or has been used previously.'; | ||
case error_code_1.ErrorCode.InvalidInputSecret: | ||
case error_code_1.ErrorCode.MissingInputSecret: | ||
return 'Invalid module configuration. Please check public-secret keys.'; | ||
case error_code_1.ErrorCode.UnknownError: | ||
case error_code_1.ErrorCode.BadRequest: | ||
default: | ||
return 'Unexpected error. Please submit issue to @nestlab/google-recaptcha.'; | ||
} | ||
} | ||
static getErrorStatus(errorCode) { | ||
return errorCode === error_code_1.ErrorCode.InvalidInputResponse || | ||
errorCode === error_code_1.ErrorCode.MissingInputResponse || | ||
errorCode === error_code_1.ErrorCode.TimeoutOrDuplicate | ||
? common_1.HttpStatus.BAD_REQUEST | ||
: common_1.HttpStatus.INTERNAL_SERVER_ERROR; | ||
} | ||
} | ||
exports.GoogleRecaptchaException = GoogleRecaptchaException; | ||
//# sourceMappingURL=google-recaptcha.exception.js.map |
{ | ||
"name": "@nestlab/google-recaptcha", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Google recaptcha module for NestJS.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -73,3 +73,3 @@ # Google recaptcha module | ||
`GoogleRecaptchaException` ← `BadRequestException` ← `HttpException` ← `Error`. | ||
`GoogleRecaptchaException` ← `HttpException` ← `Error`. | ||
@@ -85,50 +85,4 @@ You can handle it via [ExceptionFilter](https://docs.nestjs.com/exception-filters). | ||
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, | ||
}); | ||
// TODO: Your exception filter implementation | ||
} | ||
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.', | ||
}; | ||
} | ||
} | ||
} | ||
@@ -135,0 +89,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24139
277
105