ErrorController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * Part of Shuzanne - An extensible sequel to an open-source imageboard.
  4. *
  5. * @package Shuzanne
  6. * @author MisleadingName, Shuzanne Contributors
  7. * @license MPL v2.0
  8. *
  9. * This Source Code Form is subject to the terms of the Mozilla Public
  10. * License, v. 2.0. If a copy of the MPL was not distributed with this
  11. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  12. */
  13. namespace app\Controllers;
  14. use Pecee\SimpleRouter\SimpleRouter;
  15. use app\Interfaces\IRouteController;
  16. class ErrorController implements IRouteController
  17. {
  18. /**
  19. * @var array<int, string> HTTP status code lookup
  20. */
  21. private static array $codeLookup = [
  22. 100 => "Continue",
  23. 101 => "Switching Protocols",
  24. 102 => "Processing",
  25. 103 => "Early Hints",
  26. 200 => "OK",
  27. 201 => "Created",
  28. 202 => "Accepted",
  29. 203 => "Non-Authoritative Information",
  30. 204 => "No Content",
  31. 205 => "Reset Content",
  32. 206 => "Partial Content",
  33. 207 => "Multi-Status",
  34. 208 => "Already Reported",
  35. 226 => "IM Used",
  36. 300 => "Multiple Choices",
  37. 301 => "Moved Permanently",
  38. 302 => "Found",
  39. 303 => "See Other",
  40. 304 => "Not Modified",
  41. 305 => "Use Proxy",
  42. 306 => "Switch Proxy",
  43. 307 => "Temporary Redirect",
  44. 308 => "Permanent Redirect",
  45. 400 => "Bad Request",
  46. 401 => "Unauthorized",
  47. 402 => "Payment Required",
  48. 403 => "Forbidden",
  49. 404 => "Not Found",
  50. 405 => "Method Not Allowed",
  51. 406 => "Not Acceptable",
  52. 407 => "Proxy Authentication Required",
  53. 408 => "Request Timeout",
  54. 409 => "Conflict",
  55. 410 => "Gone",
  56. 411 => "Length Required",
  57. 412 => "Precondition Failed",
  58. 413 => "Request Entity Too Large",
  59. 414 => "Request-URI Too Long",
  60. 415 => "Unsupported Media Type",
  61. 416 => "Requested Range Not Satisfiable",
  62. 417 => "Expectation Failed",
  63. 418 => "I'm a teapot",
  64. 421 => "Misdirected Request",
  65. 422 => "Unprocessable Entity",
  66. 423 => "Locked",
  67. 424 => "Failed Dependency",
  68. 425 => "Too Early",
  69. 426 => "Upgrade Required",
  70. 428 => "Precondition Required",
  71. 429 => "Too Many Requests",
  72. 431 => "Request Header Fields Too Large",
  73. 451 => "Unavailable For Legal Reasons",
  74. 500 => "Internal Server Error",
  75. 501 => "Not Implemented",
  76. 502 => "Bad Gateway",
  77. 503 => "Service Unavailable",
  78. 504 => "Gateway Timeout",
  79. 505 => "HTTP Version Not Supported",
  80. 506 => "Variant Also Negotiates",
  81. 507 => "Insufficient Storage",
  82. 508 => "Loop Detected",
  83. 510 => "Not Extended",
  84. 511 => "Network Authentication Required",
  85. 599 => "Network Connect Timeout Error",
  86. ];
  87. public static function HtmlError($code): string
  88. {
  89. return render("pages/error.twig", ["error_code" => $code, "error_name" => array_key_exists($code, self::$codeLookup) ? self::$codeLookup[$code] : "Unknown Error"]);
  90. }
  91. public static function ApiError($code)
  92. {
  93. CORSHelper();
  94. return api_json([
  95. "error" => $code,
  96. "message" => self::$codeLookup[$code] ?? 500,
  97. ]);
  98. }
  99. static function RegisterRoutes(): void
  100. {
  101. // It's an error handler, no need for routing.
  102. }
  103. }