Exceptions.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Config;
  3. use CodeIgniter\Config\BaseConfig;
  4. use CodeIgniter\Debug\ExceptionHandler;
  5. use CodeIgniter\Debug\ExceptionHandlerInterface;
  6. use Psr\Log\LogLevel;
  7. use Throwable;
  8. /**
  9. * Setup how the exception handler works.
  10. */
  11. class Exceptions extends BaseConfig
  12. {
  13. /**
  14. * --------------------------------------------------------------------------
  15. * LOG EXCEPTIONS?
  16. * --------------------------------------------------------------------------
  17. * If true, then exceptions will be logged
  18. * through Services::Log.
  19. *
  20. * Default: true
  21. */
  22. public bool $log = true;
  23. /**
  24. * --------------------------------------------------------------------------
  25. * DO NOT LOG STATUS CODES
  26. * --------------------------------------------------------------------------
  27. * Any status codes here will NOT be logged if logging is turned on.
  28. * By default, only 404 (Page Not Found) exceptions are ignored.
  29. */
  30. public array $ignoreCodes = [404];
  31. /**
  32. * --------------------------------------------------------------------------
  33. * Error Views Path
  34. * --------------------------------------------------------------------------
  35. * This is the path to the directory that contains the 'cli' and 'html'
  36. * directories that hold the views used to generate errors.
  37. *
  38. * Default: APPPATH.'Views/errors'
  39. */
  40. public string $errorViewPath = APPPATH . 'Views/errors';
  41. /**
  42. * --------------------------------------------------------------------------
  43. * HIDE FROM DEBUG TRACE
  44. * --------------------------------------------------------------------------
  45. * Any data that you would like to hide from the debug trace.
  46. * In order to specify 2 levels, use "/" to separate.
  47. * ex. ['server', 'setup/password', 'secret_token']
  48. */
  49. public array $sensitiveDataInTrace = [];
  50. /**
  51. * --------------------------------------------------------------------------
  52. * LOG DEPRECATIONS INSTEAD OF THROWING?
  53. * --------------------------------------------------------------------------
  54. * By default, CodeIgniter converts deprecations into exceptions. Also,
  55. * starting in PHP 8.1 will cause a lot of deprecated usage warnings.
  56. * Use this option to temporarily cease the warnings and instead log those.
  57. * This option also works for user deprecations.
  58. */
  59. public bool $logDeprecations = true;
  60. /**
  61. * --------------------------------------------------------------------------
  62. * LOG LEVEL THRESHOLD FOR DEPRECATIONS
  63. * --------------------------------------------------------------------------
  64. * If `$logDeprecations` is set to `true`, this sets the log level
  65. * to which the deprecation will be logged. This should be one of the log
  66. * levels recognized by PSR-3.
  67. *
  68. * The related `Config\Logger::$threshold` should be adjusted, if needed,
  69. * to capture logging the deprecations.
  70. */
  71. public string $deprecationLogLevel = LogLevel::WARNING;
  72. /*
  73. * DEFINE THE HANDLERS USED
  74. * --------------------------------------------------------------------------
  75. * Given the HTTP status code, returns exception handler that
  76. * should be used to deal with this error. By default, it will run CodeIgniter's
  77. * default handler and display the error information in the expected format
  78. * for CLI, HTTP, or AJAX requests, as determined by is_cli() and the expected
  79. * response format.
  80. *
  81. * Custom handlers can be returned if you want to handle one or more specific
  82. * error codes yourself like:
  83. *
  84. * if (in_array($statusCode, [400, 404, 500])) {
  85. * return new \App\Libraries\MyExceptionHandler();
  86. * }
  87. * if ($exception instanceOf PageNotFoundException) {
  88. * return new \App\Libraries\MyExceptionHandler();
  89. * }
  90. */
  91. public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
  92. {
  93. return new ExceptionHandler($this);
  94. }
  95. }