Skip to content

Commit

Permalink
fix: throw error 예외처리
Browse files Browse the repository at this point in the history
유저가 throw Error를 입력하면 모듈 밖에서도 throw하는 문제가 있었음

이 때문에 내부에서 throw를 한번 걸러줄 수 있게 코드를 수정

추가적으로  에러 코드를 출력하도록 변경함
  • Loading branch information
dev2820 committed Nov 21, 2023
1 parent ecb0156 commit 973d8cd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
3 changes: 2 additions & 1 deletion frontend/src/modules/evaluator/EvalTaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export default class EvalTaskManager {
this.deployTask();
}
}
receiveTaskEnd({ result }: EvalResult, evaluator: Evaluator) {
receiveTaskEnd({ result, error }: EvalResult, evaluator: Evaluator) {
this.taskEndNotifier.notify({
result,
error,
task: evaluator.currentTask,
});

Expand Down
22 changes: 20 additions & 2 deletions frontend/src/modules/evaluator/quickjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,36 @@ const evalCode = (vm: QuickJSContext, code: string, params: string) => {
const startTime = performance.now();
const result = vm.unwrapResult(vm.evalCode(script));
const endTime = performance.now();
const value = vm.dump(result);
const { error, value } = vm.dump(result);
result.dispose();

return {
time: endTime - startTime,
result: value,
error,
};
};

const toRunableScript = (code: string, params: string) => {
return `
${code}\n
solution(${params});
(()=>{
try {
const result = solution(${params});
return {
value: result,
error: undefined
}
} catch(err) {
return {
error: {
name: err.name,
message: err.message,
stack: err.stack
}
}
}
})()
`;
};
5 changes: 5 additions & 0 deletions frontend/src/modules/evaluator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export type EvalMessage = {
};

export type EvalResult = {
error?: {
name: string;
message: string;
stack: string;
};
result: unknown;
};

Expand Down
13 changes: 7 additions & 6 deletions frontend/src/pages/ContestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ export default function ContestPage() {
};

useEffect(() => {
return evaluator.subscribe(({ result, task }) => {
return evaluator.subscribe(({ result, error, task }) => {
if (!task) return;

const taskId = task.clientId;

const evaluatedTestcase = testCases.find((_, index) => index === taskId);
if (evaluatedTestcase) {
evaluatedTestcase.result = String(result);
}

setTestCases((oldTestCases) => {
return oldTestCases.map((tc, index) => {
if (index !== taskId) return tc;

if (error) {
return {
...tc,
result: `${error.name}: ${error.message} \n${error.stack}`,
};
}
return {
...tc,
result,
Expand Down

0 comments on commit 973d8cd

Please sign in to comment.