From 437591b297b1531cd38d29798ef9fc419bddef59 Mon Sep 17 00:00:00 2001 From: aymm Date: Sat, 27 Mar 2021 10:18:05 +0000 Subject: [PATCH] Improved poll error handling --- src/APnutI.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/APnutI.php b/src/APnutI.php index 72ec436..f695d87 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -491,8 +491,13 @@ class APnutI public function getPollFromToken(int $poll_id, ?string $poll_token = null): Poll { $poll_token_query = empty($poll_token) ? '' : '?poll_token=' . $poll_token; - $res = $this->get('/polls/' . $poll_id . $poll_token_query); - return $this->getPollFromResponse($res); + try { + $res = $this->get('/polls/' . $poll_id . $poll_token_query); + return $this->getPollFromResponse($res); + } catch (NotAuthorizedException $nauth) { + $this->logger->error('Not authorized when fetching poll'); + throw new PollAccessRestrictedException(); + } } public function getPoll(int $poll_id, ?string $poll_token = null): Poll @@ -513,8 +518,13 @@ class APnutI 'include_html' => false, 'include_post_raw' => true ]; - $res = $this->get('/posts/' . $post_id, $args); - return $this->getPollFromResponse($res); + try { + $res = $this->get('/posts/' . $post_id, $args); + return $this->getPollFromResponse($res); + } catch (NotAuthorizedException $nauth) { + $this->logger->error('Not authorized when fetching poll'); + throw new PollAccessRestrictedException(); + } } else { $this->logger->debug('Poll token seems to be an actual poll token'); return $this->getPollFromToken($poll_id, $poll_token);