fix: [internal] Fix error code when fetching sightings

pull/9479/head
Jakub Onderka 2024-01-12 12:52:34 +01:00
parent 59916f848a
commit d2911274b5
2 changed files with 9 additions and 4 deletions

View File

@ -18,10 +18,15 @@ class HttpSocketHttpException extends Exception
{ {
$this->response = $response; $this->response = $response;
$this->url = $url; $this->url = $url;
$message = "Remote server returns HTTP error code $response->code"; $message = "Remote server returns HTTP error code $response->code";
if ($url) { if ($url) {
$message .= " for URL $url"; $message .= " for URL $url";
} }
if ($response->body) {
$message .= ': ' . substr($response->body, 0, 100);
}
parent::__construct($message, (int)$response->code); parent::__construct($message, (int)$response->code);
} }

View File

@ -1017,16 +1017,16 @@ class Sighting extends AppModel
* @return TmpFileTool * @return TmpFileTool
* @throws Exception * @throws Exception
*/ */
public function restSearch(array $user, $returnFormat, $filters) public function restSearch(array $user, $returnFormat, array $filters)
{ {
$allowedContext = array('event', 'attribute'); $allowedContext = array('event', 'attribute');
// validate context // validate context
if (isset($filters['context']) && !in_array($filters['context'], $allowedContext, true)) { if (isset($filters['context']) && !in_array($filters['context'], $allowedContext, true)) {
throw new MethodNotAllowedException(__('Invalid context %s.', $filters['context'])); throw new BadRequestException(__('Invalid context %s.', $filters['context']));
} }
// ensure that an id or uuid is provided if context is set // ensure that an id or uuid is provided if context is set
if (!empty($filters['context']) && !(isset($filters['id']) || isset($filters['uuid'])) ) { if (!empty($filters['context']) && !(isset($filters['id']) || isset($filters['uuid'])) ) {
throw new MethodNotAllowedException(__('An ID or UUID must be provided if the context is set.')); throw new BadRequestException(__('An ID or UUID must be provided if the context is set.'));
} }
if (!isset($this->validFormats[$returnFormat][1])) { if (!isset($this->validFormats[$returnFormat][1])) {
@ -1396,7 +1396,7 @@ class Sighting extends AppModel
try { try {
$sightings = $serverSync->fetchSightingsForEvents($chunk); $sightings = $serverSync->fetchSightingsForEvents($chunk);
} catch (Exception $e) { } catch (Exception $e) {
$this->logException("Failed downloading the sightings from {$serverSync->server()['Server']['name']}.", $e); $this->logException("Failed to download sightings from {$serverSync->server()['Server']['name']}.", $e);
continue; continue;
} }