Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HubSpotAuthError definition and usage #18

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion errors/HubSpotAuthError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { StatusCodeError } from '../types/Error';

export class HubSpotAuthError extends Error {
constructor(message: string) {
statusCode?: number;
category?: string;
subCategory?: string;
constructor(
message: string,
{ cause = {} }: { cause?: Partial<StatusCodeError> }
) {
super(message);
this.name = 'HubSpotAuthError';
this.statusCode = cause.statusCode;
this.category =
(cause.response && cause.response.body && cause.response.body.category) ||
undefined;
this.subCategory =
(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
1 change: 0 additions & 1 deletion lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
20 changes: 5 additions & 15 deletions lib/personalAccessKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down