/lib/Cake/Error/exceptions.php

https://github.com/gfarrell/KORDS · PHP · 579 lines · 177 code · 105 blank · 297 comment · 11 complexity · 9b110bb157704648d2ff5623e0d034ef MD5 · raw file

  1. <?php
  2. /**
  3. * Exceptions file. Contains the various exceptions CakePHP will throw until they are
  4. * moved into their permanent location.
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html
  16. * @package Cake.Error
  17. * @since CakePHP(tm) v 2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Parent class for all of the HTTP related exceptions in CakePHP.
  22. * All HTTP status/error related exceptions should extend this class so
  23. * catch blocks can be specifically typed.
  24. *
  25. * @package Cake.Error
  26. */
  27. if (!class_exists('HttpException')) {
  28. class HttpException extends RuntimeException {
  29. }
  30. }
  31. /**
  32. * Represents an HTTP 400 error.
  33. *
  34. * @package Cake.Error
  35. */
  36. class BadRequestException extends HttpException {
  37. /**
  38. * Constructor
  39. *
  40. * @param string $message If no message is given 'Bad Request' will be the message
  41. * @param string $code Status code, defaults to 400
  42. */
  43. public function __construct($message = null, $code = 400) {
  44. if (empty($message)) {
  45. $message = 'Bad Request';
  46. }
  47. parent::__construct($message, $code);
  48. }
  49. }
  50. /**
  51. * Represents an HTTP 401 error.
  52. *
  53. * @package Cake.Error
  54. */
  55. class UnauthorizedException extends HttpException {
  56. /**
  57. * Constructor
  58. *
  59. * @param string $message If no message is given 'Unauthorized' will be the message
  60. * @param string $code Status code, defaults to 401
  61. */
  62. public function __construct($message = null, $code = 401) {
  63. if (empty($message)) {
  64. $message = 'Unauthorized';
  65. }
  66. parent::__construct($message, $code);
  67. }
  68. }
  69. /**
  70. * Represents an HTTP 403 error.
  71. *
  72. * @package Cake.Error
  73. */
  74. class ForbiddenException extends HttpException {
  75. /**
  76. * Constructor
  77. *
  78. * @param string $message If no message is given 'Forbidden' will be the message
  79. * @param string $code Status code, defaults to 403
  80. */
  81. public function __construct($message = null, $code = 403) {
  82. if (empty($message)) {
  83. $message = 'Forbidden';
  84. }
  85. parent::__construct($message, $code);
  86. }
  87. }
  88. /**
  89. * Represents an HTTP 404 error.
  90. *
  91. * @package Cake.Error
  92. */
  93. class NotFoundException extends HttpException {
  94. /**
  95. * Constructor
  96. *
  97. * @param string $message If no message is given 'Not Found' will be the message
  98. * @param string $code Status code, defaults to 404
  99. */
  100. public function __construct($message = null, $code = 404) {
  101. if (empty($message)) {
  102. $message = 'Not Found';
  103. }
  104. parent::__construct($message, $code);
  105. }
  106. }
  107. /**
  108. * Represents an HTTP 405 error.
  109. *
  110. * @package Cake.Error
  111. */
  112. class MethodNotAllowedException extends HttpException {
  113. /**
  114. * Constructor
  115. *
  116. * @param string $message If no message is given 'Method Not Allowed' will be the message
  117. * @param string $code Status code, defaults to 405
  118. */
  119. public function __construct($message = null, $code = 405) {
  120. if (empty($message)) {
  121. $message = 'Method Not Allowed';
  122. }
  123. parent::__construct($message, $code);
  124. }
  125. }
  126. /**
  127. * Represents an HTTP 500 error.
  128. *
  129. * @package Cake.Error
  130. */
  131. class InternalErrorException extends HttpException {
  132. /**
  133. * Constructor
  134. *
  135. * @param string $message If no message is given 'Internal Server Error' will be the message
  136. * @param string $code Status code, defaults to 500
  137. */
  138. public function __construct($message = null, $code = 500) {
  139. if (empty($message)) {
  140. $message = 'Internal Server Error';
  141. }
  142. parent::__construct($message, $code);
  143. }
  144. }
  145. /**
  146. * CakeException is used a base class for CakePHP's internal exceptions.
  147. * In general framework errors are interpreted as 500 code errors.
  148. *
  149. * @package Cake.Error
  150. */
  151. class CakeException extends RuntimeException {
  152. /**
  153. * Array of attributes that are passed in from the constructor, and
  154. * made available in the view when a development error is displayed.
  155. *
  156. * @var array
  157. */
  158. protected $_attributes = array();
  159. /**
  160. * Template string that has attributes sprintf()'ed into it.
  161. *
  162. * @var string
  163. */
  164. protected $_messageTemplate = '';
  165. /**
  166. * Constructor.
  167. *
  168. * Allows you to create exceptions that are treated as framework errors and disabled
  169. * when debug = 0.
  170. *
  171. * @param string|array $message Either the string of the error message, or an array of attributes
  172. * that are made available in the view, and sprintf()'d into CakeException::$_messageTemplate
  173. * @param string $code The code of the error, is also the HTTP status code for the error.
  174. */
  175. public function __construct($message, $code = 500) {
  176. if (is_array($message)) {
  177. $this->_attributes = $message;
  178. $message = __d('cake_dev', $this->_messageTemplate, $message);
  179. }
  180. parent::__construct($message, $code);
  181. }
  182. /**
  183. * Get the passed in attributes
  184. *
  185. * @return array
  186. */
  187. public function getAttributes() {
  188. return $this->_attributes;
  189. }
  190. }
  191. /**
  192. * Missing Controller exception - used when a controller
  193. * cannot be found.
  194. *
  195. * @package Cake.Error
  196. */
  197. class MissingControllerException extends CakeException {
  198. protected $_messageTemplate = 'Controller class %s could not be found.';
  199. public function __construct($message, $code = 404) {
  200. parent::__construct($message, $code);
  201. }
  202. }
  203. /**
  204. * Missing Action exception - used when a controller action
  205. * cannot be found.
  206. *
  207. * @package Cake.Error
  208. */
  209. class MissingActionException extends CakeException {
  210. protected $_messageTemplate = 'Action %s::%s() could not be found.';
  211. public function __construct($message, $code = 404) {
  212. parent::__construct($message, $code);
  213. }
  214. }
  215. /**
  216. * Private Action exception - used when a controller action
  217. * starts with a `_`.
  218. *
  219. * @package Cake.Error
  220. */
  221. class PrivateActionException extends CakeException {
  222. protected $_messageTemplate = 'Private Action %s::%s() is not directly accessible.';
  223. public function __construct($message, $code = 404, Exception $previous = null) {
  224. parent::__construct($message, $code, $previous);
  225. }
  226. }
  227. /**
  228. * Used when a component cannot be found.
  229. *
  230. * @package Cake.Error
  231. */
  232. class MissingComponentException extends CakeException {
  233. protected $_messageTemplate = 'Component class %s could not be found.';
  234. }
  235. /**
  236. * Used when a behavior cannot be found.
  237. *
  238. * @package Cake.Error
  239. */
  240. class MissingBehaviorException extends CakeException {
  241. protected $_messageTemplate = 'Behavior class %s could not be found.';
  242. }
  243. /**
  244. * Used when a view file cannot be found.
  245. *
  246. * @package Cake.Error
  247. */
  248. class MissingViewException extends CakeException {
  249. protected $_messageTemplate = 'View file "%s" is missing.';
  250. }
  251. /**
  252. * Used when a layout file cannot be found.
  253. *
  254. * @package Cake.Error
  255. */
  256. class MissingLayoutException extends CakeException {
  257. protected $_messageTemplate = 'Layout file "%s" is missing.';
  258. }
  259. /**
  260. * Used when a helper cannot be found.
  261. *
  262. * @package Cake.Error
  263. */
  264. class MissingHelperException extends CakeException {
  265. protected $_messageTemplate = 'Helper class %s could not be found.';
  266. }
  267. /**
  268. * Runtime Exceptions for ConnectionManager
  269. *
  270. * @package Cake.Error
  271. */
  272. class MissingDatabaseException extends CakeException {
  273. protected $_messageTemplate = 'Database connection "%s" could not be found.';
  274. }
  275. /**
  276. * Used when no connections can be found.
  277. *
  278. * @package Cake.Error
  279. */
  280. class MissingConnectionException extends CakeException {
  281. protected $_messageTemplate = 'Database connection "%s" is missing, or could not be created.';
  282. public function __construct($message, $code = 500) {
  283. if (is_array($message)) {
  284. $message += array('enabled' => true);
  285. }
  286. parent::__construct($message, $code);
  287. }
  288. }
  289. /**
  290. * Used when a Task cannot be found.
  291. *
  292. * @package Cake.Error
  293. */
  294. class MissingTaskException extends CakeException {
  295. protected $_messageTemplate = 'Task class %s could not be found.';
  296. }
  297. /**
  298. * Used when a shell method cannot be found.
  299. *
  300. * @package Cake.Error
  301. */
  302. class MissingShellMethodException extends CakeException {
  303. protected $_messageTemplate = "Unknown command %1\$s %2\$s.\nFor usage try `cake %1\$s --help`";
  304. }
  305. /**
  306. * Used when a shell cannot be found.
  307. *
  308. * @package Cake.Error
  309. */
  310. class MissingShellException extends CakeException {
  311. protected $_messageTemplate = 'Shell class %s could not be found.';
  312. }
  313. /**
  314. * Exception class to be thrown when a datasource configuration is not found
  315. *
  316. * @package Cake.Error
  317. */
  318. class MissingDatasourceConfigException extends CakeException {
  319. protected $_messageTemplate = 'The datasource configuration "%s" was not found in database.php';
  320. }
  321. /**
  322. * Used when a datasource cannot be found.
  323. *
  324. * @package Cake.Error
  325. */
  326. class MissingDatasourceException extends CakeException {
  327. protected $_messageTemplate = 'Datasource class %s could not be found.';
  328. }
  329. /**
  330. * Exception class to be thrown when a database table is not found in the datasource
  331. *
  332. * @package Cake.Error
  333. */
  334. class MissingTableException extends CakeException {
  335. protected $_messageTemplate = 'Table %s for model %s was not found in datasource %s.';
  336. }
  337. /**
  338. * Exception raised when a Model could not be found.
  339. *
  340. * @package Cake.Error
  341. */
  342. class MissingModelException extends CakeException {
  343. protected $_messageTemplate = 'Model %s could not be found.';
  344. }
  345. /**
  346. * Exception raised when a test loader could not be found
  347. *
  348. * @package Cake.Error
  349. */
  350. class MissingTestLoaderException extends CakeException {
  351. protected $_messageTemplate = 'Test loader %s could not be found.';
  352. }
  353. /**
  354. * Exception raised when a plugin could not be found
  355. *
  356. * @package Cake.Error
  357. */
  358. class MissingPluginException extends CakeException {
  359. protected $_messageTemplate = 'Plugin %s could not be found.';
  360. }
  361. /**
  362. * Exception raised when a Dispatcher filter could not be found
  363. *
  364. * @package Cake.Error
  365. */
  366. class MissingDispatcherFilterException extends CakeException {
  367. protected $_messageTemplate = 'Dispatcher filter %s could not be found.';
  368. }
  369. /**
  370. * Exception class for AclComponent and Interface implementations.
  371. *
  372. * @package Cake.Error
  373. */
  374. class AclException extends CakeException {
  375. }
  376. /**
  377. * Exception class for Cache. This exception will be thrown from Cache when it
  378. * encounters an error.
  379. *
  380. * @package Cake.Error
  381. */
  382. class CacheException extends CakeException {
  383. }
  384. /**
  385. * Exception class for Router. This exception will be thrown from Router when it
  386. * encounters an error.
  387. *
  388. * @package Cake.Error
  389. */
  390. class RouterException extends CakeException {
  391. }
  392. /**
  393. * Exception class for CakeLog. This exception will be thrown from CakeLog when it
  394. * encounters an error.
  395. *
  396. * @package Cake.Error
  397. */
  398. class CakeLogException extends CakeException {
  399. }
  400. /**
  401. * Exception class for CakeSession. This exception will be thrown from CakeSession when it
  402. * encounters an error.
  403. *
  404. * @package Cake.Error
  405. */
  406. class CakeSessionException extends CakeException {
  407. }
  408. /**
  409. * Exception class for Configure. This exception will be thrown from Configure when it
  410. * encounters an error.
  411. *
  412. * @package Cake.Error
  413. */
  414. class ConfigureException extends CakeException {
  415. }
  416. /**
  417. * Exception class for Socket. This exception will be thrown from CakeSocket, CakeEmail, HttpSocket
  418. * SmtpTransport, MailTransport and HttpResponse when it encounters an error.
  419. *
  420. * @package Cake.Error
  421. */
  422. class SocketException extends CakeException {
  423. }
  424. /**
  425. * Exception class for Xml. This exception will be thrown from Xml when it
  426. * encounters an error.
  427. *
  428. * @package Cake.Error
  429. */
  430. class XmlException extends CakeException {
  431. }
  432. /**
  433. * Exception class for Console libraries. This exception will be thrown from Console library
  434. * classes when they encounter an error.
  435. *
  436. * @package Cake.Error
  437. */
  438. class ConsoleException extends CakeException {
  439. }
  440. /**
  441. * Represents a fatal error
  442. *
  443. * @package Cake.Error
  444. */
  445. class FatalErrorException extends CakeException {
  446. /**
  447. * Constructor
  448. *
  449. * @param string $message
  450. * @param integer $code
  451. * @param string $file
  452. * @param integer $line
  453. */
  454. public function __construct($message, $code = 500, $file = null, $line = null) {
  455. parent::__construct($message, $code);
  456. if ($file) {
  457. $this->file = $file;
  458. }
  459. if ($line) {
  460. $this->line = $line;
  461. }
  462. }
  463. }
  464. /**
  465. * Not Implemented Exception - used when an API method is not implemented
  466. *
  467. * @package Cake.Error
  468. */
  469. class NotImplementedException extends CakeException {
  470. protected $_messageTemplate = '%s is not implemented.';
  471. public function __construct($message, $code = 501) {
  472. parent::__construct($message, $code);
  473. }
  474. }