Skip to content

Commit d196f9c

Browse files
committed
Throw exception on error [BC]
1 parent 06f864b commit d196f9c

File tree

8 files changed

+32
-30
lines changed

8 files changed

+32
-30
lines changed

src/InvalidResponseException.php renamed to src/Exceptions/InvalidResponseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace Scriptotek\Sru;
1+
<?php namespace Scriptotek\Sru\Exceptions;
22

33
class InvalidResponseException extends \Exception
44
{

src/Exceptions/SruErrorException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace Scriptotek\Sru\Exceptions;
2+
3+
class SruErrorException extends \Exception
4+
{
5+
public $uri;
6+
7+
/**
8+
* @param string $message
9+
*/
10+
public function __construct($message, $uri)
11+
{
12+
parent::__construct($message);
13+
$this->uri = $uri;
14+
}
15+
}

src/Providers/SruServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function register()
3636
$app['sru-client'] = $app->share(function ($app) {
3737
return new SruClient($app['config']->get('sru.endpoint'), $app['config']->get('sru'));
3838
});
39-
$app->alias('sru-client', 'Scriptotek\Sru\Client');
39+
$app->alias('sru-client', SruClient::class);
4040
}
4141

4242
/**

src/Records.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ public function __construct($cql, Client $client, $count = 10, $extraParams = ar
5252
$this->fetchMore();
5353
}
5454

55-
/**
56-
* Return error message from last reponse, if any
57-
*
58-
* @return string|null
59-
*/
60-
public function __get($prop)
61-
{
62-
if ($prop == 'error') {
63-
return $this->lastResponse->error;
64-
}
65-
}
66-
6755
/**
6856
* Return the number of records
6957
*
@@ -88,7 +76,7 @@ private function fetchMore()
8876
$this->data = $this->lastResponse->records;
8977

9078
if (count($this->data) != 0 && $this->data[0]->position != $this->position) {
91-
throw new InvalidResponseException('Wrong index of first record in result set. '
79+
throw new Exceptions\InvalidResponseException('Wrong index of first record in result set. '
9280
. 'Expected: ' .$this->position . ', got: ' . $this->data[0]->position
9381
);
9482
}

src/Response.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ class Response implements ResponseInterface
101101
/** @var Client Reference to SRU client object */
102102
protected $client;
103103

104-
/** @var string Error message */
105-
public $error;
106-
107104
/** @var string SRU protocol version */
108105
public $version;
109106

@@ -146,7 +143,7 @@ public function __construct($text, &$client = null)
146143
if (!empty($details)) {
147144
$msg .= ' (' . $details . ')';
148145
}
149-
$this->error = $msg;
146+
throw new Exceptions\SruErrorException($msg, $uri);
150147
}
151148
}
152149

tests/ExplainResponseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public function testNormalResponse()
102102
</sru:record>
103103
</sru:explainResponse>');
104104

105-
$this->assertNull($res->error);
106105
$this->assertEquals('1.2', $res->version);
107106
$this->assertEquals('sru.bibsys.no', $res->host);
108107
$this->assertEquals(80, $res->port);

tests/RecordsTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public function testIterating()
1414

1515
$client = new Client($uri);
1616
$records = new Records($cql, $client, 10, array(), $http);
17-
$this->assertNull($records->error);
1817
$this->assertEquals(8, $records->numberOfRecords());
1918
$records->rewind();
20-
$this->assertNull($records->error);
2119

2220
$this->assertEquals(1, $records->key());
2321
$this->assertTrue($records->valid());
@@ -37,7 +35,7 @@ public function testIterating()
3735
}
3836

3937
/**
40-
* @expectedException Scriptotek\Sru\InvalidResponseException
38+
* @expectedException Scriptotek\Sru\Exceptions\InvalidResponseException
4139
*/
4240
public function testRepeatSameResponse()
4341
{

tests/SearchRetrieveResponseTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function testSingleRecordResult()
3434
</srw:extraResponseData>
3535
</srw:searchRetrieveResponse>');
3636

37-
$this->assertNull($res->error);
3837
$this->assertEquals('1.1', $res->version);
3938
$this->assertEquals(1, $res->numberOfRecords);
4039
$this->assertNull($res->nextRecordPosition);
@@ -83,7 +82,6 @@ public function testMultipleRecordsResult()
8382
</srw:extraResponseData>
8483
</srw:searchRetrieveResponse>');
8584

86-
$this->assertNull($res->error);
8785
$this->assertEquals('1.1', $res->version);
8886
$this->assertEquals(303, $res->numberOfRecords);
8987
$this->assertEquals(3, $res->nextRecordPosition);
@@ -95,6 +93,10 @@ public function testMultipleRecordsResult()
9593
$this->assertEquals('Record 1', $res->records[0]->data);
9694
}
9795

96+
/**
97+
* @expectedException Scriptotek\Sru\Exceptions\SruErrorException
98+
* @expectedExceptionMessage Unknown schema for retrieval (Invalid parameter: 'marcxml' for service: 'biblio')
99+
*/
98100
public function testErrorWithDetails()
99101
{
100102
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
@@ -107,10 +109,12 @@ public function testErrorWithDetails()
107109
</diagnostic>
108110
</srw:diagnostics>
109111
</srw:searchRetrieveResponse>');
110-
111-
$this->assertEquals('Unknown schema for retrieval (Invalid parameter: \'marcxml\' for service: \'biblio\')', $res->error);
112112
}
113113

114+
/**
115+
* @expectedException Scriptotek\Sru\Exceptions\SruErrorException
116+
* @expectedExceptionMessage General system error
117+
*/
114118
public function testErrorWithoutDetails()
115119
{
116120
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
@@ -122,10 +126,12 @@ public function testErrorWithoutDetails()
122126
</diagnostic>
123127
</srw:diagnostics>
124128
</srw:searchRetrieveResponse>');
125-
$this->assertEquals('General system error', Response::$errorMessages['info:srw/diagnostic/1/1']);
126-
$this->assertEquals('General system error', $res->error);
127129
}
128130

131+
/**
132+
* @expectedException Scriptotek\Sru\Exceptions\SruErrorException
133+
* @expectedExceptionMessage Too many boolean operators, the maximum is 10. Please try a less complex query. (10)
134+
*/
129135
public function testErrorWithCustomMessage()
130136
{
131137
$res = new SearchRetrieveResponse('<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
@@ -139,6 +145,5 @@ public function testErrorWithCustomMessage()
139145
</diagnostic>
140146
</srw:diagnostics>
141147
</srw:searchRetrieveResponse>');
142-
$this->assertEquals('Too many boolean operators, the maximum is 10. Please try a less complex query. (10)', $res->error);
143148
}
144149
}

0 commit comments

Comments
 (0)