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

chore: handle 408 error and show message [gh-824] #825

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Changes from all 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
39 changes: 28 additions & 11 deletions packages/cli/src/rest/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosInstance } from 'axios'
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
import config from '../services/config'
import { assignProxy } from '../services/util'
import Accounts from './accounts'
Expand Down Expand Up @@ -46,26 +46,43 @@ export async function validateAuthentication (): Promise<void> {
}
}

export function requestInterceptor (config: InternalAxiosRequestConfig) {
const { Authorization, accountId } = getDefaults()
if (Authorization && config.headers) {
config.headers.Authorization = Authorization
}

if (accountId && config.headers) {
config.headers['x-checkly-account'] = accountId
}

return config
}

export function responseErrorInterceptor (error: any) {
if (error.response?.status === 408) {
throw new Error('Encountered an error connecting to Checkly. ' +
'This can be triggered by a slow internet connection or a network with high packet loss.')
}
throw error
}

function init (): AxiosInstance {
const { baseURL } = getDefaults()
const axiosConf = assignProxy(baseURL, { baseURL })

const api = axios.create(axiosConf)

api.interceptors.request.use(function (config) {
const { Authorization, accountId } = getDefaults()
if (Authorization && config.headers) {
config.headers.Authorization = Authorization
}
api.interceptors.request.use(requestInterceptor)

if (accountId && config.headers) {
config.headers['x-checkly-account'] = accountId
}
api.interceptors.response.use(
response => response,
responseErrorInterceptor,
)

return config
})
return api
}

export const api = init()

export const accounts = new Accounts(api)
Expand Down
Loading