Skip to content

Commit b327a70

Browse files
authored
fix(javascript): unhandled promise rejections (#201)
1 parent 58055ec commit b327a70

File tree

9 files changed

+49
-52
lines changed

9 files changed

+49
-52
lines changed

control-service/workers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ func (w *Worker) createPod() error {
195195
Limits: v1.ResourceList{
196196
v1.ResourceMemory: resource.MustParse("1024Mi"),
197197
v1.ResourceCPU: resource.MustParse("1000m"),
198-
v1.ResourceEphemeralStorage: resource.MustParse("512Mi"),
198+
v1.ResourceEphemeralStorage: resource.MustParse("128Mi"),
199199
},
200200
Requests: v1.ResourceList{
201201
v1.ResourceMemory: resource.MustParse("128Mi"),
202202
v1.ResourceCPU: resource.MustParse("200m"),
203-
v1.ResourceEphemeralStorage: resource.MustParse("512Mi"),
203+
v1.ResourceEphemeralStorage: resource.MustParse("128Mi"),
204204
},
205205
},
206206
},

e2e/tests/visual.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe("should handle platform core related features", test => {
150150
await page.keyboard.type("();")
151151

152152
await page.click("text='Run'")
153-
await page.waitForSelector("text='Error: Execution timeout!'", {
153+
await page.waitForSelector("text=Execution timeout!", {
154154
timeout: 70 * 1000
155155
})
156156
})

frontend/src/api.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ interface FileWrapper {
44
extension: string;
55
}
66

7-
type ErroredExecutionResponse = {
8-
success: false
7+
type ExecutionResponse = Partial<{
8+
success: boolean
99
error: string
10-
}
11-
12-
type SuccessExecutionResponse ={
13-
success: true
1410
version: string;
1511
duration?: number;
1612
files: FileWrapper[];
1713
output: string;
18-
}
19-
20-
type ExecutionResponse = ErroredExecutionResponse | SuccessExecutionResponse
14+
}>

frontend/src/components/App/index.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,15 @@ import CodeLanguageSelector from '../CodeLanguageSelector';
1313
const App: React.FunctionComponent = () => {
1414
const { code, onChangeRightPanelMode } = useContext(CodeContext)
1515
const [loading, setLoading] = useState<boolean>(false)
16-
const [resp, setResponse] = useState<SuccessExecutionResponse | null>(null)
17-
const [error, setError] = useState<string | null>(null)
16+
const [resp, setResponse] = useState<ExecutionResponse|null>(null)
1817
const handleExecutionRef = useRef<() => Promise<void>>()
1918

2019
const handleExecution = async (): Promise<void> => {
2120
setLoading(true)
2221
setResponse(null)
23-
setError(null)
2422

2523
trackEvent()
26-
try {
27-
const resp = await runCode(code)
28-
setResponse(resp)
29-
} catch (err) {
30-
setError(err.toString())
31-
}
24+
setResponse(await runCode(code))
3225
setLoading(false)
3326
onChangeRightPanelMode(false)
3427
}
@@ -59,7 +52,7 @@ const App: React.FunctionComponent = () => {
5952
</Panel>
6053
</Col>
6154
<Col xs={24} md={12}>
62-
<RightPanel resp={resp} error={error}/>
55+
<RightPanel resp={resp} />
6356
</Col>
6457
</Grid >
6558
</>

frontend/src/components/RightPanel/RightOutputPanel/index.tsx

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,38 @@ import ResponseFile from '../../ResponseFile'
55
import styles from './index.module.css'
66

77
interface RightOutputPanelProps {
8-
resp: SuccessExecutionResponse | null;
9-
error: string | null
8+
resp: ExecutionResponse | null;
109
}
1110

12-
const RightOutputPanel: React.FunctionComponent<RightOutputPanelProps> = ({ resp, error }) => {
11+
const RightOutputPanel: React.FunctionComponent<RightOutputPanelProps> = ({ resp }) => {
1312
return (
1413
<>
15-
{!error && resp && <>
16-
{resp.output.length > 0 && <h4>Logs</h4>}
17-
<code className={styles.logsWrapper}>{resp.output.split("\n").map((entry, idx) => <React.Fragment key={idx}>
18-
{entry}
19-
<br />
20-
</React.Fragment>)}</code>
21-
{resp.files.length > 0 && <h4>Files</h4>}
22-
{resp.files.map((file, idx) => <ResponseFile file={file} key={idx} />)}
23-
<p>Duration of {resp.duration} ms with Playwright version {resp.version}.</p>
24-
</>}
25-
{error && <>
26-
<h4>Error</h4>
27-
<pre>
28-
{error}
29-
</pre>
14+
{resp && <>
15+
{resp.error && <>
16+
<h4>Error</h4>
17+
<pre>
18+
{resp.error}
19+
</pre>
20+
</>}
21+
22+
{resp.output && <>
23+
{resp.output.length > 0 && <h4>Logs</h4>}
24+
<code className={styles.logsWrapper}>
25+
{resp.output.split("\n").map((entry, idx) => <React.Fragment key={idx}>
26+
{entry}
27+
<br />
28+
</React.Fragment>)}
29+
</code>
30+
</>}
31+
32+
{resp.files && <>
33+
{resp.files.length > 0 && <h4>Files</h4>}
34+
{resp.files.map((file, idx) => <ResponseFile file={file} key={idx} />)}
35+
</>}
36+
37+
{resp.success && <>
38+
<p>Duration of {resp.duration} ms with Playwright version {resp.version}.</p>
39+
</>}
3040
</>}
3141
</>
3242
)

frontend/src/components/RightPanel/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import { CodeContext } from '../CodeContext'
1010
import styles from './index.module.css'
1111

1212
interface RightPanelProps {
13-
resp: SuccessExecutionResponse | null;
14-
error: string | null
13+
resp: ExecutionResponse | null;
1514
}
1615

1716
const getHeaderText = (mode: boolean): string => {
1817
return mode ? "Output" : "Examples"
1918
}
2019

21-
const RightPanel: React.FunctionComponent<RightPanelProps> = ({ resp, error }) => {
20+
const RightPanel: React.FunctionComponent<RightPanelProps> = ({ resp }) => {
2221
const { rightPanelMode, onChangeRightPanelMode } = useContext(CodeContext)
2322
const handleShowExamplesClick = (): void => onChangeRightPanelMode(!rightPanelMode)
2423

@@ -34,7 +33,7 @@ const RightPanel: React.FunctionComponent<RightPanelProps> = ({ resp, error }) =
3433
}
3534
>
3635
<div className={styles.rightPanelWrapper}>
37-
{rightPanelMode ? <RightExamplesPanel /> : <RightOutputPanel resp={resp} error={error}/>}
36+
{rightPanelMode ? <RightExamplesPanel /> : <RightOutputPanel resp={resp} />}
3837
</div>
3938
</Panel>
4039
)

frontend/src/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CodeLanguage, LANGUAGES } from "./constants";
22
import { Example } from "./examples";
33

4-
export const runCode = async (code: string): Promise<SuccessExecutionResponse> => {
4+
export const runCode = async (code: string): Promise<ExecutionResponse> => {
55
const resp = await fetch("/service/control/run", {
66
method: "POST",
77
headers: {
@@ -12,15 +12,15 @@ export const runCode = async (code: string): Promise<SuccessExecutionResponse> =
1212
language: determineLanguage()
1313
})
1414
})
15+
1516
if (!resp.ok) {
1617
if (resp.status === 429) {
17-
throw new Error("You are rate limited, please try again in a few minutes.")
18+
return { error: "You are rate limited, please try again in a few minutes." }
1819
}
1920
if (resp.headers.get("Content-Type")?.includes("application/json")) {
20-
const error = await resp.json()
21-
throw new Error(error.error)
21+
return await resp.json()
2222
}
23-
throw new Error("Execution was not successful, please try again in a few minutes.")
23+
return { error: "Execution was not successful, please try again in a few minutes." }
2424
}
2525
return await resp.json()
2626
}

internal/worker/worker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package worker
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89
"log"
@@ -94,7 +95,7 @@ func (w *Worker) ExecCommand(name string, args ...string) error {
9495
),
9596
}
9697
if err := c.Run(); err != nil {
97-
return fmt.Errorf("could not run command: %s", w.options.TransformOutput(w.output.String()))
98+
return errors.New("could not run command")
9899
}
99100
files, err := collector.Collect()
100101
if err != nil {
@@ -116,12 +117,12 @@ func (w *Worker) consumeMessage(incomingMessages <-chan amqp.Delivery) error {
116117
outgoingMessage.Error = err.Error()
117118
} else {
118119
outgoingMessage.Success = true
119-
outgoingMessage.Output = w.options.TransformOutput(w.output.String())
120120
outgoingMessage.Files, err = w.uploadFiles()
121121
if err != nil {
122122
return fmt.Errorf("could not upload files: %w", err)
123123
}
124124
}
125+
outgoingMessage.Output = w.options.TransformOutput(w.output.String())
125126
outgoingMessageBody, err := json.Marshal(outgoingMessage)
126127
if err != nil {
127128
return fmt.Errorf("could not marshal outgoing message payload: %w", err)

worker-javascript/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func handler(w *worker.Worker, code string) error {
8-
return w.ExecCommand("node", "-e", code)
8+
return w.ExecCommand("node", "--unhandled-rejections=strict", "-e", code)
99
}
1010

1111
func main() {

0 commit comments

Comments
 (0)