image.core\types\error.mbt
98.1% · 105 / 107 points · 262 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 错误模型 Error model - 3
/// - 4
/// 面向"放进服务"的统一错误面:稳定的错误码 + 结构化分类 + 建议动作。 - 5
/// 既有 `LoadError` 保持不变(避免动 70 个包),通过 `image_error_from_load` 投影到新模型。 - 6
/// - 7
/// Stable, structured error surface for service use: stable codes + category + hint. - 8
/// `LoadError` stays untouched; `image_error_from_load` projects it into this model. - 9
- 10
///| - 11
/// 稳定错误码(字符串形式是小写蛇形,可直接出现在 JSON 的 `code` 字段)。 - 12
/// Stable error codes; the string form is lower_snake_case and safe for a JSON `code` field. - 13
#derive(Eq, @debug.Debug) - 14
pub(all) enum ErrorCode { - 15
UnsupportedFormat - 16
InvalidHeader - 17
InvalidDimensions - 18
InvalidArgument - 19
Truncated - 20
DecodeFailed - 21
EncodeFailed - 22
ProcessFailed - 23
LimitBytes - 24
LimitPixels - 25
LimitTime - 26
Cancelled - 27
} - 28
- 29
///| - 30
/// 错误码 → 稳定字符串(小写蛇形,跨版本不变)。Error code to stable lower_snake_case string. - 31
pub fn ErrorCode::to_string(self : ErrorCode) -> String { - 32●
match self { - 33●
UnsupportedFormat => "unsupported_format" - 34●
InvalidHeader => "invalid_header" - 35●
InvalidDimensions => "invalid_dimensions" - 36●
InvalidArgument => "invalid_argument" - 37●
Truncated => "truncated" - 38●
DecodeFailed => "decode_failed" - 39●
EncodeFailed => "encode_failed" - 40●
ProcessFailed => "process_failed" - 41●
LimitBytes => "limit_bytes" - 42●
LimitPixels => "limit_pixels" - 43●
LimitTime => "limit_time" - 44●
Cancelled => "cancelled" - 45
} - 46
} - 47
- 48
///| - 49
/// 是否属于资源限额类错误(调用方可据此决定"换大预算重试"还是"直接拒绝")。 - 50
/// Whether the code is a budget/limit failure (caller may raise the budget and retry). - 51
pub fn ErrorCode::is_budget(self : ErrorCode) -> Bool { - 52●
match self { - 53●
LimitBytes | LimitPixels | LimitTime => true - 54●
_ => false - 55
} - 56
} - 57
- 58
///| - 59
/// 统一图像错误 Unified image error - 60
/// - 61
/// 变体 Variants: - 62
/// - `Decode(ErrorCode, String)` - 解码阶段 Decode phase - 63
/// - `Encode(ErrorCode, String)` - 编码阶段 Encode phase - 64
/// - `Process(ErrorCode, String)` - 像素处理阶段 Process phase - 65
/// - `Budget(ErrorCode, String)` - 预算/限额拒绝 Budget rejection - 66
/// - `Cancelled(String)` - 调用方取消 Caller cancelled - 67
pub(all) suberror ImageError { - 68
Decode(ErrorCode, String) - 69
Encode(ErrorCode, String) - 70
Process(ErrorCode, String) - 71
Budget(ErrorCode, String) - 72
Cancelled(String) - 73
} - 74
- 75
///| - 76
/// 稳定错误码字符串,形如 `decode/truncated`、`budget/limit_pixels`、`cancelled`。 - 77
/// Stable code string such as `decode/truncated`, `budget/limit_pixels`, `cancelled`. - 78
pub fn ImageError::code(self : ImageError) -> String { - 79●
match self { - 80●
Decode(c, _) => "decode/" + c.to_string() - 81●
Encode(c, _) => "encode/" + c.to_string() - 82●
Process(c, _) => "process/" + c.to_string() - 83●
Budget(c, _) => "budget/" + c.to_string() - 84●
Cancelled(_) => "cancelled" - 85
} - 86
} - 87
- 88
///| - 89
/// 错误消息(面向人,含具体数值)。Human-readable message (may include concrete numbers). - 90
pub fn ImageError::message(self : ImageError) -> String { - 91●
match self { - 92
Decode(_, m) | Encode(_, m) | Process(_, m) | Budget(_, m) | Cancelled(m) => - 93●
m - 94
} - 95
} - 96
- 97
///| - 98
/// 建议动作:给下游/agent 的处置建议(稳定文本,便于直接回给调用方)。 - 99
/// Suggested action for callers/agents; stable text suitable to return as-is. - 100
pub fn image_error_hint(code : String) -> String { - 101●
let base = if code.contains("limit_pixels") { - 102●
"图像像素数超出预算:提高 max_pixels 或先降采样/分区解码;不要放宽到无上限。" - 103●
} else if code.contains("limit_bytes") { - 104●
"输入字节或内存分配超出预算:提高 max_bytes 或改为流式/分块处理;拒绝超限输入。" - 105●
} else if code.contains("limit_time") { - 106●
"处理耗时超出预算:缩小任务(降采样、裁剪 ROI)或提高 max_ms;必要时异步化。" - 107●
} else if code.contains("unsupported_format") { - 108●
"格式无对应编解码器:先用 detect_format 确认格式,或转成 PNG/JPEG/QOI 再处理。" - 109●
} else if code.contains("truncated") { - 110●
"输入被截断:丢弃该输入(不要重试同一份坏数据),并记录来源。" - 111●
} else if code.contains("invalid_header") || - 112●
code.contains("invalid_dimensions") { - 113●
"文件头/尺寸非法:当作坏图拒绝,不要按头部声明去预分配内存。" - 114●
} else if code.contains("invalid_argument") { - 115●
"调用参数非法:检查尺寸/通道/质量/策略取值范围。" - 116●
} else if code.contains("cancelled") { - 117●
"调用方主动取消:按取消语义返回,不要当作错误重试。" - 118●
} else if code.contains("encode") { - 119●
"编码失败:检查输入通道数与尺寸;必要时改格式(无损用 PNG/WebP-VP8L)。" - 120
} else { - 121●
"解码/处理失败:按坏图处理,记录 code 与 message 便于定位。" - 122
} - 123
base - 124
} - 125
- 126
///| - 127
/// 把旧 `LoadError` 投影成 `ImageError`(按消息特征归类到稳定码)。 - 128
/// Project legacy `LoadError` into `ImageError`, classifying by message traits. - 129
pub fn image_error_from_load(e : LoadError) -> ImageError { - 130●
match e { - 131●
FileIO(m) => Decode(InvalidArgument, m) - 132●
UnsupportedFormat(m) => Decode(UnsupportedFormat, m) - 133●
EncodeFailed(m) => Encode(EncodeFailed, m) - 134
DecodeFailed(m) => - 135●
if m.contains("budget/limit_time") || m.contains("limit_time") { - 136○
Budget(LimitTime, m) - 137●
} else if m.contains("cancelled") { - 138○
Cancelled(m) - 139●
} else if m.contains("截断") || - 140●
m.contains("过短") || - 141●
m.contains("truncated") { - 142●
Decode(Truncated, m) - 143●
} else if m.contains("超过") || - 144●
m.contains("溢出") || - 145●
m.contains("exceed") { - 146●
Decode(InvalidDimensions, m) - 147●
} else if m.contains("尺寸无效") || - 148●
m.contains("标记") || - 149●
m.contains("marker") || - 150●
m.contains("header") { - 151●
Decode(InvalidHeader, m) - 152●
} else if m.contains("不支持") || m.contains("unsupported") { - 153●
Decode(UnsupportedFormat, m) - 154
} else { - 155●
Decode(DecodeFailed, m) - 156
} - 157
} - 158
} - 159
- 160
///| - 161
/// 解码/处理预算(默认保守)。Decode/process limits with conservative defaults. - 162
/// - 163
/// - `max_pixels`:总像素上限(默认 5 亿,与 `MAX_TOTAL_PIXELS` 一致) - 164
/// - `max_bytes`:输入字节上限(默认 256 MiB) - 165
/// - `max_ms`:单次处理时间上限(默认 10 秒;纯 MoonBit 同步实现下为"准入检查 + 事后判定",不能强杀) - 166
pub(all) struct DecodeLimits { - 167
max_pixels : Int64 - 168
max_bytes : Int64 - 169
max_ms : Int64 - 170
} - 171
- 172
///| - 173
/// 默认预算。Default limits. - 174
pub fn DecodeLimits::default_limits() -> DecodeLimits { - 175●
{ max_pixels: 500000000L, max_bytes: 268435456L, max_ms: 10000L, } - 176
} - 177
- 178
///| - 179
/// 严格预算(面向不可信输入的批处理/服务端)。Strict limits for untrusted input. - 180
pub fn DecodeLimits::strict_limits() -> DecodeLimits { - 181●
{ max_pixels: 67108864L, max_bytes: 67108864L, max_ms: 3000L, } - 182
} - 183
- 184
///| - 185
/// 按格式上限构造预算(只覆盖像素数,字节/时间沿用默认)。 - 186
/// Build limits overriding only the pixel budget. - 187
pub fn DecodeLimits::with_max_pixels(max_pixels : Int64) -> DecodeLimits { - 188●
let d = DecodeLimits::default_limits() - 189
{ ..d, max_pixels, } - 190
} - 191
- 192
///| - 193
/// 校验输入字节数是否超预算。Reject input whose byte length exceeds the budget. - 194
pub fn DecodeLimits::check_bytes( - 195
self : DecodeLimits, - 196
bytes : Int64, - 197
) -> Unit raise ImageError { - 198●
if bytes > self.max_bytes { - 199●
raise Budget( - 200
LimitBytes, - 201
"输入字节 \{bytes} 超过上限 \{self.max_bytes}", - 202
) - 203
} - 204
} - 205
- 206
///| - 207
/// 校验像素数是否超预算。Reject pixel counts exceeding the budget. - 208
pub fn DecodeLimits::check_pixels( - 209
self : DecodeLimits, - 210
width : Int64, - 211
height : Int64, - 212
) -> Unit raise ImageError { - 213●
if width <= 0L || height <= 0L { - 214●
raise Decode( - 215
InvalidDimensions, - 216
"图像尺寸无效: width=\{width}, height=\{height}", - 217
) - 218
} - 219
let pixels = width * height - 220
if pixels > self.max_pixels { - 221●
raise Budget( - 222
LimitPixels, - 223
"图像像素数 \{pixels} 超过上限 \{self.max_pixels}", - 224
) - 225
} - 226
} - 227
- 228
///| - 229
/// 内存分配预算(供像素级算子使用)。Allocation budget for pixel operators. - 230
pub(all) struct AllocBudget { - 231
max_bytes : Int64 - 232
} - 233
- 234
///| - 235
/// 默认分配预算(512 MiB)。Default allocation budget. - 236
pub fn AllocBudget::default_budget() -> AllocBudget { - 237●
{ max_bytes: 536870912L, } - 238
} - 239
- 240
///| - 241
/// 需要 `width × height × channels` 字节时的准入检查。Admission check for an allocation. - 242
pub fn AllocBudget::check_image( - 243
self : AllocBudget, - 244
width : Int64, - 245
height : Int64, - 246
channels : Int64, - 247
what : String, - 248
) -> Unit raise ImageError { - 249●
if width <= 0L || height <= 0L || channels <= 0L { - 250●
raise Process( - 251
InvalidArgument, - 252
"\{what}: 尺寸/通道无效 (\{width}×\{height}×\{channels})", - 253
) - 254
} - 255
let need = width * height * channels - 256
if need > self.max_bytes { - 257●
raise Budget( - 258
LimitBytes, - 259
"\{what} 需要 \{need} 字节,超过分配预算 \{self.max_bytes}", - 260
) - 261
} - 262
}