Skip to content

Commit

Permalink
Merge pull request #18 from HubSpot/update-hubspotautherror-definition
Browse files Browse the repository at this point in the history
Update `HubSpotAuthError` definition and usage
  • Loading branch information
camden11 authored Oct 2, 2023
2 parents ea64393 + b1ef837 commit 3ed4496
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
23 changes: 12 additions & 11 deletions errors/HubSpotAuthError.ts
Original file line number Diff line number Diff line change
@@ -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<StatusCodeError> }
) {
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;
}
}
2 changes: 1 addition & 1 deletion errors/standardErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 5 additions & 15 deletions lib/personalAccessKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions types/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface StatusCodeError extends BaseError {
body: {
message?: string;
errors?: Array<StatusCodeError>;
category?: string;
subCategory?: string;
};
headers: {
[key: string]: string;
Expand Down

0 comments on commit 3ed4496

Please sign in to comment.