PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Error/exceptions.php

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