From b363c59cac1aa1875d9693c4b60ed358d6d02a33 Mon Sep 17 00:00:00 2001 From: Adam Wang Date: Wed, 19 Jul 2023 13:54:39 -0400 Subject: [PATCH 1/3] Update HubSpotAuthError class --- errors/HubSpotAuthError.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/errors/HubSpotAuthError.ts b/errors/HubSpotAuthError.ts index 6843e855..0f5c1b5d 100644 --- a/errors/HubSpotAuthError.ts +++ b/errors/HubSpotAuthError.ts @@ -1,6 +1,16 @@ +import { FullResponse } from 'request-promise-native'; + export class HubSpotAuthError extends Error { - constructor(message: string) { + statusCode?: number; + category?: string; + subCategory?: string; + constructor(message: string, errorResponse: Partial = {}) { super(message); this.name = 'HubSpotAuthError'; + this.statusCode = errorResponse && errorResponse.statusCode; + this.category = + (errorResponse.body && errorResponse.body.category) || undefined; + this.subCategory = + (errorResponse.body && errorResponse.body.subCategory) || undefined; } } From 5ef0109799cefb51c1f4b1ae2468024646a77a64 Mon Sep 17 00:00:00 2001 From: Adam Wang Date: Wed, 19 Jul 2023 16:32:23 -0400 Subject: [PATCH 2/3] Update usage of HubSpotAuthError to use cause param --- errors/HubSpotAuthError.ts | 17 ++++++++++++----- errors/standardErrors.ts | 2 +- lang/en.lyaml | 1 - lib/personalAccessKey.ts | 20 +++++--------------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/errors/HubSpotAuthError.ts b/errors/HubSpotAuthError.ts index 0f5c1b5d..6f3cd223 100644 --- a/errors/HubSpotAuthError.ts +++ b/errors/HubSpotAuthError.ts @@ -1,16 +1,23 @@ -import { FullResponse } from 'request-promise-native'; +import { StatusCodeError } from '../types/Error'; export class HubSpotAuthError extends Error { statusCode?: number; category?: string; subCategory?: string; - constructor(message: string, errorResponse: Partial = {}) { + constructor( + message: string, + { cause = {} }: { cause?: Partial } + ) { super(message); this.name = 'HubSpotAuthError'; - this.statusCode = errorResponse && errorResponse.statusCode; + this.statusCode = cause.statusCode; this.category = - (errorResponse.body && errorResponse.body.category) || undefined; + (cause.response && cause.response.body && 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 aa1ed4f4..d912d682 100644 --- a/lang/en.lyaml +++ b/lang/en.lyaml @@ -125,7 +125,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.' utils: git: configIgnore: "Unable to determine if config file is properly ignored by git." diff --git a/lib/personalAccessKey.ts b/lib/personalAccessKey.ts index 6bf101aa..6d4c8928 100644 --- a/lib/personalAccessKey.ts +++ b/lib/personalAccessKey.ts @@ -38,21 +38,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); } From b1ef8379e53dc6d6a915300fd2afc7ca01818f32 Mon Sep 17 00:00:00 2001 From: Camden Phalen Date: Mon, 2 Oct 2023 17:04:58 -0400 Subject: [PATCH 3/3] fix type errors --- errors/HubSpotAuthError.ts | 4 +--- lib/personalAccessKey.ts | 2 +- types/Error.ts | 2 ++ 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/errors/HubSpotAuthError.ts b/errors/HubSpotAuthError.ts index 6f3cd223..d9b9844a 100644 --- a/errors/HubSpotAuthError.ts +++ b/errors/HubSpotAuthError.ts @@ -11,9 +11,7 @@ export class HubSpotAuthError extends Error { super(message); this.name = 'HubSpotAuthError'; this.statusCode = cause.statusCode; - this.category = - (cause.response && cause.response.body && cause.response.body.category) || - undefined; + this.category = cause?.response?.body?.category || undefined; this.subCategory = (cause.response && cause.response.body && diff --git a/lib/personalAccessKey.ts b/lib/personalAccessKey.ts index ac2d6f6b..e2eaf7f3 100644 --- a/lib/personalAccessKey.ts +++ b/lib/personalAccessKey.ts @@ -46,7 +46,7 @@ export async function getAccessToken( if (error.response) { throwAuthErrorWithMessage( 'personalAccessKey.invalidPersonalAccessKey', - { errorMessage: error.response.body.message }, + { errorMessage: error.response.body.message || '' }, error ); } else { 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;