Skip to content

Commit c4e1bb6

Browse files
committed
Moved exception codes into constants at the according exception.
1 parent c640cdc commit c4e1bb6

16 files changed

+128
-67
lines changed

src/PEAR2/Net/RouterOS/Client.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function __construct(
135135
$this->com->close();
136136
throw new DataFlowException(
137137
'Invalid username or password supplied.',
138-
10000
138+
DataFlowException::CODE_INVALID_CREDENTIALS
139139
);
140140
}
141141
}
@@ -218,7 +218,7 @@ public static function login(
218218
|| $e instanceof UnexpectedValueException
219219
|| !$com->getTransmitter()->isDataAwaiting()) ? new SocketException(
220220
'This is not a compatible RouterOS service',
221-
10200,
221+
SocketException::CODE_SERVICE_INCOMPATIBLE,
222222
$e
223223
) : $e;
224224
}
@@ -333,19 +333,19 @@ public function sendAsync(Request $request, $callback = null)
333333
if ('' == $tag) {
334334
throw new DataFlowException(
335335
'Asynchonous commands must have a tag.',
336-
10500
336+
DataFlowException::CODE_TAG_REQUIRED
337337
);
338338
}
339339
if ($this->isRequestActive($tag)) {
340340
throw new DataFlowException(
341341
'There must not be multiple active requests sharing a tag.',
342-
10501
342+
DataFlowException::CODE_TAG_UNIQUE
343343
);
344344
}
345345
if (null !== $callback && !is_callable($callback, true)) {
346346
throw new UnexpectedValueException(
347347
'Invalid callback provided.',
348-
10502
348+
UnexpectedValueException::CODE_CALLBACK_INVALID
349349
);
350350
}
351351

@@ -489,7 +489,7 @@ public function extractNewResponses($tag = null)
489489
} else {
490490
throw new DataFlowException(
491491
'No such request, or the request has already finished.',
492-
10900
492+
DataFlowException::CODE_UNKNOWN_REQUEST
493493
);
494494
}
495495
}
@@ -613,7 +613,7 @@ public function cancelRequest($tag = null)
613613
} else {
614614
throw new DataFlowException(
615615
'No such request. Canceling aborted.',
616-
11200
616+
DataFlowException::CODE_CANCEL_FAIL
617617
);
618618
}
619619
}

src/PEAR2/Net/RouterOS/Communicator.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ public function __construct(
119119
$context
120120
);
121121
} catch (T\Exception $e) {
122-
throw new SocketException('Error connecting to RouterOS', 100, $e);
122+
throw new SocketException(
123+
'Error connecting to RouterOS',
124+
SocketException::CODE_CONNECTION_FAIL,
125+
$e
126+
);
123127
}
124128
$this->setCharset(
125129
self::getDefaultCharset(self::CHARSET_ALL),
@@ -369,7 +373,7 @@ public function sendWordFromStream($prefix, $stream)
369373
if (!self::isSeekableStream($stream)) {
370374
throw new InvalidArgumentException(
371375
'The stream must be seekable.',
372-
1100
376+
InvalidArgumentException::CODE_SEEKABLE_REQUIRED
373377
);
374378
}
375379
if (null !== ($remoteCharset = $this->getCharset(self::CHARSET_REMOTE))
@@ -421,7 +425,7 @@ protected static function verifyLengthSupport($length)
421425
if ($length > 0xFFFFFFF) {
422426
throw new LengthException(
423427
'Words with length above 0xFFFFFFF are not supported.',
424-
1200,
428+
LengthException::CODE_UNSUPPORTED,
425429
null,
426430
$length
427431
);
@@ -440,7 +444,7 @@ public static function encodeLength($length)
440444
if ($length < 0) {
441445
throw new LengthException(
442446
'Length must not be negative.',
443-
1300,
447+
LengthException::CODE_INVALID,
444448
null,
445449
$length
446450
);
@@ -462,7 +466,7 @@ public static function encodeLength($length)
462466
}
463467
throw new LengthException(
464468
'Length must not be above 0x7FFFFFFFF.',
465-
1301,
469+
LengthException::CODE_BEYOND_SHEME,
466470
null,
467471
$length
468472
);
@@ -601,7 +605,7 @@ private static function _decodeLength(T\Stream $trans)
601605
}
602606
throw new NotSupportedException(
603607
'Unknown control byte encountered.',
604-
1601,
608+
NotSupportedException::CODE_CONTROL_BYTE,
605609
null,
606610
$byte
607611
);

src/PEAR2/Net/RouterOS/DataFlowException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@
3131
*/
3232
class DataFlowException extends \RuntimeException implements Exception
3333
{
34+
const CODE_INVALID_CREDENTIALS = 10000;
35+
const CODE_TAG_REQUIRED = 10500;
36+
const CODE_TAG_UNIQUE = 10501;
37+
const CODE_UNKNOWN_REQUEST = 10900;
38+
const CODE_CANCEL_FAIL = 11200;
3439
}

src/PEAR2/Net/RouterOS/InvalidArgumentException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@
3232
class InvalidArgumentException extends \InvalidArgumentException
3333
implements Exception
3434
{
35+
const CODE_SEEKABLE_REQUIRED = 1100;
36+
const CODE_NAME_INVALID = 20100;
37+
const CODE_ABSOLUTE_REQUIRED = 40200;
38+
const CODE_CMD_UNRESOLVABLE = 40201;
39+
const CODE_CMD_INVALID = 40202;
40+
const CODE_NAME_UNPARSABLE = 41000;
41+
const CODE_VALUE_UNPARSABLE = 41001;
42+
3543
}

src/PEAR2/Net/RouterOS/LengthException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
*/
3232
class LengthException extends \LengthException implements Exception
3333
{
34+
35+
const CODE_UNSUPPORTED = 1200;
36+
const CODE_INVALID = 1300;
37+
const CODE_BEYOND_SHEME = 1301;
3438

3539
/**
3640
*

src/PEAR2/Net/RouterOS/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function sanitizeArgumentName($name)
7979
) {
8080
throw new InvalidArgumentException(
8181
'Invalid name of argument supplied.',
82-
20100
82+
InvalidArgumentException::CODE_NAME_INVALID
8383
);
8484
}
8585
return $name;

src/PEAR2/Net/RouterOS/NotSupportedException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
class NotSupportedException extends \Exception implements Exception
3434
{
3535

36+
const CODE_CONTROL_BYTE = 1601;
37+
3638
/**
3739
* @var mixed The unsuppported value.
3840
*/

src/PEAR2/Net/RouterOS/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected static function sanitizeAction($action)
9898
default:
9999
throw new UnexpectedValueException(
100100
'Unknown action specified',
101-
30100,
101+
UnexpectedValueException::CODE_ACTION_UNKNOWN,
102102
null,
103103
$action
104104
);
@@ -203,7 +203,7 @@ private function _send(Communicator $com)
203203
if (!$com->getTransmitter()->isAcceptingData()) {
204204
throw new SocketException(
205205
'Transmitter is invalid. Sending aborted.',
206-
30600
206+
SocketException::CODE_UNACCEPTING_QUERY
207207
);
208208
}
209209
$bytes = 0;

src/PEAR2/Net/RouterOS/Request.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function setCommand($command)
124124
if (strpos($command, '/') !== 0) {
125125
throw new InvalidArgumentException(
126126
'Commands must be absolute.',
127-
40200
127+
InvalidArgumentException::CODE_ABSOLUTE_REQUIRED
128128
);
129129
}
130130
if (substr_count($command, '/') === 1) {
@@ -137,7 +137,7 @@ public function setCommand($command)
137137
if ($delIndex < 1) {
138138
throw new InvalidArgumentException(
139139
'Unable to resolve command',
140-
40201
140+
InvalidArgumentException::CODE_CMD_UNRESOLVABLE
141141
);
142142
}
143143
unset($cmdRes[$delIndex]);
@@ -151,7 +151,7 @@ public function setCommand($command)
151151
if (!preg_match('#^/\S+$#sm', $command)) {
152152
throw new InvalidArgumentException(
153153
'Invalid command supplied.',
154-
40202
154+
InvalidArgumentException::CODE_CMD_INVALID
155155
);
156156
}
157157
$this->_command = $command;
@@ -285,7 +285,7 @@ private function _send(Communicator $com)
285285
if (!$com->getTransmitter()->isAcceptingData()) {
286286
throw new SocketException(
287287
'Transmitter is invalid. Sending aborted.',
288-
40900
288+
SocketException::CODE_UNACCEPTING_REQEUST
289289
);
290290
}
291291
$bytes = 0;
@@ -339,7 +339,7 @@ protected function parseArgumentString($string)
339339
} else {
340340
throw new InvalidArgumentException(
341341
"Parsing of argument name failed near '{$string}'",
342-
41000
342+
InvalidArgumentException::CODE_NAME_UNPARSABLE
343343
);
344344
}
345345
} elseif (preg_match('/^\s/s', $string, $matches)) {
@@ -367,7 +367,7 @@ protected function parseArgumentString($string)
367367
} else {
368368
throw new InvalidArgumentException(
369369
"Parsing of argument value failed near '{$string}'",
370-
41001
370+
InvalidArgumentException::CODE_VALUE_UNPARSABLE
371371
);
372372
}
373373
}

src/PEAR2/Net/RouterOS/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private function _receive(
153153
) {
154154
throw new SocketException(
155155
'No data within the time limit',
156-
50000
156+
SocketException::CODE_NO_DATA
157157
);
158158
}
159159
$this->setType($com->getNextWord());
@@ -231,7 +231,7 @@ protected function setType($type)
231231
default:
232232
throw new UnexpectedValueException(
233233
'Unrecognized response type.',
234-
50100,
234+
UnexpectedValueException::CODE_RESPONSE_TYPE_UNKNOWN,
235235
null,
236236
$type
237237
);

0 commit comments

Comments
 (0)