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

feat: Wrap AxiosErrors in custom error object #151

Merged
merged 17 commits into from
Jul 25, 2024
Merged
Changes from 6 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
55 changes: 54 additions & 1 deletion http/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import path from 'path';
import fs from 'fs-extra';
import contentDisposition from 'content-disposition';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import axios, {
AxiosRequestConfig,
AxiosResponse,
InternalAxiosRequestConfig,
isAxiosError,
} from 'axios';

import { getAccountConfig } from '../config';
import { USER_AGENTS, getAxiosConfig } from './getAxiosConfig';
Expand All @@ -15,6 +20,54 @@ import { i18n } from '../utils/lang';

const i18nKey = 'http.index';

export class HubSpotHttpError<T = unknown, D = unknown> extends Error {
joe-yeager marked this conversation as resolved.
Show resolved Hide resolved
public status?: number;
public response?: AxiosResponse<T, D>;
public config?: InternalAxiosRequestConfig<D>;
public code?: string;
public request?: unknown;
public statusText?: string;
public data?: T;

// Add this in so any pre-existing checks for error.isAxiosError continue to
// function until they can be ported over to the new error check
public isAxiosError = true;

constructor(message?: string, options?: ErrorOptions) {
super(message, options);
this.name = 'HubSpotHttpError';

if (options && isAxiosError(options.cause)) {
// Add these for backwards compatibility until we have updated all the checks
// in the CLI for the Axios implementation details and then we can remove these or
// keep whatever we find useful in custom fields on this object
const { response, request, config, code } = options.cause;
this.response = response;
this.request = request;
this.config = config;

// Add any custom fields we feel are necessary or make our
// collective lives easier
this.code = code;
if (response) {
this.status = response.status;
this.statusText = response.statusText;
this.data = response.data;
}
}
}
}

export function isHubSpotHttpError(error?: unknown): error is HubSpotHttpError {
return !!error && error instanceof HubSpotHttpError;
}

axios.interceptors.response.use(undefined, error => {
// Wrap all axios errors in our own Error class. Attach the error
// as the cause for the new error, so we maintain the stack trace
return Promise.reject(new HubSpotHttpError(error.message, { cause: error }));
});

export function addUserAgentHeader(key: string, value: string) {
USER_AGENTS[key] = value;
}
Expand Down
Loading