-
Notifications
You must be signed in to change notification settings - Fork 35
/
custom-exception.php
53 lines (44 loc) · 1.46 KB
/
custom-exception.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
class CustomException extends Exception {
public function __construct($e) {
// make sure everything is assigned properly
parent::__construct($e->getMessage(), $e->getCode());
// log what we know
$msg = "------------------------------------------------\n";
$msg .= __CLASS__ . ": [{$this->code}]: {$this->message}\n";
$msg .= $e->getTraceAsString() . "\n";
error_log($msg);
}
// overload the __toString() method to suppress any "normal" output
public function __toString() {
return $this->printMessage();
}
// map error codes to output messages or templates
public function printMessage() {
$usermsg = '';
$code = $this->getCode();
switch ($code) {
case SOME_DEFINED_ERROR_CODE:
$usermsg = 'Ooops! Sorry about that.';
break;
case OTHER_DEFINED_ERROR_CODE:
$usermsg = "Drat!";
break;
default:
$usermsg = file_get_contents('/templates/general_error.html');
break;
}
return $usermsg;
}
// static exception_handler for default exception handling
public static function exception_handler($exception) {
throw new CustomException($exception);
}
}
// make sure to catch every exception
set_exception_handler('CustomException::exception_handler');
try {
$obj = new CoolThirdPartyPackage();
} catch (CustomException $e) {
echo $e;
}