From e99b48d7ef5781ae538a0dc3f323608199f397fa Mon Sep 17 00:00:00 2001 From: George He Date: Tue, 20 May 2025 15:01:14 +0800 Subject: [PATCH] chore: change the script error displaying behavior --- packages/insomnia/src/network/network.ts | 64 +++++++++++++++------ packages/insomnia/src/ui/routes/request.tsx | 14 ----- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/packages/insomnia/src/network/network.ts b/packages/insomnia/src/network/network.ts index b954b3f4f2..7e0b2620c7 100644 --- a/packages/insomnia/src/network/network.ts +++ b/packages/insomnia/src/network/network.ts @@ -426,7 +426,13 @@ export async function savePatchesMadeByScript(patches: { }); } -export const tryToExecuteScript = async (context: RequestAndContextAndOptionalResponse) => { +type TryToExecuteScriptResult = + | (RequestAndContextAndOptionalResponse & { requestTestResults: RequestTestResult[] | undefined }) + | { error: string }; + +export const tryToExecuteScript = async ( + context: RequestAndContextAndOptionalResponse, +): Promise => { const { script, request, @@ -592,28 +598,53 @@ export const tryToExecuteScript = async (context: RequestAndContextAndOptionalRe execution: output.execution, transientVariables, parentFolders: output.parentFolders, + timelinePath, + ancestors, + responseId, + script, }; } catch (err) { + const errMessage = `Detected error in Pre-request or After-response Script:\n${err.message || err}` await fs.promises.appendFile( timelinePath, - serializeNDJSON([{ value: err.message, name: 'Text', timestamp: Date.now() }]), + serializeNDJSON([ + { + value: errMessage, + name: 'Text', + timestamp: Date.now(), + }, + ]), ); - const requestId = request._id; - // stack trace is ignored as it is always from preload - const errMessage = err.message ? err.message : err; - const responsePatch = { - _id: responseId, - parentId: requestId, - environemntId: environment._id, - globalEnvironmentId: globals?._id, - timelinePath, - statusMessage: 'Error', - error: errMessage, + // errors are handled differently in pre-request and after-response scripts + if (response === undefined) { + // in pre-request script + // all errors are regarded as fatal error + const requestId = request._id; + // stack trace is ignored as it is always from preload + + const responsePatch = { + _id: responseId, + parentId: requestId, + environemntId: environment._id, + globalEnvironmentId: globals?._id, + timelinePath, + statusMessage: 'Error', + error: errMessage, + }; + const res = await models.response.create(responsePatch, settings.maxHistoryResponses); + models.requestMeta.updateOrCreateByParentId(requestId, { activeResponseId: res._id }); + return { + error: errMessage, + }; + } + + // in after-response script + // error will only be displayed in console, instead of preview INS-5470 + return { + ...context, + requestTestResults: undefined, }; - const res = await models.response.create(responsePatch, settings.maxHistoryResponses); - models.requestMeta.updateOrCreateByParentId(requestId, { activeResponseId: res._id }); - return { error: errMessage }; } }; @@ -679,6 +710,7 @@ export async function tryToExecuteAfterResponseScript(context: RequestAndContext return { error: `Execute after-response script failed: ${postMutatedContext?.error}`, ...context, + requestTestResults: new Array(), }; } diff --git a/packages/insomnia/src/ui/routes/request.tsx b/packages/insomnia/src/ui/routes/request.tsx index 15da469d35..34f6cf9331 100644 --- a/packages/insomnia/src/ui/routes/request.tsx +++ b/packages/insomnia/src/ui/routes/request.tsx @@ -702,19 +702,6 @@ export const sendActionImplementation = async (options: { iterationCount, runtime, }); - if ('error' in postMutatedContext) { - throw { - response: await responseTransform( - response, - requestData.activeEnvironmentId, - renderedRequest, - renderedResult.context, - ), - maxHistoryResponses: requestData.settings.maxHistoryResponses, - requestMeta, - error: postMutatedContext.error, - }; - } window.main.completeExecutionStep({ requestId }); @@ -832,7 +819,6 @@ export const deleteAllResponsesAction: ActionFunction = async ({ params }) => { } return null; }; - export const deleteResponseAction: ActionFunction = async ({ request, params }) => { const { workspaceId, requestId } = params; invariant(typeof requestId === 'string', 'Request ID is required');