GeneralException.php
1.43 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* Implements CAS server exception classes for the WP CAS Server plugin.
*
* @version 1.1.0
*/
namespace Cassava\Exception;
/**
* Base CAS server exception.
*
* @version 1.1.0
* @since 1.1.0
*/
class GeneralException extends \Exception {
/**
* Internal Error
*/
const ERROR_INTERNAL_ERROR = 'INTERNAL_ERROR';
/**
* Error code.
* @var string
*/
protected $casCode;
/**
* Exception constructor.
*
* @param string $message Exception message.
* @param string $casCode CAS error code (default: "INTERNAL_ERROR").
*/
public function __construct( $message = '', $casCode = self::ERROR_INTERNAL_ERROR ) {
parent::__construct( $message );
$this->casCode = $casCode;
}
/**
* Generate a new exception instance from a WordPress error.
*
* @param \WP_Error $error WordPress error.
* @return GeneralException WordPress error as an exception.
*/
public static function fromError( \WP_Error $error ) {
$code = $error->get_error_code();
$message = $error->get_error_message( $code );
return new static ($message, $code);
}
/**
* Error code getter.
*
* @return string Associated error code.
*/
final public function getCASCode() {
return $this->casCode;
}
/**
* Returns a WordPress error object based on the exception.
*
* @return \WP_Error WordPress error.
*/
public function getErrorInstance() {
return new \WP_Error( $this->casCode, $this->message );
}
}