exceptions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  16. * @package cake.libs.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.libs
  26. */
  27. class HttpException extends RuntimeException { }
  28. /**
  29. * Represents an HTTP 400 error.
  30. *
  31. * @package cake.libs
  32. */
  33. class BadRequestException extends HttpException {
  34. /**
  35. * Constructor
  36. *
  37. * @param string $message If no message is given 'Bad Request' will be the message
  38. * @param string $code Status code, defaults to 401
  39. */
  40. public function __construct($message = null, $code = 400) {
  41. if (empty($message)) {
  42. $message = 'Bad Request';
  43. }
  44. parent::__construct($message, $code);
  45. }
  46. }
  47. /**
  48. * Represents an HTTP 401 error.
  49. *
  50. * @package cake.libs
  51. */
  52. class UnauthorizedException extends HttpException {
  53. /**
  54. * Constructor
  55. *
  56. * @param string $message If no message is given 'Unauthorized' will be the message
  57. * @param string $code Status code, defaults to 401
  58. */
  59. public function __construct($message = null, $code = 401) {
  60. if (empty($message)) {
  61. $message = 'Unauthorized';
  62. }
  63. parent::__construct($message, $code);
  64. }
  65. }
  66. /**
  67. * Represents an HTTP 403 error.
  68. *
  69. * @package cake.libs
  70. */
  71. class ForbiddenException extends HttpException {
  72. /**
  73. * Constructor
  74. *
  75. * @param string $message If no message is given 'Forbidden' will be the message
  76. * @param string $code Status code, defaults to 401
  77. */
  78. public function __construct($message = null, $code = 403) {
  79. if (empty($message)) {
  80. $message = 'Forbidden';
  81. }
  82. parent::__construct($message, $code);
  83. }
  84. }
  85. /**
  86. * Represents an HTTP 404 error.
  87. *
  88. * @package cake.libs
  89. */
  90. class NotFoundException extends HttpException {
  91. /**
  92. * Constructor
  93. *
  94. * @param string $message If no message is given 'Not Found' will be the message
  95. * @param string $code Status code, defaults to 404
  96. */
  97. public function __construct($message = null, $code = 404) {
  98. if (empty($message)) {
  99. $message = 'Not Found';
  100. }
  101. parent::__construct($message, $code);
  102. }
  103. }
  104. /**
  105. * Represents an HTTP 405 error.
  106. *
  107. * @package cake.libs
  108. */
  109. class MethodNotAllowedException extends HttpException {
  110. /**
  111. * Constructor
  112. *
  113. * @param string $message If no message is given 'Method Not Allowed' will be the message
  114. * @param string $code Status code, defaults to 401
  115. */
  116. public function __construct($message = null, $code = 405) {
  117. if (empty($message)) {
  118. $message = 'Method Not Allowed';
  119. }
  120. parent::__construct($message, $code);
  121. }
  122. }
  123. /**
  124. * Represents an HTTP 500 error.
  125. *
  126. * @package cake.libs
  127. */
  128. class InternalErrorException extends HttpException {
  129. /**
  130. * Constructor
  131. *
  132. * @param string $message If no message is given 'Not Found' will be the message
  133. * @param string $code Status code, defaults to 404
  134. */
  135. public function __construct($message = null, $code = 500) {
  136. if (empty($message)) {
  137. $message = 'Internal Server Error';
  138. }
  139. parent::__construct($message, $code);
  140. }
  141. }
  142. /**
  143. * CakeException is used a base class for CakePHP's internal exceptions.
  144. * In general framework errors are interpreted as 500 code errors.
  145. *
  146. * @package cake.libs
  147. */
  148. class CakeException extends RuntimeException {
  149. /**
  150. * Array of attributes that are passed in from the constructor, and
  151. * made available in the view when a development error is displayed.
  152. *
  153. * @var array
  154. */
  155. protected $_attributes = array();
  156. /**
  157. * Template string that has attributes sprintf()'ed into it.
  158. *
  159. * @var string
  160. */
  161. protected $_messageTemplate = '';
  162. /**
  163. * Constructor.
  164. *
  165. * Allows you to create exceptions that are treated as framework errors and disabled
  166. * when debug = 0.
  167. *
  168. * @param mixed $message Either the string of the error message, or an array of attributes
  169. * that are made available in the view, and sprintf()'d into CakeException::$_messageTemplate
  170. * @param string $code The code of the error, is also the HTTP status code for the error.
  171. */
  172. public function __construct($message, $code = 500) {
  173. if (is_array($message)) {
  174. $this->_attributes = $message;
  175. $message = __($this->_messageTemplate, $message);
  176. }
  177. parent::__construct($message, $code);
  178. }
  179. /**
  180. * Get the passed in attributes
  181. *
  182. * @return array
  183. */
  184. public function getAttributes() {
  185. return $this->_attributes;
  186. }
  187. }
  188. /**
  189. * Missing Controller exception - used when a controller
  190. * cannot be found.
  191. *
  192. * @package cake.libs
  193. */
  194. class MissingControllerException extends CakeException {
  195. protected $_messageTemplate = 'Controller class %s could not be found.';
  196. public function __construct($message, $code = 404) {
  197. parent::__construct($message, $code);
  198. }
  199. }
  200. /**
  201. * Missing Action exception - used when a controller action
  202. * cannot be found.
  203. *
  204. * @package cake.libs
  205. */
  206. class MissingActionException extends CakeException {
  207. protected $_messageTemplate = 'Action %s::%s() could not be found.';
  208. public function __construct($message, $code = 404) {
  209. parent::__construct($message, $code);
  210. }
  211. }
  212. /**
  213. * Private Action exception - used when a controller action
  214. * is protected, or starts with a `_`.
  215. *
  216. * @package cake.libs
  217. */
  218. class PrivateActionException extends CakeException {
  219. protected $_messageTemplate = 'Private Action %s::%s() is not directly accessible.';
  220. public function __construct($message, $code = 404, Exception $previous = null) {
  221. parent::__construct($message, $code, $previous);
  222. }
  223. }
  224. /**
  225. * Used when a Component file cannot be found.
  226. *
  227. * @package cake.libs
  228. */
  229. class MissingComponentFileException extends CakeException {
  230. protected $_messageTemplate = 'Component File "%s" is missing.';
  231. }
  232. /**
  233. * Used when a Component class cannot be found.
  234. *
  235. * @package cake.libs
  236. */
  237. class MissingComponentClassException extends CakeException {
  238. protected $_messageTemplate = 'Component class "%s" is missing.';
  239. }
  240. /**
  241. * Used when a Behavior file cannot be found.
  242. *
  243. * @package cake.libs
  244. */
  245. class MissingBehaviorFileException extends CakeException { }
  246. /**
  247. * Used when a Behavior class cannot be found.
  248. *
  249. * @package cake.libs
  250. */
  251. class MissingBehaviorClassException extends CakeException { }
  252. /**
  253. * Used when a view file cannot be found.
  254. *
  255. * @package cake.libs
  256. */
  257. class MissingViewException extends CakeException {
  258. protected $_messageTemplate = 'View file "%s" is missing.';
  259. }
  260. /**
  261. * Used when a layout file cannot be found.
  262. *
  263. * @package cake.libs
  264. */
  265. class MissingLayoutException extends CakeException {
  266. protected $_messageTemplate = 'Layout file "%s" is missing.';
  267. }
  268. /**
  269. * Used when a helper file cannot be found.
  270. *
  271. * @package cake.libs
  272. */
  273. class MissingHelperFileException extends CakeException {
  274. protected $_messageTemplate = 'Helper File "%s" is missing.';
  275. }
  276. /**
  277. * Used when a helper class cannot be found.
  278. *
  279. * @package cake.libs
  280. */
  281. class MissingHelperClassException extends CakeException {
  282. protected $_messageTemplate = 'Helper class "%s" is missing.';
  283. }
  284. /**
  285. * Runtime Exceptions for ConnectionManager
  286. *
  287. * @package cake.libs
  288. */
  289. class MissingDatabaseException extends CakeException {
  290. protected $_messageTemplate = 'Database connection "%s" could not be found.';
  291. }
  292. /**
  293. * Used when no connections can be found.
  294. *
  295. * @package cake.libs
  296. */
  297. class MissingConnectionException extends CakeException {
  298. protected $_messageTemplate = 'Database connection "%s" is missing.';
  299. }
  300. /**
  301. * Used when a Task file cannot be found.
  302. *
  303. * @package cake.libs
  304. */
  305. class MissingTaskFileException extends CakeException {
  306. protected $_messageTemplate = 'Task file "%s" is missing.';
  307. }
  308. /**
  309. * Used when a Task class cannot be found.
  310. *
  311. * @package cake.libs
  312. */
  313. class MissingTaskClassException extends CakeException {
  314. protected $_messageTemplate = 'Task class "%s" is missing.';
  315. }
  316. /**
  317. * Used when a shell method cannot be found.
  318. *
  319. * @package cake.libs
  320. */
  321. class MissingShellMethodException extends CakeException {
  322. protected $_messageTemplate = "Unknown command %1\$s %2\$s.\nFor usage try `cake %1\$s --help`";
  323. }
  324. /**
  325. * Used when a shell class cannot be found.
  326. *
  327. * @package cake.libs
  328. */
  329. class MissingShellClassException extends CakeException {
  330. protected $_messageTemplate = "Shell class %s could not be loaded.";
  331. }
  332. /**
  333. * Used when a shell class cannot be found.
  334. *
  335. * @package cake.libs
  336. */
  337. class MissingShellFileException extends CakeException {
  338. protected $_messageTemplate = "Shell file %s could not be loaded.";
  339. }
  340. /**
  341. * Exception class to be thrown when a database file is not found
  342. *
  343. * @package cake.libs
  344. */
  345. class MissingDatasourceConfigException extends CakeException {
  346. protected $_messageTemplate = 'Database connection "%s" could not be loaded.';
  347. }
  348. /**
  349. * Exception class to be thrown when a database file is not found
  350. *
  351. * @package cake.libs
  352. */
  353. class MissingDatasourceFileException extends CakeException {
  354. protected $_messageTemplate = 'Database connection "%s" could not be loaded.';
  355. }
  356. /**
  357. * Exception class to be thrown when a database table is not found in the datasource
  358. *
  359. * @package cake.libs
  360. */
  361. class MissingTableException extends CakeException {
  362. protected $_messageTemplate = 'Database table %s for model %s was not found.';
  363. }
  364. /**
  365. * Exception Raised when a Model could not be found.
  366. *
  367. * @package cake.libs
  368. */
  369. class MissingModelException extends CakeException {
  370. protected $_messageTemplate = 'Model %s could not be found.';
  371. }
  372. /**
  373. * Exception Raised when a test loader could not be found
  374. *
  375. * @package cake.libs
  376. */
  377. class MissingTestLoaderException extends CakeException {
  378. protected $_messageTemplate = 'Test loader %s could not be found.';
  379. }
  380. /**
  381. * Exception class for Cache. This exception will be thrown from Cache when it
  382. * encounters an error.
  383. *
  384. * @package cake.libs
  385. */
  386. class CacheException extends CakeException { }
  387. /**
  388. * Exception class for Router. This exception will be thrown from Router when it
  389. * encounters an error.
  390. *
  391. * @package cake.libs
  392. */
  393. class RouterException extends CakeException { }
  394. /**
  395. * Exception class for CakeLog. This exception will be thrown from CakeLog when it
  396. * encounters an error.
  397. *
  398. * @package cake.libs
  399. */
  400. class CakeLogException extends CakeException { }
  401. /**
  402. * Exception class for CakeSession. This exception will be thrown from CakeSession when it
  403. * encounters an error.
  404. *
  405. * @package cake.libs
  406. */
  407. class CakeSessionException extends CakeException { }
  408. /**
  409. * Exception class for Configure. This exception will be thrown from Configure when it
  410. * encounters an error.
  411. *
  412. * @package cake.libs
  413. */
  414. class ConfigureException extends CakeException { }
  415. /**
  416. * Exception class for Socket. This exception will be thrown from CakeSocket, HttpSocket and HttpResponse when it
  417. * encounters an error.
  418. *
  419. * @package cake.libs
  420. */
  421. class SocketException extends CakeException { }
  422. /**
  423. * Exception class for Xml. This exception will be thrown from Xml when it
  424. * encounters an error.
  425. *
  426. * @package cake.libs
  427. */
  428. class XmlException extends CakeException { }
  429. /**
  430. * Exception class for Console libraries. This exception will be thrown from Console library
  431. * classes when they encounter an error.
  432. *
  433. * @package cake.libs
  434. */
  435. class ConsoleException extends CakeException { }