Skip to content

chore: change the script error displaying behavior - INS-5470 #8738

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

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
63 changes: 47 additions & 16 deletions packages/insomnia/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TryToExecuteScriptResult> => {
const {
script,
request,
Expand Down Expand Up @@ -592,28 +598,52 @@ export const tryToExecuteScript = async (context: RequestAndContextAndOptionalRe
execution: output.execution,
transientVariables,
parentFolders: output.parentFolders,
timelinePath,
ancestors,
responseId,
script,
};
} catch (err) {
const errMessage = eventName === 'prerequest' ? `Error executing pre-request script:\n${err.message || err}` : `Error executing 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 (eventName === 'prerequest') {
// in pre-request script
// all errors are regarded as fatal error
const requestId = request._id;

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 };
}
};

Expand Down Expand Up @@ -679,6 +709,7 @@ export async function tryToExecuteAfterResponseScript(context: RequestAndContext
return {
error: `Execute after-response script failed: ${postMutatedContext?.error}`,
...context,
requestTestResults: new Array<RequestTestResult>(),
};
}

Expand Down
3 changes: 1 addition & 2 deletions packages/insomnia/src/ui/routes/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ export const sendActionImplementation = async (options: {
iterationCount,
runtime,
});
if ('error' in postMutatedContext) {
if ('error' in postMutatedContext && postMutatedContext.error.includes("Executing script timeout")) {
throw {
response: await responseTransform(
response,
Expand Down Expand Up @@ -832,7 +832,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');
Expand Down
Loading