feat(题目): 运行时错误告诉学生第几行、为什么出错(Python 看回溯,C 看信号),不泄露测试数据

原来学生只看到「运行时错误」四个字:主判题 output: false,Python 回溯没回来;
C 的信号值在 info 里,而 info 只给管理员。

- Python 判出 RE 时,拿第一个失败测试点单独重跑一次(内联测试点 + output: true),
  从回溯取行号(扣掉模板前置行)、异常类型和按句式归的类别。主判题仍不开 output,
  免得每个点最多 16MB 的输出回到 worker
- C / C++ 取失败点的信号和退出码(段错误提示 scanf 漏 &、return 非 0 等)
- 写进 statistic_info.runtime_error;不存异常原文(里面常带测试点输入),名字只在
  出现在学生代码里时才存——raise Exception(input()) 实测只落下类型
- 前端按类型翻成中文、摆出出错那行并在编辑器标红;比赛提交不诊断(ACM 只报对错)
- 没有诊断的 RE 不再说「代码格式写错了」

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-09-27 22:01:30 +08:00
1 parent 2a4405d2ea
commit db70ab62e8
7 files changed
+391 -11

No files matched your search

@@ -63,7 +63,7 @@ onUnmounted(clearErrorMark)
</script>
<template>
<n-card embedded>
<n-card embedded class="explain-card">
<n-flex vertical :size="12">
<template v-if="explanation">
<div class="explain">
@@ -95,6 +95,11 @@ onUnmounted(clearErrorMark)
</template>
<style scoped>
/* 结果弹窗不限宽,长句子不折行会把弹窗撑出屏幕 */
.explain-card {
max-width: 560px;
}
.explain {
font-size: 16px;
line-height: 1.7;
@@ -0,0 +1,67 @@
<script setup lang="ts">
import { clearErrorMark, showErrorMark } from "oj/problem/utils/errorMark"
import { explainRuntimeError, type RuntimeErrorInfo } from "oj/problem/utils/runtimeError"
/**
* 运行时错误的中文说明卡片(诊断由后端 judge/runtime-diagnosis.ts 给)。
* 知道是第几行的,就把那一行代码摆出来,并在编辑器里标红;卸掉时清掉标记。
*/
const props = defineProps<{
info: RuntimeErrorInfo
/** 这次提交的代码,用来摆出出错的那一行 */
code: string
}>()
const message = computed(() => explainRuntimeError(props.info))
const sourceLine = computed(() => {
const line = props.info.line
if (!line) return null
const text = props.code.split("\n")[line - 1]
return text?.trim() ? text.trim() : null
})
watch(
() => props.info.line,
(line) => {
if (line) showErrorMark(line, null, null)
else clearErrorMark()
},
{ immediate: true },
)
onUnmounted(clearErrorMark)
</script>
<template>
<n-card embedded class="explain-card">
<n-flex vertical :size="12">
<div class="explain">
<b v-if="info.line">第 {{ info.line }} 行运行时出错:</b>{{ message }}
</div>
<pre v-if="sourceLine" class="explain-code">{{ sourceLine }}</pre>
</n-flex>
</n-card>
</template>
<style scoped>
/* 结果弹窗不限宽,长句子不折行会把弹窗撑出屏幕 */
.explain-card {
max-width: 560px;
}
.explain {
font-size: 16px;
line-height: 1.7;
}
.explain-code {
margin: 0;
padding: 8px 12px;
border-radius: 4px;
font-size: 15px;
white-space: pre-wrap;
word-break: break-all;
background-color: rgba(208, 48, 80, 0.12);
}
</style>
@@ -15,6 +15,7 @@ import type { Submission } from "utils/types"
import SubmissionResultTag from "shared/components/SubmissionResultTag.vue"
import { useProblemStore } from "oj/store/problem"
import PythonErrorExplain from "./PythonErrorExplain.vue"
import RuntimeErrorExplain from "./RuntimeErrorExplain.vue"
import { useAIStream } from "shared/composables/aiStream"
import { submitHintFeedback } from "oj/api"
import { MdPreview } from "md-editor-v3"
@@ -102,21 +103,34 @@ const pythonCompileError = computed(() => {
})
// 错误信息格式化
/**
* 运行时错误的诊断(行号 + 归类),交给 RuntimeErrorExplain。比赛提交、诊断失败的
* 没有这个字段(见后端 judge/run.ts 的 diagnoseRuntimeError),走下面 msg 的通用提示。
*/
const runtimeError = computed(() => {
const submission = props.submission
if (!submission || submission.result !== SubmissionStatus.runtime_error) return null
return submission.statisticInfo?.runtime_error ?? null
})
const msg = computed(() => {
if (!props.submission) return ""
// 走 PythonErrorExplain 那张中文卡片,英文原文在它的折叠区里
if (pythonCompileError.value) return ""
// 走 PythonErrorExplain / RuntimeErrorExplain 那两张中文卡片
if (pythonCompileError.value || runtimeError.value) return ""
let msg = ""
const result = props.submission.result
// 编译错误或运行时错误时给出提示;
// SQL 题的运行错误多半是"查询题里写了增删改"这类被判题拒绝的语句,err_info 已说明原因,不套这句
if (
(result === SubmissionStatus.compile_error || result === SubmissionStatus.runtime_error) &&
props.submission.language !== "SQL"
) {
msg += "请仔细检查,看看代码的格式是不是写错了!\n\n"
if (props.submission.language !== "SQL") {
if (result === SubmissionStatus.compile_error) {
msg += "请仔细检查,看看代码的格式是不是写错了!\n\n"
} else if (result === SubmissionStatus.runtime_error) {
// 原来和编译错误共用「代码格式写错了」那句,可运行时错误恰恰是格式没错、跑起来才出事
msg +=
"程序运行到一半出错,停下来了。检查一下输入是怎么读的、下标有没有越界、除数是不是 0。\n\n"
}
}
if (result !== SubmissionStatus.ast_check_failed && props.submission.statisticInfo?.err_info) {
@@ -270,12 +284,14 @@ const columns: DataTableColumn<JudgeCaseResult>[] = [
vertical
v-if="
pythonCompileError ||
runtimeError ||
msg ||
infoTable.length ||
submission.statisticInfo?.ast_results?.length
"
>
<PythonErrorExplain v-if="pythonCompileError" :err-info="pythonCompileError" />
<RuntimeErrorExplain v-if="runtimeError" :info="runtimeError" :code="submission.code" />
<n-card v-if="submission.statisticInfo?.ast_results?.length" embedded>
<n-flex vertical :size="8">
<n-flex
@@ -0,0 +1,89 @@
import type { StatisticInfo } from "@oj2/contract"
/**
* 把运行时错误的诊断(`statistic_info.runtime_error`,后端 judge/runtime-diagnosis.ts
* 写的)翻成给中职学生看的中文。界面上不出现英文的异常名。
*
* 诊断里只有异常类型、归好类的 kind 和学生代码里的名字,**没有异常的原文消息**
* (原文里常带着隐藏测试点的输入),所以这里的说明都是按类型写的通用话,
* 定位靠行号和编辑器里的标红。
*/
export type RuntimeErrorInfo = NonNullable<StatisticInfo["runtime_error"]>
/** 说得最多的一句:input() 读进来的是文字。学生栽在类型上,十有八九是这个 */
const INPUT_IS_TEXT = "input() 读进来的是文字,要计算先用 int() 或 float() 转成数字。"
const BY_KIND: Record<string, string> = {
"int-parse":
"把输入转成整数的时候失败了:读到的内容不是一个整数。常见原因:一行里输入了好几个数,要先用 split() 拆开再转换;或者输入的是小数,要用 float()。",
"float-parse":
"把输入转成数字的时候失败了:读到的内容不是一个数。常见原因:一行里输入了好几个数,要先用 split() 拆开再转换。",
unpack:
"等号左边的变量个数,和右边拆出来的值的个数对不上。比如 a, b = input().split() 要求这一行正好有两个值,对照题目的输入格式看看一行里有几个数。",
"math-domain": "数学函数拿到了不合法的值,比如给负数开平方。",
"str-concat": `文字和数字不能直接用 + 连在一起。${INPUT_IS_TEXT}要拼成一句话,就用 str() 把数字转成文字。`,
operand: `这两个值的类型不能做这种运算,多半是拿文字和数字一起算。${INPUT_IS_TEXT}`,
"not-int":
"这里需要一个整数,给的却是文字或小数。比如 range() 里要放整数,input() 读进来的要先用 int() 转换。",
compare: `文字和数字不能比大小。${INPUT_IS_TEXT}`,
"not-callable":
"把一个不是函数的东西当成函数用了。常见原因:乘法漏写了 *(比如 2(a+b) 要写成 2*(a+b)),或者拿 print、input、sum 这类名字当了变量名。",
"not-subscriptable": "对一个数字用了下标 [ ]。只有列表、字符串这类值才能用 [ ] 取出其中一个。",
"missing-arg": "调用函数的时候少给了参数。",
"index-type": "下标要用整数。input() 读进来的是文字,要先用 int() 转换。",
}
const BY_TYPE: Record<string, string> = {
ValueError: "这里用到的值不符合要求。",
TypeError: `这里的值类型不对,比如把文字当成数字来用。${INPUT_IS_TEXT}`,
IndexError:
"下标越界了:要取的位置超出了列表或字符串的长度。下标从 0 开始,长度是 n 的话,最后一个是 [n-1]。",
UnboundLocalError: "函数里的这个变量还没赋值就先用了。",
ZeroDivisionError: "除数是 0 了(除法 / 、整除 // 或者取余 %)。做除法之前先判断除数是不是 0。",
EOFError:
"程序读的输入比题目给的多:input() 用的次数超过了输入的行数。看看输入格式,如果几个数在同一行,要用一次 input() 读进来,再用 split() 拆开。",
KeyError: "字典里没有这个键。取之前先用 in 判断一下键在不在字典里。",
RecursionError: "函数自己调用自己的层数太多了,检查一下递归有没有写结束条件。",
ModuleNotFoundError: "导入的模块不存在,判题的环境里只有 Python 自带的模块。",
ImportError: "导入的模块或者名字不存在,判题的环境里只有 Python 自带的模块。",
MemoryError: "程序占用的内存太多了。",
OverflowError: "算出来的数太大了,超出了能表示的范围。",
}
function explainPython(info: RuntimeErrorInfo) {
if (info.type === "NameError") {
if (!info.name)
return "用到了一个没有定义过的名字:检查拼写和大小写,或者是不是还没赋值就先用了。"
if (info.suggestion)
return `「${info.name}」这个名字没有定义过,是不是想写「${info.suggestion}」?`
return `「${info.name}」这个名字没有定义过:检查拼写和大小写,或者是不是还没赋值就先用了。`
}
if (info.type === "AttributeError") {
const what = info.name ? `「${info.name}」这个` : "你用的这个"
return `这个值没有${what}方法或属性:可能是拼错了,或者这个值的类型不对。`
}
return (
(info.kind && BY_KIND[info.kind]) ||
(info.type && BY_TYPE[info.type]) ||
"程序运行到这里出错,停下来了。"
)
}
function explainNative(info: RuntimeErrorInfo) {
switch (info.signal) {
case 11:
return "程序访问了不该访问的内存。常见原因:scanf 的变量前面忘了写 &;数组下标越界了;数组开得太大(可以挪到 main 外面定义)。"
case 8:
return "整数除以 0 了:除法 / 或者取余 % 的右边是 0。"
case 6:
return "程序被强制终止了,常见原因是内存访问出错、数组越界,C++ 里也可能是有异常没有处理。"
}
if (info.signal) return "程序运行的时候崩溃了。"
if (info.exit_code)
return `程序结束时返回的是 ${info.exit_code} 而不是 0:main 函数的最后要写 return 0;`
return "程序运行到一半出错,停下来了。"
}
export function explainRuntimeError(info: RuntimeErrorInfo) {
return info.type ? explainPython(info) : explainNative(info)
}