Skip to content

Commit 853d880

Browse files
committed
Added possibility to declare err code
1 parent 3a3243c commit 853d880

File tree

9 files changed

+56
-83
lines changed

9 files changed

+56
-83
lines changed

src/Throwable/Error.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ class Error extends \Error
66
{
77
/**
88
* @param string $message
9+
* @param int $code
910
* @param \Error|\Exception|null $previous
1011
*/
11-
public function __construct($message = 'Unknown error', $previous = null)
12+
public function __construct($message = 'Unknown error', $code = 0, $previous = null)
1213
{
13-
parent::__construct($message, 0, $previous);
14+
parent::__construct($message, $code, $previous);
1415
}
1516

1617
/**

src/Throwable/Exception.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ class Exception extends \Exception
66
{
77
/**
88
* @param string $message
9+
* @param int $code
910
* @param \Error|\Exception|null $previous
1011
*/
11-
public function __construct($message = 'Unknown exception', $previous = null)
12+
public function __construct($message = 'Unknown exception', $code = 0, $previous = null)
1213
{
13-
parent::__construct($message, 0, $previous);
14+
parent::__construct($message, $code, $previous);
1415
}
1516

1617
/**

src/Throwable/ThrowableProxy.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ public function toThrowable()
8989
$class = $this->class;
9090
$prev = $this->prev !== null ? $this->prev->toThrowable() : $this->prev;
9191

92-
return preg_match('#^(\\\?)Dazzle#si', $class)
93-
? new $class($this->message, $prev)
94-
: new $class($this->message, 0, $prev);
92+
return new $class($this->message, 0, $prev);
9593
}
9694

9795
/**

test/Callback.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Dazzle\Throwable\Test;
4+
5+
class Callback
6+
{
7+
public function __invoke()
8+
{}
9+
}

test/TUnit.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -103,46 +103,6 @@ public function createCallableMock()
103103
return $this->getMock(Callback::class);
104104
}
105105

106-
/**
107-
* @return LoopInterface|\PHPUnit_Framework_MockObject_MockObject
108-
*/
109-
public function createLoopMock()
110-
{
111-
return $this->getMock('Dazzle\Loop\LoopInterface');
112-
}
113-
114-
/**
115-
* @return LoopInterface|\PHPUnit_Framework_MockObject_MockObject
116-
*/
117-
public function createWritableLoopMock()
118-
{
119-
$loop = $this->createLoopMock();
120-
$loop
121-
->expects($this->once())
122-
->method('addWriteStream')
123-
->will($this->returnCallback(function($stream, $listener) {
124-
call_user_func($listener, $stream);
125-
}));
126-
127-
return $loop;
128-
}
129-
130-
/**
131-
* @return LoopInterface|\PHPUnit_Framework_MockObject_MockObject
132-
*/
133-
public function createReadableLoopMock()
134-
{
135-
$loop = $this->createLoopMock();
136-
$loop
137-
->expects($this->once())
138-
->method('addReadStream')
139-
->will($this->returnCallback(function($stream, $listener) {
140-
call_user_func($listener, $stream);
141-
}));
142-
143-
return $loop;
144-
}
145-
146106
/**
147107
* Check if protected property exists.
148108
*

test/TUnit/ErrorTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testApiConstructor_CreatesInstance()
2424
public function testApiConstructor_ChainsErrors()
2525
{
2626
$previous = $this->createError('Previous');
27-
$ex = $this->createError('Error', $previous);
27+
$ex = $this->createError('Error', 0, $previous);
2828

2929
$this->assertSame($previous, $ex->getPrevious());
3030
}
@@ -44,7 +44,7 @@ public function testApiDestructor_DoesNotThrowError()
4444
public function testApiToString_ReturnsErrorStack()
4545
{
4646
$prev = $this->createError('Previous');
47-
$ex = $this->createError('Error', $prev);
47+
$ex = $this->createError('Error', 0, $prev);
4848

4949
$this->assertString((string) $ex);
5050
}
@@ -55,7 +55,7 @@ public function testApiToString_ReturnsErrorStack()
5555
public function testStaticApiToString_ReturnsErrorStack()
5656
{
5757
$prev = $this->createError('Previous');
58-
$ex = $this->createError('Error', $prev);
58+
$ex = $this->createError('Error', 0, $prev);
5959

6060
$this->assertSame((string) $ex, Error::toString($ex));
6161
}
@@ -66,7 +66,7 @@ public function testStaticApiToString_ReturnsErrorStack()
6666
public function testStaticApiToTrace_ReturnsTrace()
6767
{
6868
$prev = $this->createError('Previous');
69-
$ex = $this->createError('Error', $prev);
69+
$ex = $this->createError('Error', 0, $prev);
7070

7171
$this->assertTrace(Error::toTrace($ex));
7272
}
@@ -77,7 +77,7 @@ public function testStaticApiToTrace_ReturnsTrace()
7777
public function testStaticApiToStackTrace_ReturnsStackTrace()
7878
{
7979
$prev = $this->createError('Previous');
80-
$ex = $this->createError('Error', $prev);
80+
$ex = $this->createError('Error', 0, $prev);
8181

8282
$this->assertStackTrace(Error::toStackTrace($ex));
8383
}
@@ -88,7 +88,7 @@ public function testStaticApiToStackTrace_ReturnsStackTrace()
8888
public function testStaticApiToThrowableTrace_ReturnsStackThrowable()
8989
{
9090
$prev = $this->createError('Previous');
91-
$ex = $this->createError('Error', $prev);
91+
$ex = $this->createError('Error', 0, $prev);
9292

9393
$this->assertThrowableTrace(Error::toThrowableTrace($ex));
9494
}
@@ -99,7 +99,7 @@ public function testStaticApiToThrowableTrace_ReturnsStackThrowable()
9999
public function testStaticApiToStackString_ReturnsStackTraceAsString()
100100
{
101101
$prev = $this->createError('Previous');
102-
$ex = $this->createError('Error', $prev);
102+
$ex = $this->createError('Error', 0, $prev);
103103

104104
$this->assertStackString(Error::toStackString($ex));
105105
}
@@ -110,19 +110,20 @@ public function testStaticApiToStackString_ReturnsStackTraceAsString()
110110
public function testStaticApiToThrowableString_ReturnsThrowableTraceAsString()
111111
{
112112
$prev = $this->createError('Previous');
113-
$ex = $this->createError('Error', $prev);
113+
$ex = $this->createError('Error', 0, $prev);
114114

115115
$this->assertThrowableString(Error::toThrowableString($ex));
116116
}
117117

118118
/**
119119
* @param string $message
120+
* @param int $code
120121
* @param null $previous
121122
* @return Error
122123
*/
123-
public function createError($message, $previous = null)
124+
public function createError($message, $code = 0, $previous = null)
124125
{
125-
return new Error($message, $previous);
126+
return new Error($message, $code, $previous);
126127
}
127128

128129
/**

test/TUnit/ExceptionTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public function testApiConstructor_CreatesInstance()
2323
*/
2424
public function testApiConstructor_ChainsExceptions()
2525
{
26-
$previous = $this->createException('Previous');
27-
$ex = $this->createException('Exception', $previous);
26+
$prev = $this->createException('Previous');
27+
$ex = $this->createException('Exception', 0, $prev);
2828

29-
$this->assertSame($previous, $ex->getPrevious());
29+
$this->assertSame($prev, $ex->getPrevious());
3030
}
3131

3232
/**
@@ -44,7 +44,7 @@ public function testApiDestructor_DoesNotThrowException()
4444
public function testApiToString_ReturnsExceptionStack()
4545
{
4646
$prev = $this->createException('Previous');
47-
$ex = $this->createException('Exception', $prev);
47+
$ex = $this->createException('Exception', 0, $prev);
4848

4949
$this->assertString((string) $ex);
5050
}
@@ -55,7 +55,7 @@ public function testApiToString_ReturnsExceptionStack()
5555
public function testStaticApiToString_ReturnsExceptionStack()
5656
{
5757
$prev = $this->createException('Previous');
58-
$ex = $this->createException('Exception', $prev);
58+
$ex = $this->createException('Exception', 0, $prev);
5959

6060
$this->assertSame((string) $ex, Exception::toString($ex));
6161
}
@@ -66,7 +66,7 @@ public function testStaticApiToString_ReturnsExceptionStack()
6666
public function testStaticApiToTrace_ReturnsTrace()
6767
{
6868
$prev = $this->createException('Previous');
69-
$ex = $this->createException('Exception', $prev);
69+
$ex = $this->createException('Exception', 0, $prev);
7070

7171
$this->assertTrace(Exception::toTrace($ex));
7272
}
@@ -77,7 +77,7 @@ public function testStaticApiToTrace_ReturnsTrace()
7777
public function testStaticApiToStackTrace_ReturnsStackTrace()
7878
{
7979
$prev = $this->createException('Previous');
80-
$ex = $this->createException('Exception', $prev);
80+
$ex = $this->createException('Exception', 0, $prev);
8181

8282
$this->assertStackTrace(Exception::toStackTrace($ex));
8383
}
@@ -88,7 +88,7 @@ public function testStaticApiToStackTrace_ReturnsStackTrace()
8888
public function testStaticApiToThrowableTrace_ReturnsStackThrowable()
8989
{
9090
$prev = $this->createException('Previous');
91-
$ex = $this->createException('Exception', $prev);
91+
$ex = $this->createException('Exception', 0, $prev);
9292

9393
$this->assertThrowableTrace(Exception::toThrowableTrace($ex));
9494
}
@@ -99,7 +99,7 @@ public function testStaticApiToThrowableTrace_ReturnsStackThrowable()
9999
public function testStaticApiToStackString_ReturnsStackTraceAsString()
100100
{
101101
$prev = $this->createException('Previous');
102-
$ex = $this->createException('Exception', $prev);
102+
$ex = $this->createException('Exception', 0, $prev);
103103

104104
$this->assertStackString(Exception::toStackString($ex));
105105
}
@@ -110,19 +110,20 @@ public function testStaticApiToStackString_ReturnsStackTraceAsString()
110110
public function testStaticApiToThrowableString_ReturnsThrowableTraceAsString()
111111
{
112112
$prev = $this->createException('Previous');
113-
$ex = $this->createException('Exception', $prev);
113+
$ex = $this->createException('Exception', 0, $prev);
114114

115115
$this->assertThrowableString(Exception::toThrowableString($ex));
116116
}
117117

118118
/**
119119
* @param string $message
120+
* @param int $code
120121
* @param null $previous
121122
* @return Exception
122123
*/
123-
public function createException($message, $previous = null)
124+
public function createException($message, $code = 0, $previous = null)
124125
{
125-
return new Exception($message, $previous);
126+
return new Exception($message, $code, $previous);
126127
}
127128

128129
/**

test/TUnit/ThrowableProxyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function testApiConstructor_CreatesInstance_WithPreviousElementBeingError
9191
public function testApiConstructor_CreatesInstance_WithPreviousElementBeingFrameworkError()
9292
{
9393
$prev = new \Dazzle\Throwable\Error('Previous');
94-
$ex = new \Dazzle\Throwable\Error('Error', $prev);
94+
$ex = new \Dazzle\Throwable\Error('Error', 0, $prev);
9595

9696
$proxy = $this->createThrowableProxy($ex);
9797

@@ -110,7 +110,7 @@ public function testApiConstructor_CreatesInstance_WithPreviousElementBeingFrame
110110
public function testApiConstructor_CreatesInstance_WithPreviousElementBeingException()
111111
{
112112
$prev = new \Dazzle\Throwable\Exception('Previous');
113-
$ex = new \Dazzle\Throwable\Exception('Exception', $prev);
113+
$ex = new \Dazzle\Throwable\Exception('Exception', 0, $prev);
114114

115115
$proxy = $this->createThrowableProxy($ex);
116116

test/TUnit/ThrowableTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ThrowableTest extends TUnit
1515
public function testStaticApiParseThrowableMessage_ParsesThrowableMessage_ForError()
1616
{
1717
$prev = $this->createError('Previous');
18-
$ex = $this->createError($message = 'Exception', $prev);
18+
$ex = $this->createError($message = 'Exception', 0, $prev);
1919
$stack = Throwable::getThrowableStack($ex);
2020
$base = $this->callProtectedMethod(Throwable::class, 'getBasename', [ get_class($ex) ]);
2121

@@ -28,7 +28,7 @@ public function testStaticApiParseThrowableMessage_ParsesThrowableMessage_ForErr
2828
public function testStaticApiParseThrowableMessage_ParsesThrowableMessage_ForException()
2929
{
3030
$prev = $this->createException('Previous');
31-
$ex = $this->createException($message = 'Exception', $prev);
31+
$ex = $this->createException($message = 'Exception', 0, $prev);
3232
$stack = Throwable::getThrowableStack($ex);
3333
$base = $this->callProtectedMethod(Throwable::class, 'getBasename', [ get_class($ex) ]);
3434

@@ -41,7 +41,7 @@ public function testStaticApiParseThrowableMessage_ParsesThrowableMessage_ForExc
4141
public function testStaticApiGetThrowableStack_ReturnsStack_ForError()
4242
{
4343
$prev = $this->createError('Previous');
44-
$ex = $this->createError($message = 'Exception', $prev);
44+
$ex = $this->createError($message = 'Exception', 0, $prev);
4545

4646
$stack = Throwable::getThrowableStack($ex);
4747
$this->assertData($stack);
@@ -54,7 +54,7 @@ public function testStaticApiGetThrowableStack_ReturnsStack_ForError()
5454
public function testStaticApiGetThrowableStack_ReturnsStack_ForException()
5555
{
5656
$prev = $this->createException('Previous');
57-
$ex = $this->createException($message = 'Exception', $prev);
57+
$ex = $this->createException($message = 'Exception', 0, $prev);
5858

5959
$stack = Throwable::getThrowableStack($ex);
6060
$this->assertData($stack);
@@ -67,7 +67,7 @@ public function testStaticApiGetThrowableStack_ReturnsStack_ForException()
6767
public function testStaticApiGetThrowableData_ReturnsData_ForError()
6868
{
6969
$prev = $this->createError('Previous');
70-
$ex = $this->createError($message = 'Exception', $prev);
70+
$ex = $this->createError($message = 'Exception', 0, $prev);
7171

7272
$data = Throwable::getThrowableData($ex);
7373
$this->assertData($data);
@@ -79,7 +79,7 @@ public function testStaticApiGetThrowableData_ReturnsData_ForError()
7979
public function testStaticApiGetThrowableData_ReturnsData_ForException()
8080
{
8181
$prev = $this->createException('Previous');
82-
$ex = $this->createException($message = 'Exception', $prev);
82+
$ex = $this->createException($message = 'Exception', 0, $prev);
8383

8484
$data = Throwable::getThrowableData($ex);
8585
$this->assertData($data);
@@ -91,7 +91,7 @@ public function testStaticApiGetThrowableData_ReturnsData_ForException()
9191
public function testStaticApiGetTraceElements_ReturnsTraceElements_ForError()
9292
{
9393
$prev = $this->createError('Previous');
94-
$ex = $this->createError('Exception', $prev);
94+
$ex = $this->createError('Exception', 0, $prev);
9595

9696
$elements = $this->callProtectedMethod(Throwable::class, 'getTraceElements', [ $ex ]);
9797
$this->assertTrace($elements);
@@ -103,7 +103,7 @@ public function testStaticApiGetTraceElements_ReturnsTraceElements_ForError()
103103
public function testStaticApiGetTraceElements_ReturnsTraceElements_ForException()
104104
{
105105
$prev = $this->createException('Previous');
106-
$ex = $this->createException('Exception', $prev);
106+
$ex = $this->createException('Exception', 0, $prev);
107107

108108
$elements = $this->callProtectedMethod(Throwable::class, 'getTraceElements', [ $ex ]);
109109
$this->assertTrace($elements);
@@ -176,22 +176,24 @@ public function testStaticApiGetBasename_ReturnsThrowableBasename_ForException()
176176

177177
/**
178178
* @param string $message
179+
* @param int $code
179180
* @param \Error|\Exception|null $prev
180181
* @return Exception
181182
*/
182-
public function createException($message, $prev = null)
183+
public function createException($message, $code = 0, $prev = null)
183184
{
184-
return new Exception($message, $prev);
185+
return new Exception($message, $code, $prev);
185186
}
186187

187188
/**
188189
* @param string $message
190+
* @param int $code
189191
* @param \Error|\Exception|null $prev
190192
* @return Error
191193
*/
192-
public function createError($message, $prev = null)
194+
public function createError($message, $code = 0, $prev = null)
193195
{
194-
return new Error($message, $prev);
196+
return new Error($message, $code, $prev);
195197
}
196198

197199
/**

0 commit comments

Comments
 (0)