mirror of
https://litchi.icu/ngc2207/judge.git
synced 2025-05-18 16:17:21 +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 { pullImage } from "@/actions/docker/image";
|
||||
import {
|
||||
MessageType,
|
||||
MessageProps,
|
||||
} from "@/app/playground/layout/terminal/components/message";
|
||||
|
||||
enum ResultType {
|
||||
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(
|
||||
language: string,
|
||||
code: string
|
||||
): Promise<RunCodeResult> {
|
||||
): Promise<MessageProps> {
|
||||
const docker = new Dockerode({ socketPath: "/var/run/docker.sock" });
|
||||
const imageGCC = "gcc:latest";
|
||||
let container: Dockerode.Container | undefined;
|
||||
@ -106,17 +122,19 @@ export async function runCode(
|
||||
stderr: true,
|
||||
});
|
||||
|
||||
return await getOutputAndResultType(stream);
|
||||
const result = await getOutputAndResultType(stream);
|
||||
return {
|
||||
type: mapResultTypeToMessageType(result.type),
|
||||
message: result.message,
|
||||
};
|
||||
} else {
|
||||
throw new Error(`不支持的语言: ${language}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("运行代码时出错:", error);
|
||||
return {
|
||||
success: false,
|
||||
output: error instanceof Error ? error.message : "未知错误",
|
||||
message: "运行错误:未知错误",
|
||||
type: ResultType.RuntimeError,
|
||||
type: "error",
|
||||
message: error instanceof Error ? error.message : "未知错误",
|
||||
};
|
||||
} finally {
|
||||
if (container) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import Run from "./button/run";
|
||||
import Nav from "./nav";
|
||||
import User from "./user";
|
||||
import Run from "./button/run";
|
||||
|
||||
export default function Tools() {
|
||||
return (
|
||||
|
@ -1,7 +1,7 @@
|
||||
import React from "react";
|
||||
import { CircleCheck, Info, TriangleAlert } from "lucide-react";
|
||||
|
||||
type MessageType = "info" | "error" | "warning" | "success";
|
||||
export type MessageType = "info" | "error" | "warning" | "success";
|
||||
|
||||
interface MessageTypeDetails {
|
||||
icon: React.ComponentType<{
|
||||
@ -32,7 +32,7 @@ const messageTypeIconMap: Record<MessageType, MessageTypeDetails> = {
|
||||
},
|
||||
};
|
||||
|
||||
interface MessageProps {
|
||||
export interface MessageProps {
|
||||
type: MessageType;
|
||||
message: string;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user