Skip to content

Commit d739ee4

Browse files
restructure constructor arguments
remove addContext(), setSeverity() refactor to use traits fix tests
1 parent 60cff25 commit d739ee4

File tree

6 files changed

+233
-450
lines changed

6 files changed

+233
-450
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _Exceptables_ make exceptions exceptional. Exceptables provide some nice utilit
88
dependencies
99
------------
1010

11-
Requires php 7.0 or later.
11+
Requires php 7.1 or later.
1212

1313
installation
1414
------------
@@ -56,6 +56,9 @@ see more in [the wiki](https://github.com/php-enspired/exceptable/wiki).
5656
docs
5757
----
5858

59+
**Note**, version 2.0 requires PHP 7.1 or greater, and also introduces a number of changes from the version 1 api.
60+
[Read more](https://github.com/php-enspired/exceptable/wiki/new-in-2.0).
61+
5962
- API:
6063
- [The Exceptable Interface](https://github.com/php-enspired/exceptable/wiki/API:-The-Exceptable-Interface)
6164
- [The Exception Class](https://github.com/php-enspired/exceptable/wiki/API:-The-Exception-Class)

src/Exceptable.php

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @package at.exceptable
44
* @author Adrian <adrian@enspi.red>
5-
* @copyright 2014 - 2016
5+
* @copyright 2014 - 2018
66
* @license GPL-3.0 (only)
77
*
88
* This program is free software: you can redistribute it and/or modify it
@@ -32,17 +32,8 @@
3232
* exceptable::getSeverity() will need to be aliased when the trait is used.
3333
* - implementations cannot extend from PDOException,
3434
* because it breaks the Throwable interface (its getCode() returns a string).
35-
*
36-
* @method string Throwable::__toString( void )
37-
* @method int Throwable::getCode( void )
38-
* @method string Throwable::getFile( void )
39-
* @method int Throwable::getLine( void )
40-
* @method string Throwable::getMessage( void )
41-
* @method Throwable Throwable::getPrevious( void )
42-
* @method array Throwable::getTrace( void )
43-
* @method string Throwable::getTraceAsString( void )
4435
*/
45-
interface Exceptable extends \Throwable {
36+
interface Exceptable extends Throwable {
4637

4738
/**
4839
* exception severity levels.
@@ -55,53 +46,30 @@ interface Exceptable extends \Throwable {
5546
const WARNING = E_WARNING;
5647
const NOTICE = E_NOTICE;
5748

58-
/**
59-
* default info for unknown/generic exception cases.
60-
*
61-
* @type int DEFAULT_CODE
62-
* @type int DEFAULT_MESSAGE
63-
* @type int DEFAULT_SEVERITY
64-
*/
65-
const DEFAULT_CODE = 0;
66-
const DEFAULT_MESSAGE = '';
67-
const DEFAULT_SEVERITY = self::ERROR;
68-
6949
/**
7050
* gets information about a code known to the implementing class.
7151
*
72-
* @param int $code the exception code to look up
73-
* @throws ExceptableException if the code is not known to the implementation
74-
* @return array a map of info about the code,
75-
* including (at a minimum) its "code", "severity", and "message".
52+
* @param int $code the exception code to look up
53+
* @throws Exceptable if the code is not known to the implementation
54+
* @return array a map of info about the error condition
7655
*/
77-
public static function get_info(int $code) : array;
56+
public static function getInfo(int $code) : array;
7857

7958
/**
8059
* checks whether the implementation has info about the given code.
8160
*
8261
* @param int $code the code to check
8362
* @return bool true if the code is known; false otherwise
8463
*/
85-
public static function has_info(int $code) : bool;
64+
public static function hasInfo(int $code) : bool;
8665

8766
/**
88-
* @param string $0 exception message
89-
* if omitted, a message must be set based on the exception code
90-
* @param int $1 exception code
91-
* if omitted, a default code must be set
92-
* @param Throwable $2 previous exception
93-
* @param array $3 additional exception context
94-
* @throws ExceptableException if argument(s) are invalid
67+
* @param int $code exception code
68+
* @param array $context additional exception context
69+
* @param Throwable $previous previous exception
70+
* @throws ExceptableException if code is invalid
9571
*/
96-
public function __construct(...$args);
97-
98-
/**
99-
* adds contextual info to this exception.
100-
*
101-
* @param array $context map of info to add
102-
* @return $this
103-
*/
104-
public function addContext(array $context) : Exceptable;
72+
public function __construct(int $code, array $context = [], Throwable $previous = null);
10573

10674
/**
10775
* gets contextual info about this exception.
@@ -115,42 +83,12 @@ public function getContext() : array;
11583
*
11684
* @return Throwable the root exception
11785
*/
118-
public function getRoot() : \Throwable;
86+
public function getRoot() : Throwable;
11987

12088
/**
12189
* gets exception severity.
12290
*
12391
* @return int the exception severity
12492
*/
12593
public function getSeverity() : int;
126-
127-
/**
128-
* checks the exception severity.
129-
*
130-
* @return bool true if exception severity is "Error"; false otherwise
131-
*/
132-
public function isError() : bool;
133-
134-
/**
135-
* checks the exception severity.
136-
*
137-
* @return bool true if exception severity is "Warning"; false otherwise
138-
*/
139-
public function isWarning() : bool;
140-
141-
/**
142-
* checks the exception severity.
143-
*
144-
* @return bool true if exception severity is "Notice"; false otherwise
145-
*/
146-
public function isNotice() : bool;
147-
148-
/**
149-
* adds contextual info to this exception.
150-
*
151-
* @param int $severity one of Exceptable::ERROR|WARNING|NOTICE
152-
* @throws ExceptableException if severity is invalid
153-
* @return $this
154-
*/
155-
public function setSeverity(int $severity) : Exceptable;
15694
}

src/ExceptableException.php

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,24 @@
2020

2121
namespace at\exceptable;
2222

23-
use at\exceptable\Exceptable,
24-
at\exceptable\Exception;
23+
use at\exceptable\ {
24+
Exceptable,
25+
Exception
26+
};
2527

2628
/**
2729
* exceptableexceptionsexceptableexceptionsexceptableexceptions
28-
*
29-
* @method int \Exception::getCode( void )
30-
* @method string \Exception::getFile( void )
31-
* @method int \Exception::getLine( void )
32-
* @method string \Exception::getMessage( void )
33-
* @method Throwable \Exception::getPrevious( void )
34-
* @method array \Exception::getTrace( void )
35-
* @method string \Exception::getTraceAsString( void )
36-
*
37-
* @method array Exception::get_info( int $code )
38-
* @method bool Exception::has_info( int $code )
39-
* @method void Exception::__construct( string|int|Throwable|array …$args )
40-
* @method string Exception::__toString( void )
41-
* @method Exceptable Exception::addContext( array $context )
42-
* @method array Exception::getContext( void )
43-
* @method Throwable Exception::getRoot( void )
44-
* @method int Exception::getSeverity( void )
45-
* @method bool Exception::isError( void )
46-
* @method bool Exception::isWarning( void )
47-
* @method bool Exception::isNotice( void )
48-
* @method Exceptable Exception::setSeverity( int $severity )
4930
*/
5031
class ExceptableException extends Exception {
5132

5233
/**
53-
* @type int NO_SUCH_CODE invalid exception code.
54-
* @type int INVALID_CONSTRUCT_ARGS invalid/out-of-order constructor arguments.
55-
* @type int INVALID_SEVERITY invalid severity level.
56-
* @type int UNCAUGHT_EXCEPTION uncaught/unhandled exception during runtime.
57-
* @type int INVALID_HANDLER invalid handler (e.g., wrong signature, or throws).
34+
* @type int NO_SUCH_CODE invalid exception code
35+
* @type int UNCAUGHT_EXCEPTION uncaught/unhandled exception during runtime
36+
* @type int INVALID_HANDLER invalid handler (e.g., wrong signature, or throws)
5837
*/
5938
const NO_SUCH_CODE = 1;
60-
const INVALID_CONSTRUCT_ARGS = (1<<1);
61-
const INVALID_SEVERITY = (1<<2);
62-
const UNCAUGHT_EXCEPTION = (1<<3);
63-
const INVALID_HANDLER = (1<<4);
39+
const UNCAUGHT_EXCEPTION = 2;
40+
const INVALID_HANDLER = 3;
6441

6542
/** @see Exceptable::INFO */
6643
const INFO = [
@@ -69,17 +46,6 @@ class ExceptableException extends Exception {
6946
'severity' => Exceptable::WARNING,
7047
'tr_message' => "no exception code '{code}' is known"
7148
],
72-
self::INVALID_CONSTRUCT_ARGS => [
73-
'message' => 'constructor arguments are invalid and/or out of order',
74-
'severity' => Exceptable::ERROR,
75-
'tr_message' => "constructor arguments are invalid and/or out of order: {args}"
76-
],
77-
self::INVALID_SEVERITY => [
78-
'message' => 'invalid severity',
79-
'severity' => Exceptable::WARNING,
80-
'tr_message' =>
81-
'severity must be one of Exceptable::ERROR|WARNING|NOTICE; {severity} provided'
82-
],
8349
self::UNCAUGHT_EXCEPTION => [
8450
'message' => 'uncaught exception',
8551
'severity' => Exceptable::ERROR,

0 commit comments

Comments
 (0)