| 123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace App\Support;
- use PDOException;
- use Throwable;
- final class AnnouncementFailure
- {
- public static function message(Throwable $error): string
- {
- if ($error instanceof PDOException) {
- $state = (string) ($error->errorInfo[0] ?? $error->getCode());
- $code = (int) ($error->errorInfo[1] ?? 0);
- $text = strtolower($error->getMessage());
- if ($state === '42S02' || str_contains($text, 'no such table')) {
- if (str_contains($text, 'site_realtime_events')) return '实时通知服务未初始化,请先完成系统更新';
- if (str_contains($text, 'operation_audits')) return '操作记录未初始化,请先完成系统更新';
- return '公告数据未初始化,请先完成系统更新';
- }
- if ($state === '42S22' || str_contains($text, 'no such column') || str_contains($text, 'has no column named')) return '公告字段结构未更新完整,请先完成系统更新';
- if (in_array($code, [2002, 2003, 2006, 2013], true) || str_starts_with($state, '08')) return '数据库连接失败,请检查数据库服务后重新保存';
- if ($code === 1406 || $state === '22001') return '公告内容超出存储长度,请缩短内容后保存';
- if (in_array($code, [1205, 1213], true)) return '公告数据正在被更新,请重新提交';
- if ($code === 1062) return '公告数据重复,请刷新页面后重新保存';
- if (in_array($code, [1048, 1364], true)) return '公告缺少必要数据,请检查填写内容及系统更新状态';
- return '公告数据库操作失败(错误代码' . ($code ?: preg_replace('/[^A-Za-z0-9]/', '', $state)) . ')';
- }
- if ($error instanceof \JsonException) return '公告内容格式错误,请检查后重新保存';
- if ($error instanceof \TypeError) return '公告字段类型处理错误,请联系管理员修正';
- return '公告处理失败(' . class_basename($error) . '),请联系管理员检查该错误';
- }
- }
|