Commit 99de175f authored by Felip Manyer i Ballester's avatar Felip Manyer i Ballester
Browse files

Exception handling

parent 252c210a
Pipeline #92 passed with stages
in 19 seconds
......@@ -39,6 +39,9 @@ abstract class Gbif {
*
* @return array
* An array of results.
*
* @throws \ResTelae\Gbif\GbifException
* In case the HTTP request ends with an error.
*/
protected function gbifGet($uri, array $args = []) {
$query_string = $this->formatQueryString($args);
......@@ -46,11 +49,11 @@ abstract class Gbif {
$response = $this->gbifClient->get($uri, ['query' => $query_string]);
}
catch (RequestException $e) {
error_log((string) $e->getRequest());
$msg = (string) $e->getRequest();
if ($e->hasResponse) {
error_log((string) $e->getResponse());
$msg .= (string) $e->getResponse();
throw new GbifException($msg);
}
return [];
}
$data = (string) $response->getBody();
......
<?php
namespace ResTelae\Gbif;
/**
* GBIF: exception handler.
*/
class GbifException extends \Exception {
/**
* {@inheritdoc}
*/
public function __construct($message = '', $code = 0, \Throwable $previous = NULL) {
parent::__construct($message, $code, $previous);
}
}
......@@ -84,6 +84,9 @@ class Species extends Gbif {
* @return array
* An array of results.
*
* @throws \ResTelae\Gbif\GbifException
* When some arguments are wrong.
*
* @see http://www.gbif.org/developer/species#nameUsages
*/
public function nameUsage(array $args = [], $data = 'all', $key = NULL, $uuid = NULL, $short_name = NULL) {
......@@ -111,11 +114,11 @@ class Species extends Gbif {
];
if (!in_array($data, $data_choices)) {
// TODO: raise exception.
throw new GbifException('Illegal choice for `data`');
}
if ($data != 'all' && !$key) {
// TODO: raise exception.
throw new GbifException('You must specify a key if `data` does not equal `all`');
}
if ($data == 'all') {
......@@ -123,7 +126,7 @@ class Species extends Gbif {
}
elseif ($data == 'root') {
if (!$uuid && !$short_name) {
// TODO: raise exception.
throw new GbifException('`uuid` and `short_name` cannot be both NULL if `data` equals "root"');
}
$uri = $uuid ? 'species/' . $uuid : 'species/' . $short_name;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment