Skip to content

Commit

Permalink
fix: parse network error code
Browse files Browse the repository at this point in the history
  • Loading branch information
chvarkov committed Sep 10, 2024
1 parent 60374ff commit dad24b7
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/helpers/get-error-info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import * as axios from 'axios';
import { LiteralObject } from '../interfaces/literal-object';

export function isAxiosError(error: Error | axios.AxiosError): error is axios.AxiosError {
export function isAxiosError<T = LiteralObject>(error: Error | axios.AxiosError<T>): error is axios.AxiosError<T> {
return (<axios.AxiosError>error).isAxiosError;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class GoogleRecaptchaEnterpriseValidator extends AbstractGoogleRecaptchaV

return this.axios.post<VerifyResponseEnterprise>(url, body, config)
.then((res) => res.data)
.then((data): VerifyResponse => {
.then((data: VerifyResponseEnterprise): VerifyResponse => {
if (this.options.valueOf.debug) {
this.logger.debug(data, `${GoogleRecaptchaContext.GoogleRecaptchaEnterprise}.response`);
}
Expand All @@ -110,7 +110,7 @@ export class GoogleRecaptchaEnterpriseValidator extends AbstractGoogleRecaptchaV
this.logger.debug(getErrorInfo(err), `${GoogleRecaptchaContext.GoogleRecaptchaEnterprise}.error`);
}

const networkErrorCode = err.isAxiosError && err.code;
const networkErrorCode = err.isAxiosError && !err.response && err.code;

if (networkErrorCode) {
throw new GoogleRecaptchaNetworkException(networkErrorCode);
Expand Down
4 changes: 2 additions & 2 deletions src/services/validators/google-recaptcha.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { RecaptchaVerificationResult } from '../../models/recaptcha-verification
import { GoogleRecaptchaContext } from '../../enums/google-recaptcha-context';
import { getErrorInfo } from '../../helpers/get-error-info';
import { AxiosInstance } from 'axios';
import { RecaptchaConfigRef } from "../../models/recaptcha-config-ref";
import { RecaptchaConfigRef } from '../../models/recaptcha-config-ref';

@Injectable()
export class GoogleRecaptchaValidator extends AbstractGoogleRecaptchaValidator<VerifyResponseV3> {
Expand Down Expand Up @@ -105,7 +105,7 @@ export class GoogleRecaptchaValidator extends AbstractGoogleRecaptchaValidator<V
this.logger.debug(getErrorInfo(err), `${GoogleRecaptchaContext.GoogleRecaptcha}.error`);
}

const networkErrorCode = err.isAxiosError && err.code;
const networkErrorCode = err.isAxiosError && !err.response && err.code;

if (networkErrorCode) {
throw new GoogleRecaptchaNetworkException(networkErrorCode);
Expand Down
1 change: 1 addition & 0 deletions test/load-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ describe('loadModule', () => {

test('failed load', () => {
expect(() => loadModule('@unknown/unknown-package', true)).toThrowError('Cannot find module');
expect(() => loadModule('@unknown/unknown-package', false)).toThrowError('Cannot find module');
});
});

0 comments on commit dad24b7

Please sign in to comment.