Skip to content

Commit 92aab25

Browse files
committed
add errCode to MessageNotFound
1 parent c3bb72e commit 92aab25

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

examples/consumer-not-loop.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@
5050
// The message will be re-delivered after the specified time
5151
// $consumer->nack($message);
5252

53-
} catch (MessageNotFound$e) {
54-
echo "Message Not Found\n";
55-
continue;
53+
} catch (MessageNotFound $e) {
54+
// enum code see Exception/MessageNotFound.php
55+
$code = $e->getCode();
56+
if ($code == MessageNotFound::Ignore) {
57+
continue;
58+
}
59+
60+
throw $e;
5661
} catch (Throwable $e) {
5762
echo $e->getMessage();
5863
throw $e;

src/Consumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function receive(bool $loop = true): Message
204204
// It may appear that the current message is not CommandMessage
205205
if (!( $commandMessage instanceof CommandMessage )) {
206206
if (!$loop) {
207-
throw new MessageNotFound();
207+
throw new MessageNotFound('command parse fail', MessageNotFound::CommandParseFail);
208208
}
209209
return $this->receive($loop);
210210
}

src/Exception/MessageNotFound.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,23 @@
1818
class MessageNotFound extends Exception
1919
{
2020

21+
/**
22+
* Can be ignored, not a err
23+
*/
24+
const Ignore = 0;
25+
26+
/**
27+
* Command parsing failed
28+
*/
29+
const CommandParseFail = 1;
30+
31+
2132
/**
2233
* @param string $message
34+
* @param int $code
2335
*/
24-
public function __construct(string $message = 'Message Not Found')
36+
public function __construct(string $message = 'Message Not Found', int $code = MessageNotFound::Ignore)
2537
{
26-
parent::__construct($message);
38+
parent::__construct($message, $code);
2739
}
2840
}

0 commit comments

Comments
 (0)