From 1db35122c969800953750ea379621ab932d22352 Mon Sep 17 00:00:00 2001 From: jackkav Date: Tue, 15 Oct 2024 09:57:24 +0200 Subject: [PATCH 1/2] fallback timeout --- packages/insomnia/src/ui/insomniaFetch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/insomnia/src/ui/insomniaFetch.ts b/packages/insomnia/src/ui/insomniaFetch.ts index 4c9ac789b91..f4a0148d248 100644 --- a/packages/insomnia/src/ui/insomniaFetch.ts +++ b/packages/insomnia/src/ui/insomniaFetch.ts @@ -34,7 +34,7 @@ export async function insomniaFetch({ origin, headers, onlyResolveOnSuccess = false, - timeout = INSOMNIA_FETCH_TIME_OUT, + timeout, }: FetchConfig): Promise { const config: RequestInit = { method, @@ -49,7 +49,7 @@ export async function insomniaFetch({ ...(PLAYWRIGHT ? { 'X-Mockbin-Test': 'true' } : {}), }, ...(data ? { body: JSON.stringify(data) } : {}), - signal: AbortSignal.timeout(timeout), + signal: AbortSignal.timeout(timeout || INSOMNIA_FETCH_TIME_OUT), }; if (sessionId === undefined) { throw new Error(`No session ID provided to ${method}:${path}`); From 6ad7236f655edbc4b4a8b37e1c37b9863fe08524 Mon Sep 17 00:00:00 2001 From: jackkav Date: Tue, 15 Oct 2024 10:23:41 +0200 Subject: [PATCH 2/2] move inconsistent error parsing to the fetcher --- .../invite-modal/organization-invite-form.tsx | 24 ++++------- packages/insomnia/src/ui/insomniaFetch.ts | 42 +++++++------------ 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/packages/insomnia/src/ui/components/modals/invite-modal/organization-invite-form.tsx b/packages/insomnia/src/ui/components/modals/invite-modal/organization-invite-form.tsx index f74efe1bbe8..dc26fdb4673 100644 --- a/packages/insomnia/src/ui/components/modals/invite-modal/organization-invite-form.tsx +++ b/packages/insomnia/src/ui/components/modals/invite-modal/organization-invite-form.tsx @@ -6,7 +6,7 @@ import { decryptRSAWithJWK, encryptRSAWithJWK } from '../../../../account/crypt' import { getCurrentSessionId, getPrivateKey } from '../../../../account/session'; import { SegmentEvent } from '../../../analytics'; import useStateRef from '../../../hooks/use-state-ref'; -import { insomniaFetch, ResponseFailError } from '../../../insomniaFetch'; +import { insomniaFetch } from '../../../insomniaFetch'; import { Icon } from '../../icon'; import { type PendingMember, updateInvitationRole } from './invite-modal'; import { OrganizationMemberRolesSelector, type Role, SELECTOR_TYPE } from './organization-member-roles-selector'; @@ -240,25 +240,19 @@ async function getInviteInstruction( sessionId: await getCurrentSessionId(), onlyResolveOnSuccess: true, }).catch(async error => { - if (error instanceof ResponseFailError && error.response.headers.get('content-type')?.includes('application/json')) { - let json; - try { - json = await error.response.json(); - } catch (e) { - throw new Error(`Failed to get invite instruction for ${inviteeEmail}`); - } - if (json?.error === NEEDS_TO_UPGRADE_ERROR) { + if (error.message && error.error) { + if (error?.error === NEEDS_TO_UPGRADE_ERROR) { throw new Error( `You are currently on the Free plan where you can invite as many collaborators as you want only as long as you don’t have more than one project. Since you have more than one project, you need to upgrade to Individual or above to continue.` ); } - if (json?.error === NEEDS_TO_INCREASE_SEATS_ERROR) { + if (error?.error === NEEDS_TO_INCREASE_SEATS_ERROR) { throw new Error(needToIncreaseSeatErrMsg); } - if (json?.message) { - throw new Error(json.message); + if (error?.message) { + throw new Error(error.message); } } throw new Error(`Failed to get invite instruction for ${inviteeEmail}`); @@ -455,11 +449,7 @@ async function inviteUserToOrganization( }).then( () => inviteeEmail, async error => { - let errMsg = `Failed to invite ${inviteeEmail}`; - if (error instanceof ResponseFailError && error.message) { - errMsg = error.message; - } - throw new Error(errMsg); + throw new Error(error.message || `Failed to invite ${inviteeEmail}`); } ); } diff --git a/packages/insomnia/src/ui/insomniaFetch.ts b/packages/insomnia/src/ui/insomniaFetch.ts index f4a0148d248..f51c3243a06 100644 --- a/packages/insomnia/src/ui/insomniaFetch.ts +++ b/packages/insomnia/src/ui/insomniaFetch.ts @@ -11,17 +11,8 @@ interface FetchConfig { retries?: number; origin?: string; headers?: Record; - onlyResolveOnSuccess?: boolean; timeout?: number; -} - -export class ResponseFailError extends Error { - constructor(msg: string, response: Response) { - super(msg); - this.response = response; - } - response; - name = 'ResponseFailError'; + onlyResolveOnSuccess?: boolean; } // Adds headers, retries and opens deep links returned from the api @@ -33,8 +24,8 @@ export async function insomniaFetch({ organizationId, origin, headers, - onlyResolveOnSuccess = false, timeout, + onlyResolveOnSuccess, }: FetchConfig): Promise { const config: RequestInit = { method, @@ -61,27 +52,26 @@ export async function insomniaFetch({ if (uri) { window.main.openDeepLink(uri); } - const isJson = - response.headers.get('content-type')?.includes('application/json') || - path.match(/\.json$/); - if (onlyResolveOnSuccess && !response.ok) { - let errMsg = ''; - if (isJson) { - try { - const json = await response.json(); - if (typeof json?.message === 'string') { - errMsg = json.message; - } - } catch (err) {} + const contentType = response.headers.get('content-type'); + const isJson = contentType?.includes('application/json') || path.match(/\.json$/); + + // assume the backend is sending us a meaningful error message + if (isJson && !response.ok && onlyResolveOnSuccess) { + try { + const json = await response.json(); + throw ({ + message: json?.message || '', + error: json?.error || '', + }); + } catch (err) { + throw err; } - throw new ResponseFailError(errMsg, response); } return isJson ? response.json() : response.text(); } catch (err) { if (err.name === 'AbortError') { throw new Error('insomniaFetch timed out'); - } else { - throw err; } + throw err; } }