Exceptions.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * @var list<int>
  31. */
  32. public array $ignoreCodes = [404];
  33. /**
  34. * --------------------------------------------------------------------------
  35. * Error Views Path
  36. * --------------------------------------------------------------------------
  37. * This is the path to the directory that contains the 'cli' and 'html'
  38. * directories that hold the views used to generate errors.
  39. *
  40. * Default: APPPATH.'Views/errors'
  41. */
  42. public string $errorViewPath = APPPATH . 'Views/errors';
  43. /**
  44. * --------------------------------------------------------------------------
  45. * HIDE FROM DEBUG TRACE
  46. * --------------------------------------------------------------------------
  47. * Any data that you would like to hide from the debug trace.
  48. * In order to specify 2 levels, use "/" to separate.
  49. * ex. ['server', 'setup/password', 'secret_token']
  50. *
  51. * @var list<string>
  52. */
  53. public array $sensitiveDataInTrace = [];
  54. /**
  55. * --------------------------------------------------------------------------
  56. * WHETHER TO THROW AN EXCEPTION ON DEPRECATED ERRORS
  57. * --------------------------------------------------------------------------
  58. * If set to `true`, DEPRECATED errors are only logged and no exceptions are
  59. * thrown. This option also works for user deprecations.
  60. */
  61. public bool $logDeprecations = true;
  62. /**
  63. * --------------------------------------------------------------------------
  64. * LOG LEVEL THRESHOLD FOR DEPRECATIONS
  65. * --------------------------------------------------------------------------
  66. * If `$logDeprecations` is set to `true`, this sets the log level
  67. * to which the deprecation will be logged. This should be one of the log
  68. * levels recognized by PSR-3.
  69. *
  70. * The related `Config\Logger::$threshold` should be adjusted, if needed,
  71. * to capture logging the deprecations.
  72. */
  73. public string $deprecationLogLevel = LogLevel::WARNING;
  74. /*
  75. * DEFINE THE HANDLERS USED
  76. * --------------------------------------------------------------------------
  77. * Given the HTTP status code, returns exception handler that
  78. * should be used to deal with this error. By default, it will run CodeIgniter's
  79. * default handler and display the error information in the expected format
  80. * for CLI, HTTP, or AJAX requests, as determined by is_cli() and the expected
  81. * response format.
  82. *
  83. * Custom handlers can be returned if you want to handle one or more specific
  84. * error codes yourself like:
  85. *
  86. * if (in_array($statusCode, [400, 404, 500])) {
  87. * return new \App\Libraries\MyExceptionHandler();
  88. * }
  89. * if ($exception instanceOf PageNotFoundException) {
  90. * return new \App\Libraries\MyExceptionHandler();
  91. * }
  92. */
  93. public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
  94. {
  95. return new ExceptionHandler($this);
  96. }
  97. }