Skip to content

Commit

Permalink
use isSpecifiedError for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
camden11 committed Mar 12, 2024
1 parent ed89a1c commit 6484c6d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config/config_DEPRECATED.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ function getConfigVariablesFromEnv() {
portalId: parseInt(env[ENVIRONMENT_VARIABLES.HUBSPOT_PORTAL_ID] || '', 10),
refreshToken: env[ENVIRONMENT_VARIABLES.HUBSPOT_REFRESH_TOKEN],
httpTimeout: env[ENVIRONMENT_VARIABLES.HTTP_TIMEOUT]
? parseInt(env[ENVIRONMENT_VARIABLES.HTTP_TIMEOUT] || '')
? parseInt(env[ENVIRONMENT_VARIABLES.HTTP_TIMEOUT] as string)
: undefined,
env: getValidEnv(
env[ENVIRONMENT_VARIABLES.HUBSPOT_ENVIRONMENT] as Environment
Expand Down
21 changes: 19 additions & 2 deletions errors/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@ export function isSpecifiedError(
statusCode,
category,
subCategory,
}: { statusCode?: number; category?: string; subCategory?: string }
code,
}: {
statusCode?: number;
category?: string;
subCategory?: string;
code?: string;
}
): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const error = (err && (err.cause as AxiosError<any>)) || err;
const statusCodeErr = !statusCode || error.response?.status === statusCode;
const categoryErr = !category || error.response?.data?.category === category;
const subCategoryErr =
!subCategory || error.response?.data?.subCategory === subCategory;
const codeError = !code || error.code === code;

return error.isAxiosError && statusCodeErr && categoryErr && subCategoryErr;
return (
error.isAxiosError &&
statusCodeErr &&
categoryErr &&
subCategoryErr &&
codeError
);
}

export function isMissingScopeError(err: Error | AxiosError): boolean {
Expand All @@ -39,6 +52,10 @@ export function isGatingError(err: Error | AxiosError): boolean {
return isSpecifiedError(err, { statusCode: 403, category: 'GATED' });
}

export function isTimeoutError(err: Error | AxiosError): boolean {
return isSpecifiedError(err, { code: 'ETIMEDOUT' });
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isApiUploadValidationError(err: AxiosError<any>): boolean {
return (
Expand Down
9 changes: 3 additions & 6 deletions lib/fileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
FileMapperInputOptions,
} from '../types/Files';
import { throwFileSystemError } from '../errors/fileSystemErrors';
import { isTimeoutError } from '../errors/apiErrors';
import { BaseError } from '../types/Error';
import { i18n } from '../utils/lang';

Expand Down Expand Up @@ -264,10 +265,6 @@ async function writeFileMapperNode(
return true;
}

function isTimeout(err: BaseError): boolean {
return !!err && (err.status === 408 || err.code === 'ETIMEDOUT');
}

async function downloadFile(
accountId: number,
src: string,
Expand Down Expand Up @@ -308,7 +305,7 @@ async function downloadFile(
);
} catch (err) {
const error = err as AxiosError;
if (isHubspot && isTimeout(error)) {
if (isHubspot && isTimeoutError(error)) {
throwErrorWithMessage(`${i18nKey}.errors.assetTimeout`, {}, error);
} else {
throwErrorWithMessage(
Expand Down Expand Up @@ -393,7 +390,7 @@ async function downloadFolder(
}
} catch (err) {
const error = err as AxiosError;
if (isTimeout(error)) {
if (isTimeoutError(error)) {
throwErrorWithMessage(`${i18nKey}.errors.assetTimeout`, {}, error);
} else {
throwErrorWithMessage(
Expand Down

0 comments on commit 6484c6d

Please sign in to comment.