PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Error/exceptions.php

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