-
Notifications
You must be signed in to change notification settings - Fork 0
API: The Exceptable Interface
An augmented interface for php exceptions. Exceptable extends the Throwable interface with a severity rating, various utility methods, and a specialized (but backwards compatible) constructor.
cautions on implementation:
- the implementing class must extend from a
Throwableclass (e.g.,Exception). - implementations cannot extend from
PDOException, because it breaks theThrowableinterface (itsgetCode()returns a string).
-
ERROR
An Exceptable of high severity, comparable toE_ERRORorE_USER_ERROR. -
WARNING
An Exceptable of medium severity, comparable toE_WARNINGorE_USER_WARNING. -
NOTICE
An Exceptable of low severity, comparable toE_NOTICEorE_USER_NOTICE. -
DEFAULT_CODE
If no code is provided to an Exceptable, it defaults to0. -
DEFAULT_MESSAGE
If no message is provided to an Exceptable, it defaults to""(empty string). -
DEFAULT_SEVERITY
If no severity is provided to an Exceptable, it defaults toExceptable::ERROR.
Gets information about an error case identified by the given code.
- parameters:
- int
$code
The exceptable code to look up
- int
- throws:
-
at\exceptable\ExceptableException
If the code is not known to the implementation
-
at\exceptable\ExceptableException
- returns: array
A map of info about the code, including (at a minimum) its "code", "severity", and "message"
Checks whether there is an error case associated with the given code.
- parameters:
- int
$codeThe exceptable code to look up
- int
- returns: bool
True if the code identifies a known error case; false otherwise
The exceptable constructor.
Constructor arguments are the same as those on the Throwable interface — $message, $code, and $previous — with an additional argument $context which accepts an array of values you provide (typically, details for the exception message).
Constructor arguments are all optional; any or all may be omitted (but their order must be preserved; e.g., $previous may not appear before $message).
- parameters:
- string
$message
The Exceptable message. If omitted, a default message will be set based on the Exceptable code. - int
$code
The Exceptable code. If omitted, a default code (defined by the implementing class) will be set. - Throwable
$previous
The previous Exception, if any. - array
$context
Contextual information about the Exceptable. Typically, used to provide details for the Exceptable message, but may include any information that should be available later (e.g., to loggers).
- string
- throws:
-
at\exceptable\ExceptableException
If constructor arguments are invalid and/or out of order.
-
at\exceptable\ExceptableException
Adds contextual information to the Exceptable. This will be added to any information passed to the constructor.
- parameters:
- array
$context
Key → value map of information to add. User-provided keys should not begin with a double underscore (__) as these are reserved for internal use.
- array
- returns: at\exceptable\Exceptable
The Exceptable instance.
Gets contextual information about this Exceptable.
- returns: array
A map of contextual information about the Exceptable. Keys will vary depending on what information was provided at runtime. At a minimum, will include "__severity__" (the Exceptable's severity) and "__rootMessage__" (if a previous exception exists).
Traverses the chain of previous exception(s) and gets the root exception.
- returns: Throwable
The root exception (which will be the Exceptable instance if no previous exception exists).
Gets the Exceptable severity.
- returns: int
One ofExceptable::ERROR,Exceptable::WARNING, orExceptable::NOTICE.
Checks the Exceptable severity.
- returns: bool
True if the Exceptable severity isExceptable::ERROR; false otherwise.
Checks the Exceptable severity.
- returns: bool
True if the Exceptable severity isExceptable::WARNING; false otherwise.
Checks the Exceptable severity.
- returns: bool
True if the Exceptable severity isExceptable::NOTICE; false otherwise.
Sets the Exceptable severity.
- params:
- int
$severity
The severity to set. Must be one ofExceptable::ERROR,Exceptable::WARNING, orExceptable::NOTICE.
- int
- throws:
-
ExceptableException
If the given severity is invalid.
-
ExceptableException
- returns: at\exceptable\Exceptable
The Exceptable instance.