Skip to content

Commit 88605e8

Browse files
committed
Don't throw exception if diagnostics is set, but empty
1 parent 0cd39fa commit 88605e8

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/Response.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,25 @@ public function __construct($text, &$client = null)
127127

128128
$this->version = $this->response->text('/srw:*/srw:version');
129129

130-
$e = $this->response->first('/srw:*/srw:diagnostics');
131-
if ($e) {
130+
$diagnostic = $this->response->first('/srw:*/srw:diagnostics/d:diagnostic');
131+
if ($diagnostic) {
132132
// Only the 'uri' field is required, 'message' and 'details' are optional
133-
$uri = $e->text('d:diagnostic/d:uri');
134-
$msg = $e->text('d:diagnostic/d:message');
135-
$details = $e->text('d:diagnostic/d:details');
136-
if (empty($msg)) {
137-
if (isset(self::$errorMessages[$uri])) {
138-
$msg = self::$errorMessages[$uri];
139-
} else {
140-
$msg = 'Unknown error';
133+
$uri = $diagnostic->text('d:uri');
134+
if (strlen($uri)) {
135+
$msg = $diagnostic->text('d:message');
136+
$details = $diagnostic->text('d:details');
137+
if (empty($msg)) {
138+
if (isset(self::$errorMessages[$uri])) {
139+
$msg = self::$errorMessages[$uri];
140+
} else {
141+
$msg = 'Unknown error';
142+
}
141143
}
144+
if (!empty($details)) {
145+
$msg .= ' (' . $details . ')';
146+
}
147+
throw new Exceptions\SruErrorException($msg, $uri);
142148
}
143-
if (!empty($details)) {
144-
$msg .= ' (' . $details . ')';
145-
}
146-
throw new Exceptions\SruErrorException($msg, $uri);
147149
}
148150
}
149151

tests/SearchRetrieveResponseTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,16 @@ public function testErrorWithCustomMessage()
146146
</srw:diagnostics>
147147
</srw:searchRetrieveResponse>');
148148
}
149+
150+
// Should not throw error
151+
public function testDiagnosticsWithoutError()
152+
{
153+
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
154+
<srw:version>1.1</srw:version>
155+
<srw:numberOfRecords>0</srw:numberOfRecords>
156+
<srw:diagnostics xmlns="http://www.loc.gov/zing/srw/diagnostic/">
157+
</srw:diagnostics>
158+
</srw:searchRetrieveResponse>');
159+
}
160+
149161
}

0 commit comments

Comments
 (0)