Skip to content

Commit 74ebfdd

Browse files
committed
Show human-readable error even if only uri is provided
List from http://www.loc.gov/standards/sru/diagnostics/diagnosticsList.html
1 parent cfb97e9 commit 74ebfdd

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

src/Response.php

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,91 @@
77
*/
88
class Response implements ResponseInterface {
99

10+
static public $errorMessages = array(
11+
'info:srw/diagnostic/1/1' => 'General system error',
12+
'info:srw/diagnostic/1/2' => 'System temporarily unavailable',
13+
'info:srw/diagnostic/1/3' => 'Authentication error',
14+
'info:srw/diagnostic/1/4' => 'Unsupported operation',
15+
'info:srw/diagnostic/1/5' => 'Unsupported version',
16+
'info:srw/diagnostic/1/6' => 'Unsupported parameter value',
17+
'info:srw/diagnostic/1/7' => 'Mandatory parameter not supplied',
18+
'info:srw/diagnostic/1/8' => 'Unsupported parameter',
19+
'info:srw/diagnostic/1/10' => 'Query syntax error',
20+
'info:srw/diagnostic/1/12' => 'Too many characters in query',
21+
'info:srw/diagnostic/1/13' => 'Invalid or unsupported use of parentheses',
22+
'info:srw/diagnostic/1/14' => 'Invalid or unsupported use of quotes',
23+
'info:srw/diagnostic/1/15' => 'Unsupported context set',
24+
'info:srw/diagnostic/1/16' => 'Unsupported index',
25+
'info:srw/diagnostic/1/18' => 'Unsupported combination of indexes',
26+
'info:srw/diagnostic/1/19' => 'Unsupported relation',
27+
'info:srw/diagnostic/1/20' => 'Unsupported relation modifier',
28+
'info:srw/diagnostic/1/21' => 'Unsupported combination of relation modifers',
29+
'info:srw/diagnostic/1/22' => 'Unsupported combination of relation and index',
30+
'info:srw/diagnostic/1/23' => 'Too many characters in term',
31+
'info:srw/diagnostic/1/24' => 'Unsupported combination of relation and term',
32+
'info:srw/diagnostic/1/26' => 'Non special character escaped in term',
33+
'info:srw/diagnostic/1/27' => 'Empty term unsupported',
34+
'info:srw/diagnostic/1/28' => 'Masking character not supported',
35+
'info:srw/diagnostic/1/29' => 'Masked words too short',
36+
'info:srw/diagnostic/1/30' => 'Too many masking characters in term',
37+
'info:srw/diagnostic/1/31' => 'Anchoring character not supported',
38+
'info:srw/diagnostic/1/32' => 'Anchoring character in unsupported position',
39+
'info:srw/diagnostic/1/33' => 'Combination of proximity/adjacency and masking characters not supported',
40+
'info:srw/diagnostic/1/34' => 'Combination of proximity/adjacency and anchoring characters not supported',
41+
'info:srw/diagnostic/1/35' => 'Term contains only stopwords',
42+
'info:srw/diagnostic/1/36' => 'Term in invalid format for index or relatio',
43+
'info:srw/diagnostic/1/37' => 'Unsupported boolean operator',
44+
'info:srw/diagnostic/1/38' => 'Too many boolean operators in query',
45+
'info:srw/diagnostic/1/39' => 'Proximity not supported',
46+
'info:srw/diagnostic/1/40' => 'Unsupported proximity relation',
47+
'info:srw/diagnostic/1/41' => 'Unsupported proximity distance',
48+
'info:srw/diagnostic/1/42' => 'Unsupported proximity unit',
49+
'info:srw/diagnostic/1/43' => 'Unsupported proximity ordering',
50+
'info:srw/diagnostic/1/44' => 'Unsupported combination of proximity modifiers',
51+
'info:srw/diagnostic/1/46' => 'Unsupported boolean modifier',
52+
'info:srw/diagnostic/1/47' => 'Cannot process query; reason unknown',
53+
'info:srw/diagnostic/1/48' => 'Query feature unsupported',
54+
'info:srw/diagnostic/1/49' => 'Masking character in unsupported position',
55+
'info:srw/diagnostic/1/50' => 'Result sets not supported',
56+
'info:srw/diagnostic/1/51' => 'Result set does not exist',
57+
'info:srw/diagnostic/1/52' => 'Result set temporarily unavailable',
58+
'info:srw/diagnostic/1/53' => 'Result sets only supported for retrieval',
59+
'info:srw/diagnostic/1/55' => 'Combination of result sets with search terms not supported',
60+
'info:srw/diagnostic/1/58' => 'Result set created with unpredictable partial results available',
61+
'info:srw/diagnostic/1/59' => 'Result set created with valid partial results available',
62+
'info:srw/diagnostic/1/60' => 'Result set not created: too many matching records',
63+
'info:srw/diagnostic/1/61' => 'First record position out of range',
64+
'info:srw/diagnostic/1/64' => 'Record temporarily unavailable',
65+
'info:srw/diagnostic/1/65' => 'Record does not exist',
66+
'info:srw/diagnostic/1/66' => 'Unknown schema for retrieval',
67+
'info:srw/diagnostic/1/67' => 'Record not available in this schema',
68+
'info:srw/diagnostic/1/68' => 'Not authorised to send record',
69+
'info:srw/diagnostic/1/69' => 'Not authorised to send record in this schema',
70+
'info:srw/diagnostic/1/70' => 'Record too large to send',
71+
'info:srw/diagnostic/1/71' => 'Unsupported record packing',
72+
'info:srw/diagnostic/1/72' => 'XPath retrieval unsupported',
73+
'info:srw/diagnostic/1/73' => 'XPath expression contains unsupported feature',
74+
'info:srw/diagnostic/1/74' => 'Unable to evaluate XPath expression',
75+
'info:srw/diagnostic/1/80' => 'Sort not supported',
76+
'info:srw/diagnostic/1/82' => 'Unsupported sort sequence',
77+
'info:srw/diagnostic/1/83' => 'Too many records to sort',
78+
'info:srw/diagnostic/1/84' => 'Too many sort keys to sort',
79+
'info:srw/diagnostic/1/86' => 'Cannot sort: incompatible record formats',
80+
'info:srw/diagnostic/1/87' => 'Unsupported schema for sort',
81+
'info:srw/diagnostic/1/88' => 'Unsupported path for sort',
82+
'info:srw/diagnostic/1/89' => 'Path unsupported for schema',
83+
'info:srw/diagnostic/1/90' => 'Unsupported direction',
84+
'info:srw/diagnostic/1/91' => 'Unsupported case',
85+
'info:srw/diagnostic/1/92' => 'Unsupported missing value action',
86+
'info:srw/diagnostic/1/93' => 'Sort ended due to missing value',
87+
'info:srw/diagnostic/1/94' => 'Sort spec included both in query and protocol: query prevails',
88+
'info:srw/diagnostic/1/95' => 'Sort spec included both in query and protocol: protocol prevails',
89+
'info:srw/diagnostic/1/96' => 'Sort spec included both in query and protocol: error',
90+
'info:srw/diagnostic/1/110' => 'Stylesheets not supported',
91+
'info:srw/diagnostic/1/120' => 'Response position out of range',
92+
'info:srw/diagnostic/1/130' => 'Too many terms matched by masked query term',
93+
);
94+
1095
/** @var string Raw XML response */
1196
protected $rawResponse;
1297

@@ -47,7 +132,22 @@ public function __construct($text, &$client = null)
47132

48133
$e = $this->response->first('/srw:*/srw:diagnostics');
49134
if ($e) {
50-
$this->error = $e->text('d:diagnostic/d:message') . '. ' . $e->text('d:diagnostic/d:details');
135+
// Only the 'uri' field is required, 'message' and 'details' are optional
136+
$uri = $e->text('d:diagnostic/d:uri');
137+
$msg = $e->text('d:diagnostic/d:message');
138+
$details = $e->text('d:diagnostic/d:details');
139+
if (empty($msg)) {
140+
if (isset(self::$errorMessages[$uri])) {
141+
$msg = self::$errorMessages[$uri];
142+
} else {
143+
$msg = 'Unknown error';
144+
}
145+
}
146+
if (!empty($details)) {
147+
$msg .= ' (' . $details . ')';
148+
149+
}
150+
$this->error = $msg;
51151
}
52152
}
53153

tests/SearchRetrieveResponseTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testMultipleRecordsResult()
9696
$this->assertEquals('Record 1', $res->records[0]->data);
9797
}
9898

99-
public function testError()
99+
public function testErrorWithDetails()
100100
{
101101
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
102102
<srw:version>1.1</srw:version>
@@ -105,12 +105,42 @@ public function testError()
105105
<diagnostic >
106106
<uri>info:srw/diagnostic/1/66</uri>
107107
<details>Invalid parameter: \'marcxml\' for service: \'biblio\'</details>
108-
<message>Unknown schema for retrieval</message>
109108
</diagnostic>
110109
</srw:diagnostics>
111110
</srw:searchRetrieveResponse>');
112111

113-
$this->assertEquals('Unknown schema for retrieval. Invalid parameter: \'marcxml\' for service: \'biblio\'', $res->error);
112+
$this->assertEquals('Unknown schema for retrieval (Invalid parameter: \'marcxml\' for service: \'biblio\')', $res->error);
113+
}
114+
115+
public function testErrorWithoutDetails()
116+
{
117+
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
118+
<srw:version>1.1</srw:version>
119+
<srw:numberOfRecords>0</srw:numberOfRecords>
120+
<srw:diagnostics xmlns="http://www.loc.gov/zing/srw/diagnostic/">
121+
<diagnostic >
122+
<uri>info:srw/diagnostic/1/1</uri>
123+
</diagnostic>
124+
</srw:diagnostics>
125+
</srw:searchRetrieveResponse>');
126+
$this->assertEquals('General system error', Response::$errorMessages['info:srw/diagnostic/1/1']);
127+
$this->assertEquals('General system error', $res->error);
128+
}
129+
130+
public function testErrorWithCustomMessage()
131+
{
132+
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
133+
<srw:version>1.1</srw:version>
134+
<srw:numberOfRecords>0</srw:numberOfRecords>
135+
<srw:diagnostics xmlns="http://www.loc.gov/zing/srw/diagnostic/">
136+
<diagnostic >
137+
<uri>info:srw/diagnostic/1/10</uri>
138+
<message>Too many boolean operators, the maximum is 10. Please try a less complex query.</message>
139+
<details>10</details>
140+
</diagnostic>
141+
</srw:diagnostics>
142+
</srw:searchRetrieveResponse>');
143+
$this->assertEquals('Too many boolean operators, the maximum is 10. Please try a less complex query. (10)', $res->error);
114144
}
115145

116146
}

0 commit comments

Comments
 (0)