Skip to content

Commit 1c94ebb

Browse files
committed
Sort out nicer handling of a document not found
1 parent f420720 commit 1c94ebb

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/PHPCouchDB/Database.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,22 @@ public function create($doc)
143143
public function getDocById($id) : Document
144144
{
145145
$endpoint = "/" . $this->db_name . "/" . $id;
146-
$response = $this->client->request("GET", $endpoint);
147-
if ($response->getStatusCode() == 200) {
146+
try {
147+
$response = $this->client->request("GET", $endpoint);
148148
if ($data = json_decode($response->getBody(), true)) {
149149
$doc = new Document($this, $data);
150150
return $doc;
151151
} else {
152152
throw new Exception\ServerException('JSON response not received or not understood');
153153
}
154-
} else {
155-
throw new Exception\DatabaseException('Document not found');
154+
} catch (\GuzzleHttp\Exception\ClientException $e) {
155+
$status = $e->getResponse()->getStatusCode();
156+
if ($status == 404) {
157+
// not really a disaster, the doc just isn't there so throw specific exception
158+
throw new Exception\DocumentNotFoundException('Document not found');
159+
} else {
160+
throw new Exception\DatabaseException('The document could not be retrieved', 0, $e);
161+
}
156162
}
157163
}
158164
}

0 commit comments

Comments
 (0)