Skip to content

Commit 0b1af44

Browse files
committed
Improve error messages about wrapper segments from Message::parse()
1 parent 73c35a6 commit 0b1af44

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/Fhp/Protocol/Message.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,16 @@ public static function parse(string $rawMessage): Message
300300
$segments = Parser::parseSegments($rawMessage);
301301

302302
// Message header and footer must always be there, or something went badly wrong.
303-
if (!($segments[0] instanceof HNHBKv3)) {
304-
throw new \InvalidArgumentException("Expected first segment to be HNHBK: $rawMessage");
305-
}
306-
if (!($segments[count($segments) - 1] instanceof HNHBSv1)) {
307-
throw new \InvalidArgumentException("Expected last segment to be HNHBS: $rawMessage");
308-
}
309303
$result->header = $segments[0];
310304
$result->footer = $segments[count($segments) - 1];
305+
if (!($result->header instanceof HNHBKv3)) {
306+
$actual = $result->header->getName();
307+
throw new \InvalidArgumentException("Expected first segment to be HNHBK, but got $actual: $rawMessage");
308+
}
309+
if (!($result->footer instanceof HNHBSv1)) {
310+
$actual = $result->footer->getName();
311+
throw new \InvalidArgumentException("Expected last segment to be HNHBS, but got $actual: $rawMessage");
312+
}
311313

312314
// Check if there's an encryption header and "encrypted" data.
313315
// Section B.8 specifies that there are exactly 4 segments: HNHBK, HNVSK, HNVSD, HNHBS.

0 commit comments

Comments
 (0)