統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Error / exceptions.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (13.823 KB)

1
<?php
2
/**
3
 * Exceptions file. Contains the various exceptions CakePHP will throw until they are
4
 * moved into their permanent location.
5
 *
6
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
7
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
14
 * @link          http://book.cakephp.org/2.0/en/development/testing.html
15
 * @package       Cake.Error
16
 * @since         CakePHP(tm) v 2.0
17
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
18
 */
19

    
20
/**
21
 * Base class that all Exceptions extend.
22
 *
23
 * @package       Cake.Error
24
 */
25
class CakeBaseException extends RuntimeException {
26

    
27
/**
28
 * Array of headers to be passed to CakeResponse::header()
29
 *
30
 * @var array
31
 */
32
        protected $_responseHeaders = null;
33

    
34
/**
35
 * Get/set the response header to be used
36
 *
37
 * @param string|array $header An array of header strings or a single header string
38
 *  - an associative array of "header name" => "header value"
39
 *  - an array of string headers is also accepted
40
 * @param string $value The header value.
41
 * @return array
42
 * @see CakeResponse::header()
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

    
56
/**
57
 * Parent class for all of the HTTP related exceptions in CakePHP.
58
 * All HTTP status/error related exceptions should extend this class so
59
 * catch blocks can be specifically typed.
60
 *
61
 * @package       Cake.Error
62
 */
63
if (!class_exists('HttpException', false)) {
64
        class HttpException extends CakeBaseException {
65
        }
66
}
67

    
68
/**
69
 * Represents an HTTP 400 error.
70
 *
71
 * @package       Cake.Error
72
 */
73
class BadRequestException extends HttpException {
74

    
75
/**
76
 * Constructor
77
 *
78
 * @param string $message If no message is given 'Bad Request' will be the message
79
 * @param int $code Status code, defaults to 400
80
 */
81
        public function __construct($message = null, $code = 400) {
82
                if (empty($message)) {
83
                        $message = 'Bad Request';
84
                }
85
                parent::__construct($message, $code);
86
        }
87

    
88
}
89

    
90
/**
91
 * Represents an HTTP 401 error.
92
 *
93
 * @package       Cake.Error
94
 */
95
class UnauthorizedException extends HttpException {
96

    
97
/**
98
 * Constructor
99
 *
100
 * @param string $message If no message is given 'Unauthorized' will be the message
101
 * @param int $code Status code, defaults to 401
102
 */
103
        public function __construct($message = null, $code = 401) {
104
                if (empty($message)) {
105
                        $message = 'Unauthorized';
106
                }
107
                parent::__construct($message, $code);
108
        }
109

    
110
}
111

    
112
/**
113
 * Represents an HTTP 403 error.
114
 *
115
 * @package       Cake.Error
116
 */
117
class ForbiddenException extends HttpException {
118

    
119
/**
120
 * Constructor
121
 *
122
 * @param string $message If no message is given 'Forbidden' will be the message
123
 * @param int $code Status code, defaults to 403
124
 */
125
        public function __construct($message = null, $code = 403) {
126
                if (empty($message)) {
127
                        $message = 'Forbidden';
128
                }
129
                parent::__construct($message, $code);
130
        }
131

    
132
}
133

    
134
/**
135
 * Represents an HTTP 404 error.
136
 *
137
 * @package       Cake.Error
138
 */
139
class NotFoundException extends HttpException {
140

    
141
/**
142
 * Constructor
143
 *
144
 * @param string $message If no message is given 'Not Found' will be the message
145
 * @param int $code Status code, defaults to 404
146
 */
147
        public function __construct($message = null, $code = 404) {
148
                if (empty($message)) {
149
                        $message = 'Not Found';
150
                }
151
                parent::__construct($message, $code);
152
        }
153

    
154
}
155

    
156
/**
157
 * Represents an HTTP 405 error.
158
 *
159
 * @package       Cake.Error
160
 */
161
class MethodNotAllowedException extends HttpException {
162

    
163
/**
164
 * Constructor
165
 *
166
 * @param string $message If no message is given 'Method Not Allowed' will be the message
167
 * @param int $code Status code, defaults to 405
168
 */
169
        public function __construct($message = null, $code = 405) {
170
                if (empty($message)) {
171
                        $message = 'Method Not Allowed';
172
                }
173
                parent::__construct($message, $code);
174
        }
175

    
176
}
177

    
178
/**
179
 * Represents an HTTP 500 error.
180
 *
181
 * @package       Cake.Error
182
 */
183
class InternalErrorException extends HttpException {
184

    
185
/**
186
 * Constructor
187
 *
188
 * @param string $message If no message is given 'Internal Server Error' will be the message
189
 * @param int $code Status code, defaults to 500
190
 */
191
        public function __construct($message = null, $code = 500) {
192
                if (empty($message)) {
193
                        $message = 'Internal Server Error';
194
                }
195
                parent::__construct($message, $code);
196
        }
197

    
198
}
199

    
200
/**
201
 * CakeException is used a base class for CakePHP's internal exceptions.
202
 * In general framework errors are interpreted as 500 code errors.
203
 *
204
 * @package       Cake.Error
205
 */
206
class CakeException extends CakeBaseException {
207

    
208
/**
209
 * Array of attributes that are passed in from the constructor, and
210
 * made available in the view when a development error is displayed.
211
 *
212
 * @var array
213
 */
214
        protected $_attributes = array();
215

    
216
/**
217
 * Template string that has attributes sprintf()'ed into it.
218
 *
219
 * @var string
220
 */
221
        protected $_messageTemplate = '';
222

    
223
/**
224
 * Constructor.
225
 *
226
 * Allows you to create exceptions that are treated as framework errors and disabled
227
 * when debug = 0.
228
 *
229
 * @param string|array $message Either the string of the error message, or an array of attributes
230
 *   that are made available in the view, and sprintf()'d into CakeException::$_messageTemplate
231
 * @param int $code The code of the error, is also the HTTP status code for the error.
232
 */
233
        public function __construct($message, $code = 500) {
234
                if (is_array($message)) {
235
                        $this->_attributes = $message;
236
                        $message = __d('cake_dev', $this->_messageTemplate, $message);
237
                }
238
                parent::__construct($message, $code);
239
        }
240

    
241
/**
242
 * Get the passed in attributes
243
 *
244
 * @return array
245
 */
246
        public function getAttributes() {
247
                return $this->_attributes;
248
        }
249

    
250
}
251

    
252
/**
253
 * Missing Controller exception - used when a controller
254
 * cannot be found.
255
 *
256
 * @package       Cake.Error
257
 */
258
class MissingControllerException extends CakeException {
259

    
260
        protected $_messageTemplate = 'Controller class %s could not be found.';
261

    
262
//@codingStandardsIgnoreStart
263
        public function __construct($message, $code = 404) {
264
                parent::__construct($message, $code);
265
        }
266
//@codingStandardsIgnoreEnd
267

    
268
}
269

    
270
/**
271
 * Missing Action exception - used when a controller action
272
 * cannot be found.
273
 *
274
 * @package       Cake.Error
275
 */
276
class MissingActionException extends CakeException {
277

    
278
        protected $_messageTemplate = 'Action %s::%s() could not be found.';
279

    
280
//@codingStandardsIgnoreStart
281
        public function __construct($message, $code = 404) {
282
                parent::__construct($message, $code);
283
        }
284
//@codingStandardsIgnoreEnd
285

    
286
}
287

    
288
/**
289
 * Private Action exception - used when a controller action
290
 * starts with a  `_`.
291
 *
292
 * @package       Cake.Error
293
 */
294
class PrivateActionException extends CakeException {
295

    
296
        protected $_messageTemplate = 'Private Action %s::%s() is not directly accessible.';
297

    
298
//@codingStandardsIgnoreStart
299
        public function __construct($message, $code = 404, Exception $previous = null) {
300
                parent::__construct($message, $code, $previous);
301
        }
302
//@codingStandardsIgnoreEnd
303

    
304
}
305

    
306
/**
307
 * Used when a component cannot be found.
308
 *
309
 * @package       Cake.Error
310
 */
311
class MissingComponentException extends CakeException {
312

    
313
        protected $_messageTemplate = 'Component class %s could not be found.';
314

    
315
}
316

    
317
/**
318
 * Used when a behavior cannot be found.
319
 *
320
 * @package       Cake.Error
321
 */
322
class MissingBehaviorException extends CakeException {
323

    
324
        protected $_messageTemplate = 'Behavior class %s could not be found.';
325

    
326
}
327

    
328
/**
329
 * Used when a view file cannot be found.
330
 *
331
 * @package       Cake.Error
332
 */
333
class MissingViewException extends CakeException {
334

    
335
        protected $_messageTemplate = 'View file "%s" is missing.';
336

    
337
}
338

    
339
/**
340
 * Used when a layout file cannot be found.
341
 *
342
 * @package       Cake.Error
343
 */
344
class MissingLayoutException extends CakeException {
345

    
346
        protected $_messageTemplate = 'Layout file "%s" is missing.';
347

    
348
}
349

    
350
/**
351
 * Used when a helper cannot be found.
352
 *
353
 * @package       Cake.Error
354
 */
355
class MissingHelperException extends CakeException {
356

    
357
        protected $_messageTemplate = 'Helper class %s could not be found.';
358

    
359
}
360

    
361
/**
362
 * Runtime Exceptions for ConnectionManager
363
 *
364
 * @package       Cake.Error
365
 */
366
class MissingDatabaseException extends CakeException {
367

    
368
        protected $_messageTemplate = 'Database connection "%s" could not be found.';
369

    
370
}
371

    
372
/**
373
 * Used when no connections can be found.
374
 *
375
 * @package       Cake.Error
376
 */
377
class MissingConnectionException extends CakeException {
378

    
379
        protected $_messageTemplate = 'Database connection "%s" is missing, or could not be created.';
380

    
381
/**
382
 * Constructor
383
 *
384
 * @param string|array $message The error message.
385
 * @param int $code The error code.
386
 */
387
        public function __construct($message, $code = 500) {
388
                if (is_array($message)) {
389
                        $message += array('enabled' => true);
390
                }
391
                parent::__construct($message, $code);
392
        }
393

    
394
}
395

    
396
/**
397
 * Used when a Task cannot be found.
398
 *
399
 * @package       Cake.Error
400
 */
401
class MissingTaskException extends CakeException {
402

    
403
        protected $_messageTemplate = 'Task class %s could not be found.';
404

    
405
}
406

    
407
/**
408
 * Used when a shell method cannot be found.
409
 *
410
 * @package       Cake.Error
411
 */
412
class MissingShellMethodException extends CakeException {
413

    
414
        protected $_messageTemplate = "Unknown command %1\$s %2\$s.\nFor usage try `cake %1\$s --help`";
415

    
416
}
417

    
418
/**
419
 * Used when a shell cannot be found.
420
 *
421
 * @package       Cake.Error
422
 */
423
class MissingShellException extends CakeException {
424

    
425
        protected $_messageTemplate = 'Shell class %s could not be found.';
426

    
427
}
428

    
429
/**
430
 * Exception class to be thrown when a datasource configuration is not found
431
 *
432
 * @package       Cake.Error
433
 */
434
class MissingDatasourceConfigException extends CakeException {
435

    
436
        protected $_messageTemplate = 'The datasource configuration "%s" was not found in database.php';
437

    
438
}
439

    
440
/**
441
 * Used when a datasource cannot be found.
442
 *
443
 * @package       Cake.Error
444
 */
445
class MissingDatasourceException extends CakeException {
446

    
447
        protected $_messageTemplate = 'Datasource class %s could not be found. %s';
448

    
449
}
450

    
451
/**
452
 * Exception class to be thrown when a database table is not found in the datasource
453
 *
454
 * @package       Cake.Error
455
 */
456
class MissingTableException extends CakeException {
457

    
458
        protected $_messageTemplate = 'Table %s for model %s was not found in datasource %s.';
459

    
460
}
461

    
462
/**
463
 * Exception raised when a Model could not be found.
464
 *
465
 * @package       Cake.Error
466
 */
467
class MissingModelException extends CakeException {
468

    
469
        protected $_messageTemplate = 'Model %s could not be found.';
470

    
471
}
472

    
473
/**
474
 * Exception raised when a test loader could not be found
475
 *
476
 * @package       Cake.Error
477
 */
478
class MissingTestLoaderException extends CakeException {
479

    
480
        protected $_messageTemplate = 'Test loader %s could not be found.';
481

    
482
}
483

    
484
/**
485
 * Exception raised when a plugin could not be found
486
 *
487
 * @package       Cake.Error
488
 */
489
class MissingPluginException extends CakeException {
490

    
491
        protected $_messageTemplate = 'Plugin %s could not be found.';
492

    
493
}
494

    
495
/**
496
 * Exception raised when a Dispatcher filter could not be found
497
 *
498
 * @package       Cake.Error
499
 */
500
class MissingDispatcherFilterException extends CakeException {
501

    
502
        protected $_messageTemplate = 'Dispatcher filter %s could not be found.';
503

    
504
}
505

    
506
/**
507
 * Exception class for AclComponent and Interface implementations.
508
 *
509
 * @package       Cake.Error
510
 */
511
class AclException extends CakeException {
512
}
513

    
514
/**
515
 * Exception class for Cache. This exception will be thrown from Cache when it
516
 * encounters an error.
517
 *
518
 * @package       Cake.Error
519
 */
520
class CacheException extends CakeException {
521
}
522

    
523
/**
524
 * Exception class for Router. This exception will be thrown from Router when it
525
 * encounters an error.
526
 *
527
 * @package       Cake.Error
528
 */
529
class RouterException extends CakeException {
530
}
531

    
532
/**
533
 * Exception class for CakeLog. This exception will be thrown from CakeLog when it
534
 * encounters an error.
535
 *
536
 * @package       Cake.Error
537
 */
538
class CakeLogException extends CakeException {
539
}
540

    
541
/**
542
 * Exception class for CakeSession. This exception will be thrown from CakeSession when it
543
 * encounters an error.
544
 *
545
 * @package       Cake.Error
546
 */
547
class CakeSessionException extends CakeException {
548
}
549

    
550
/**
551
 * Exception class for Configure. This exception will be thrown from Configure when it
552
 * encounters an error.
553
 *
554
 * @package       Cake.Error
555
 */
556
class ConfigureException extends CakeException {
557
}
558

    
559
/**
560
 * Exception class for Socket. This exception will be thrown from CakeSocket, CakeEmail, HttpSocket
561
 * SmtpTransport, MailTransport and HttpResponse when it encounters an error.
562
 *
563
 * @package       Cake.Error
564
 */
565
class SocketException extends CakeException {
566
}
567

    
568
/**
569
 * Exception class for Xml. This exception will be thrown from Xml when it
570
 * encounters an error.
571
 *
572
 * @package       Cake.Error
573
 */
574
class XmlException extends CakeException {
575
}
576

    
577
/**
578
 * Exception class for Console libraries. This exception will be thrown from Console library
579
 * classes when they encounter an error.
580
 *
581
 * @package       Cake.Error
582
 */
583
class ConsoleException extends CakeException {
584
}
585

    
586
/**
587
 * Represents a fatal error
588
 *
589
 * @package       Cake.Error
590
 */
591
class FatalErrorException extends CakeException {
592

    
593
/**
594
 * Constructor
595
 *
596
 * @param string $message The error message.
597
 * @param int $code The error code.
598
 * @param string $file The file the error occurred in.
599
 * @param int $line The line the error occurred on.
600
 */
601
        public function __construct($message, $code = 500, $file = null, $line = null) {
602
                parent::__construct($message, $code);
603
                if ($file) {
604
                        $this->file = $file;
605
                }
606
                if ($line) {
607
                        $this->line = $line;
608
                }
609
        }
610

    
611
}
612

    
613
/**
614
 * Not Implemented Exception - used when an API method is not implemented
615
 *
616
 * @package       Cake.Error
617
 */
618
class NotImplementedException extends CakeException {
619

    
620
        protected $_messageTemplate = '%s is not implemented.';
621

    
622
//@codingStandardsIgnoreStart
623
        public function __construct($message, $code = 501) {
624
                parent::__construct($message, $code);
625
        }
626
//@codingStandardsIgnoreEnd
627

    
628
}