Skip to content

JSON response error handling + abort timeout handling #145

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

Merged
merged 4 commits into from
Jul 23, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public long getScriptStatsLimit() {
@AttributeDefinition(
name = "Execution Poll Interval",
description = "Interval in milliseconds to poll execution status.")
long executionPollInterval() default 1000;
long executionPollInterval() default 1400;

@AttributeDefinition(
name = "Script Stats Limit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static dev.vml.es.acm.core.util.ServletResult.*;
import static dev.vml.es.acm.core.util.ServletUtils.respondJson;
import static dev.vml.es.acm.core.util.ServletUtils.respondJsonBuffered;

import dev.vml.es.acm.core.code.*;
import dev.vml.es.acm.core.util.JsonUtils;
Expand Down Expand Up @@ -51,7 +50,7 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
ExecutionId.generate(), ExecutionMode.PARSE, code, request.getResourceResolver())) {
Description description = executor.describe(context);

respondJsonBuffered(
respondJson(
response,
ok(String.format("Code from '%s' described successfully", code.getId()), description));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static dev.vml.es.acm.core.util.ServletResult.*;
import static dev.vml.es.acm.core.util.ServletUtils.respondJson;
import static dev.vml.es.acm.core.util.ServletUtils.respondJsonBuffered;

import dev.vml.es.acm.core.code.*;
import dev.vml.es.acm.core.util.JsonUtils;
Expand Down Expand Up @@ -66,7 +65,7 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
try {
Execution execution = executor.execute(context);

respondJsonBuffered(
respondJson(
response, ok(String.format("Code from '%s' executed successfully", code.getId()), execution));
} catch (Exception e) {
LOG.error("Code from '{}' cannot be executed!", code.getId(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ public static List<String> stringsParam(SlingHttpServletRequest request, String
.collect(Collectors.toList());
}

public static void respondJson(SlingHttpServletResponse response, ServletResult<?> result) throws IOException {
respondJsonBuffered(response, result);
Comment on lines +46 to +47
Copy link
Preview

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change from streaming to buffered JSON responses is a breaking change in behavior. The old streaming method consumed less memory, and switching all responses to buffered mode could impact performance for large responses without providing a migration path.

Suggested change
public static void respondJson(SlingHttpServletResponse response, ServletResult<?> result) throws IOException {
respondJsonBuffered(response, result);
public static void respondJson(SlingHttpServletResponse response, ServletResult<?> result, boolean useBuffered) throws IOException {
if (useBuffered) {
respondJsonBuffered(response, result);
} else {
respondJsonStreamed(response, result);
}

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but improves error handling; now when something breaks the malformed JSON is output

}

/**
* Responds with JSON result in streaming mode.
* Consumes less memory, but JSON may be malformed in case of serialization errors.
*/
public static void respondJson(SlingHttpServletResponse response, ServletResult<?> result) throws IOException {
public static void respondJsonStreamed(SlingHttpServletResponse response, ServletResult<?> result) throws IOException {
response.setStatus(result.getStatus());
response.setContentType(JsonUtils.APPLICATION_JSON_UTF8);

Expand Down
2 changes: 1 addition & 1 deletion ui.frontend/src/components/ExecutionAbortButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const ExecutionAbortButton: React.FC<ExecutionAbortButtonProps> = ({ execution,
timeout: ToastTimeoutQuick,
});
} else {
console.warn('Code execution aborting failed!');
console.warn('Code execution aborting failed!', queuedExecution);
ToastQueue.negative('Code execution aborting failed!', {
timeout: ToastTimeoutQuick,
});
Expand Down
2 changes: 1 addition & 1 deletion ui.frontend/src/hooks/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InstanceRole, InstanceType, State } from '../utils/api.types';
export const appState = signal<State>({
spaSettings: {
appStateInterval: 3000,
executionPollInterval: 1000,
executionPollInterval: 1400,
scriptStatsLimit: 30,
},
healthStatus: {
Expand Down
22 changes: 14 additions & 8 deletions ui.frontend/src/hooks/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useExecutionPolling = (executionId: string | undefined | null, poll
operation: 'Code execution state',
url: `/apps/acm/api/queue-code.json?executionId=${executionId}`,
method: 'get',
quiet: true,
timeout: intervalToTimeout(pollInterval),
});
const queuedExecution = response.data.data.executions.find((e: Execution) => e.id === executionId)!;
Expand Down Expand Up @@ -65,15 +66,20 @@ export const pollExecutionPending = async (executionId: string, pollInterval: nu
let queuedExecution: Execution | null = null;

while (queuedExecution === null || isExecutionPending(queuedExecution.status)) {
const response = await apiRequest<QueueOutput>({
operation: 'Code execution state',
url: `/apps/acm/api/queue-code.json?executionId=${executionId}`,
method: 'get',
timeout: intervalToTimeout(pollInterval),
});
queuedExecution = response.data.data.executions[0]!;
try {
const response = await apiRequest<QueueOutput>({
operation: 'Code execution pending state',
url: `/apps/acm/api/queue-code.json?executionId=${executionId}`,
method: 'get',
quiet: true,
timeout: intervalToTimeout(pollInterval),
});
queuedExecution = response.data.data.executions[0]!;
} catch (error) {
console.warn('Code execution pending state unknown:', error);
Comment on lines +77 to +79
Copy link
Preview

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setTimeout is called regardless of whether the API request succeeded or failed. This means the function will continue polling indefinitely even after errors, potentially creating an infinite loop if the API consistently fails.

Suggested change
queuedExecution = response.data.data.executions[0]!;
} catch (error) {
console.warn('Code execution pending state unknown:', error);
queuedExecution = response.data.data.executions[0]!;
retryCount = 0; // Reset retry counter on success
} catch (error) {
console.warn('Code execution pending state unknown:', error);
retryCount++;
if (retryCount >= maxRetries) {
throw new Error(`Failed to fetch execution state after ${maxRetries} retries.`);
}

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's expected; temporary fails are possible

}
await new Promise((resolve) => setTimeout(resolve, pollInterval));
}

return queuedExecution;
return queuedExecution!;
Copy link
Preview

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non-null assertion operator (!) is unsafe here. If an error occurs in the try-catch block and queuedExecution remains null, this will cause a runtime error. Consider returning null or throwing a specific error instead.

Suggested change
return queuedExecution!;
if (queuedExecution === null) {
throw new Error('Failed to retrieve a valid execution state after polling.');
}
return queuedExecution;

Copilot uses AI. Check for mistakes.

};
4 changes: 2 additions & 2 deletions ui.frontend/src/pages/ConsolePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const ConsolePage = () => {
</Item>
</TabList>
<TabPanels flex="1" UNSAFE_style={{ display: 'flex' }}>
<Item key="code">
<Item key="code" aria-label="Code">
<Flex direction="column" gap="size-200" marginY="size-100" flex={1}>
<Flex direction="row" justifyContent="space-between" alignItems="center">
<Flex flex="1" alignItems="center">
Expand All @@ -126,7 +126,7 @@ const ConsolePage = () => {
<CodeEditor id="code-editor" initialValue={code} readOnly={executing} onChange={setCode} syntaxError={syntaxError} language="groovy" />
</Flex>
</Item>
<Item key="output">
<Item key="output" aria-label="Output">
<Flex direction="column" gap="size-200" marginY="size-100" flex={1}>
<Flex direction="row" justifyContent="space-between" alignItems="center">
<Flex flex="1" alignItems="center">
Expand Down
10 changes: 5 additions & 5 deletions ui.frontend/src/pages/ExecutionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ const ExecutionView = () => {
<Flex direction="column" flex="1" gap="size-400">
<Tabs flex="1" aria-label="Executions" selectedKey={selectedTab} onSelectionChange={handleTabChange}>
<TabList>
<Item key="details">
<Item key="details" aria-label="Details">
<History />
<Text>Execution</Text>
</Item>
<Item key="code" aria-label="Code">
<FileCode />
<Text>Code</Text>
</Item>
<Item key="output" aria-label="Execution">
<Item key="output" aria-label="Output">
<Print />
<Text>Output</Text>
</Item>
</TabList>
<TabPanels flex="1" UNSAFE_style={{ display: 'flex' }}>
<Item key="details">
<Item key="details" aria-label="Details">
<Flex direction="column" flex="1" gap="size-200" marginY="size-100">
<View backgroundColor="gray-50" padding="size-200" borderRadius="medium" borderColor="dark" borderWidth="thin">
<Flex direction="row" justifyContent="space-between" gap="size-200">
Expand Down Expand Up @@ -129,7 +129,7 @@ const ExecutionView = () => {
</View>
</Flex>
</Item>
<Item key="code">
<Item key="code" aria-label="Code">
<Flex direction="column" flex="1" gap="size-200" marginY="size-100">
<View>
<Flex justifyContent="space-between" alignItems="center">
Expand All @@ -144,7 +144,7 @@ const ExecutionView = () => {
<CodeEditor id="execution-view" value={execution.executable.content} language="groovy" readOnly />
</Flex>
</Item>
<Item key="output">
<Item key="output" aria-label="Output">
<Flex direction="column" flex="1" gap="size-200" marginY="size-100">
<Flex direction="row" justifyContent="space-between" alignItems="center">
<Flex flex="1" alignItems="center">
Expand Down
8 changes: 4 additions & 4 deletions ui.frontend/src/pages/MaintenancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ const MaintenancePage = () => {
<Flex direction="column" flex="1" gap="size-400">
<Tabs flex="1" aria-label="Maintenance tabs" selectedKey={selectedTab} onSelectionChange={setSelectedTab}>
<TabList>
<Item key="script-executor">
<Item key="script-executor" aria-label="Script Executor">
<Code />
<Text>Script Executor</Text>
</Item>
<Item key="health-checker">
<Item key="health-checker" aria-label="Health Checker">
<Heart />
<Text>Health Checker</Text>
</Item>
</TabList>
<TabPanels flex="1" UNSAFE_style={{ display: 'flex' }}>
<Item key="script-executor">
<Item key="script-executor" aria-label="Script Executor">
<ScriptExecutor />
</Item>
<Item key="health-checker">
<Item key="health-checker" aria-label="Health Checker">
<HealthChecker />
</Item>
</TabPanels>
Expand Down
6 changes: 3 additions & 3 deletions ui.frontend/src/pages/ScriptView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const ScriptView = () => {
<Flex direction="column" flex="1" gap="size-400">
<Tabs flex="1" aria-label="Script Details" selectedKey={selectedTab} onSelectionChange={handleTabChange}>
<TabList>
<Item key="details">
<Item key="details" aria-label="Script">
<FileCode />
<Text>Script</Text>
</Item>
Expand All @@ -128,7 +128,7 @@ const ScriptView = () => {
</Item>
</TabList>
<TabPanels flex="1" UNSAFE_style={{ display: 'flex' }}>
<Item key="details">
<Item key="details" aria-label="Details">
<Flex direction="column" flex="1" gap="size-200" marginY="size-100">
<View>
<Flex justifyContent="space-between" alignItems="center">
Expand Down Expand Up @@ -165,7 +165,7 @@ const ScriptView = () => {
</View>
</Flex>
</Item>
<Item key="code">
<Item key="code" aria-label="Code">
<Flex direction="column" flex="1" gap="size-200" marginY="size-100">
<View>
<Flex justifyContent="space-between" alignItems="center">
Expand Down
10 changes: 5 additions & 5 deletions ui.frontend/src/pages/ScriptsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ const ScriptsPage = () => {
</Item>
</TabList>
<TabPanels flex="1" UNSAFE_style={{ display: 'flex' }}>
<Item key="manual">
<Item key="manual" aria-label="Manual">
<ScriptListRich type={ScriptType.MANUAL} />
</Item>
<Item key="enabled">
<Item key="enabled" aria-label="Enabled">
<ScriptListRich type={ScriptType.ENABLED} />
</Item>
<Item key="disabled">
<Item key="disabled" aria-label="Disabled">
<ScriptListRich type={ScriptType.DISABLED} />
</Item>
{appState.value.mockStatus.enabled ? (
<Item key="mock">
<Item key="mock" aria-label="Mock">
<ScriptListSimple type={ScriptType.MOCK} />
</Item>
) : null}
<Item key="extension">
<Item key="extension" aria-label="Extension">
<ScriptListSimple type={ScriptType.EXTENSION} />
</Item>
</TabPanels>
Expand Down
4 changes: 2 additions & 2 deletions ui.frontend/src/pages/SnippetsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const SnippetsPage = () => {
{Object.keys(snippetGroups)
.sort()
.map((group, groupIndex) => (
<Item key={group}>
<Item key={group} aria-label={group}>
{snippetGroupIcons[groupIndex % snippetGroupIcons.length]}
<Text>{group}</Text>
</Item>
Expand All @@ -72,7 +72,7 @@ const SnippetsPage = () => {
{Object.keys(snippetGroups)
.sort()
.map((group) => (
<Item key={group}>
<Item key={group} aria-label={group}>
<Flex direction="column" flex="1" gap="size-100" marginY="size-100">
{snippetGroups[group]
.sort((a, b) => a.name.localeCompare(b.name))
Expand Down