mirror of
https://litchi.icu/ngc2207/judge.git
synced 2025-05-18 22:37:11 +00:00
feat(docker): enhance runCode function to return structured message props and improve error handling
This commit is contained in:
parent
0c065af788
commit
dbf121aee4
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
import Dockerode from "dockerode";
|
import Dockerode from "dockerode";
|
||||||
import { pullImage } from "@/actions/docker/image";
|
import { pullImage } from "@/actions/docker/image";
|
||||||
|
import {
|
||||||
|
MessageType,
|
||||||
|
MessageProps,
|
||||||
|
} from "@/app/playground/layout/terminal/components/message";
|
||||||
|
|
||||||
enum ResultType {
|
enum ResultType {
|
||||||
CompilationError = "compilation error",
|
CompilationError = "compilation error",
|
||||||
@ -88,10 +92,22 @@ async function cleanupContainer(container: Dockerode.Container): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapResultTypeToMessageType(type: ResultType): MessageType {
|
||||||
|
switch (type) {
|
||||||
|
case ResultType.CompilationError:
|
||||||
|
case ResultType.RuntimeError:
|
||||||
|
return "error";
|
||||||
|
case ResultType.Success:
|
||||||
|
return "success";
|
||||||
|
default:
|
||||||
|
return "info";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function runCode(
|
export async function runCode(
|
||||||
language: string,
|
language: string,
|
||||||
code: string
|
code: string
|
||||||
): Promise<RunCodeResult> {
|
): Promise<MessageProps> {
|
||||||
const docker = new Dockerode({ socketPath: "/var/run/docker.sock" });
|
const docker = new Dockerode({ socketPath: "/var/run/docker.sock" });
|
||||||
const imageGCC = "gcc:latest";
|
const imageGCC = "gcc:latest";
|
||||||
let container: Dockerode.Container | undefined;
|
let container: Dockerode.Container | undefined;
|
||||||
@ -106,17 +122,19 @@ export async function runCode(
|
|||||||
stderr: true,
|
stderr: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
return await getOutputAndResultType(stream);
|
const result = await getOutputAndResultType(stream);
|
||||||
|
return {
|
||||||
|
type: mapResultTypeToMessageType(result.type),
|
||||||
|
message: result.message,
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`不支持的语言: ${language}`);
|
throw new Error(`不支持的语言: ${language}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("运行代码时出错:", error);
|
console.error("运行代码时出错:", error);
|
||||||
return {
|
return {
|
||||||
success: false,
|
type: "error",
|
||||||
output: error instanceof Error ? error.message : "未知错误",
|
message: error instanceof Error ? error.message : "未知错误",
|
||||||
message: "运行错误:未知错误",
|
|
||||||
type: ResultType.RuntimeError,
|
|
||||||
};
|
};
|
||||||
} finally {
|
} finally {
|
||||||
if (container) {
|
if (container) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import Run from "./button/run";
|
|
||||||
import Nav from "./nav";
|
import Nav from "./nav";
|
||||||
import User from "./user";
|
import User from "./user";
|
||||||
|
import Run from "./button/run";
|
||||||
|
|
||||||
export default function Tools() {
|
export default function Tools() {
|
||||||
return (
|
return (
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { CircleCheck, Info, TriangleAlert } from "lucide-react";
|
import { CircleCheck, Info, TriangleAlert } from "lucide-react";
|
||||||
|
|
||||||
type MessageType = "info" | "error" | "warning" | "success";
|
export type MessageType = "info" | "error" | "warning" | "success";
|
||||||
|
|
||||||
interface MessageTypeDetails {
|
interface MessageTypeDetails {
|
||||||
icon: React.ComponentType<{
|
icon: React.ComponentType<{
|
||||||
@ -32,7 +32,7 @@ const messageTypeIconMap: Record<MessageType, MessageTypeDetails> = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
interface MessageProps {
|
export interface MessageProps {
|
||||||
type: MessageType;
|
type: MessageType;
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user