diff --git a/errors/HubSpotAuthError.ts b/errors/HubSpotAuthError.ts index 298301b4..d9b9844a 100644 --- a/errors/HubSpotAuthError.ts +++ b/errors/HubSpotAuthError.ts @@ -1,20 +1,21 @@ -type ErrorResponse = { - statusCode: number; - body: { category: string; subCategory: string }; -}; +import { StatusCodeError } from '../types/Error'; export class HubSpotAuthError extends Error { - statusCode: number; + statusCode?: number; category?: string; subCategory?: string; - - constructor(message: string, errorResponse: ErrorResponse) { + constructor( + message: string, + { cause = {} }: { cause?: Partial } + ) { super(message); this.name = 'HubSpotAuthError'; - this.statusCode = errorResponse.statusCode; - this.category = - (errorResponse.body && errorResponse.body.category) || undefined; + this.statusCode = cause.statusCode; + this.category = cause?.response?.body?.category || undefined; this.subCategory = - (errorResponse.body && errorResponse.body.subCategory) || undefined; + (cause.response && + cause.response.body && + cause.response.body.subCategory) || + undefined; } } diff --git a/errors/standardErrors.ts b/errors/standardErrors.ts index c1f82b12..be5c4c24 100644 --- a/errors/standardErrors.ts +++ b/errors/standardErrors.ts @@ -53,7 +53,7 @@ export function throwTypeErrorWithMessage( export function throwAuthErrorWithMessage( identifier: string, interpolation?: { [key: string]: string | number }, - cause?: BaseError + cause?: StatusCodeError ): never { genericThrowErrorWithMessage( // @ts-expect-error HubSpotAuthError is not callable diff --git a/lang/en.lyaml b/lang/en.lyaml index 521dfc71..8f52626c 100644 --- a/lang/en.lyaml +++ b/lang/en.lyaml @@ -181,7 +181,6 @@ en: personalAccessKey: accountNotFound: "Account with id {{ accountId }} does not exist." invalidPersonalAccessKey: "Error while retrieving new access token: {{ errorMessage }}" - invalidPersonalAccessKey401: 'Error while retrieving new access token: {{ errorMessage }}. Your personal access key is invalid. Please run "hs auth personalaccesskey" to reauthenticate. See https://designers.hubspot.com/docs/personal-access-keys for more information.' templates: fileAnnotations: "Error reading file annotations {{ file }}" pathExists: "The {{ path }} path already exists" @@ -219,7 +218,7 @@ en: functions: updateExistingConfig: configIsNotObjectError: "The existing {{ configFilePath }} is not an object" - endpointAreadyExistsError: "The endpoint {{ endpointPath }} already exists in {{ configFilePath }}" + endpointAreadyExistsError: "The endpoint {{ endpointPath }} already exists in {{ configFilePath }}" createFunction: nestedConfigError: "Cannot create a functions directory inside '{{ ancestorConfigPath }}'" jsFileConflictError: "The JavaScript file at '{{ functionFilePath }}'' already exists" diff --git a/lib/personalAccessKey.ts b/lib/personalAccessKey.ts index 9a5351b5..e2eaf7f3 100644 --- a/lib/personalAccessKey.ts +++ b/lib/personalAccessKey.ts @@ -44,21 +44,11 @@ export async function getAccessToken( } catch (e) { const error = e as StatusCodeError; if (error.response) { - if (error.response.statusCode === 401) { - // Before adjusting the error message below, please verify that changes do not break regex match in cli/commands/sandbox/delete.js - // For future changes: if response.statusCode is passed into the new error below, sandboxes can skip the regex check and pull the statusCode instead - throwAuthErrorWithMessage( - 'personalAccessKey.invalidPersonalAccessKey401', - { errorMessage: error.response.body.message || '' }, - error - ); - } else { - throwAuthErrorWithMessage( - 'personalAccessKey.invalidPersonalAccessKey', - { errorMessage: error.response.body.message || '' }, - error - ); - } + throwAuthErrorWithMessage( + 'personalAccessKey.invalidPersonalAccessKey', + { errorMessage: error.response.body.message || '' }, + error + ); } else { throwError(e as BaseError); } diff --git a/types/Error.ts b/types/Error.ts index 3f07d418..f6ead280 100644 --- a/types/Error.ts +++ b/types/Error.ts @@ -27,6 +27,8 @@ export interface StatusCodeError extends BaseError { body: { message?: string; errors?: Array; + category?: string; + subCategory?: string; }; headers: { [key: string]: string;