AnnouncementFailure.php 2.1 KB

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