From 5d484977179086dcdb34dec61c6b74176166525b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 15 Jan 2019 12:30:02 -0100 Subject: [PATCH] +RequestResultNotJsonException Signed-off-by: Maxence Lange --- lib/Exceptions/RequestResultNotJsonException.php | 8 ++++++++ lib/Interfaces/Activity/FollowInterface.php | 2 ++ lib/Service/ActivityService.php | 3 +++ lib/Service/CacheActorService.php | 3 +++ lib/Service/CacheDocumentService.php | 3 +++ lib/Service/CurlService.php | 11 ++++++++++- lib/Service/DocumentService.php | 2 ++ lib/Service/FollowService.php | 3 +++ lib/Service/NoteService.php | 3 +++ lib/Service/SignatureService.php | 7 ++++++- 10 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 lib/Exceptions/RequestResultNotJsonException.php diff --git a/lib/Exceptions/RequestResultNotJsonException.php b/lib/Exceptions/RequestResultNotJsonException.php new file mode 100644 index 00000000..7564c0bd --- /dev/null +++ b/lib/Exceptions/RequestResultNotJsonException.php @@ -0,0 +1,8 @@ +signatureService->signRequest($request, $queue); $this->curlService->request($request); $this->queueService->endRequest($queue, true); + } catch (RequestResultNotJsonException $e) { + $this->queueService->endRequest($queue, true); } catch (ActorDoesNotExistException $e) { $this->miscService->log( 'Error while managing request: ' . json_encode($request) . ' ' . $e->getMessage(), 1 diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index d4e4473f..4b4e2308 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -40,6 +40,7 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\RequestContentException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\RequestNetworkException; use OCA\Social\Exceptions\RequestResultSizeException; @@ -120,6 +121,7 @@ class CacheActorService { * @throws RequestServerException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ public function getFromId(string $id, bool $refresh = false): Person { @@ -188,6 +190,7 @@ class CacheActorService { * @throws RequestServerException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ public function getFromAccount(string $account, bool $retrieve = true): Person { diff --git a/lib/Service/CacheDocumentService.php b/lib/Service/CacheDocumentService.php index 15095330..8f5c3fe0 100644 --- a/lib/Service/CacheDocumentService.php +++ b/lib/Service/CacheDocumentService.php @@ -39,6 +39,7 @@ use OCA\Social\Exceptions\CacheContentMimeTypeException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; use OCA\Social\Exceptions\RequestContentException; use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCP\Files\IAppData; @@ -98,6 +99,7 @@ class CacheDocumentService { * @throws RequestNetworkException * @throws RequestServerException * @throws RequestResultSizeException + * @throws RequestResultNotJsonException */ public function saveRemoteFileToCache(string $url, &$mime = '') { @@ -191,6 +193,7 @@ class CacheDocumentService { * @throws RequestNetworkException * @throws RequestServerException * @throws RequestResultSizeException + * @throws RequestResultNotJsonException */ public function retrieveContent(string $url) { $url = parse_url($url); diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index 1c1e1ba1..f90c5776 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -41,6 +41,7 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\RequestContentException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\RequestNetworkException; use OCA\Social\Exceptions\RequestResultSizeException; @@ -101,6 +102,7 @@ class CurlService { * @throws RequestServerException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ public function retrieveAccount(string $account): Person { $account = $this->withoutBeginAt($account); @@ -152,6 +154,7 @@ class CurlService { * @throws RequestNetworkException * @throws RequestServerException * @throws RequestResultSizeException + * @throws RequestResultNotJsonException */ public function retrieveObject($id): array { @@ -177,6 +180,7 @@ class CurlService { * @throws RequestNetworkException * @throws RequestResultSizeException * @throws RequestServerException + * @throws RequestResultNotJsonException */ public function request(Request $request) { @@ -204,7 +208,12 @@ class CurlService { '[>>] request: ' . json_encode($request) . ' - result: ' . $result, 1 ); - return json_decode((string)$result, true); + $result = json_decode((string)$result, true); + if (is_array($result)) { + return $result; + } + + throw new RequestResultNotJsonException(); } diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 07b86f6c..c250d0e3 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -42,6 +42,7 @@ use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RequestContentException; use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; @@ -114,6 +115,7 @@ class DocumentService { * @return Document * @throws CacheDocumentDoesNotExistException * @throws MalformedArrayException + * @throws RequestResultNotJsonException */ public function cacheRemoteDocument(string $id, bool $public = false) { $document = $this->cacheDocumentsRequest->getById($id, $public); diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index f6a2c45c..7998760c 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -41,6 +41,7 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\RequestContentException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\RequestNetworkException; use OCA\Social\Exceptions\RequestResultSizeException; @@ -133,6 +134,7 @@ class FollowService { * @throws RequestNetworkException * @throws RequestResultSizeException * @throws RequestServerException + * @throws RequestResultNotJsonException */ public function followAccount(Person $actor, string $account) { $remoteActor = $this->cacheActorService->getFromAccount($account); @@ -181,6 +183,7 @@ class FollowService { * @throws SocialAppConfigException * @throws ItemUnknownException * @throws UrlCloudException + * @throws RequestResultNotJsonException */ public function unfollowAccount(Person $actor, string $account) { $remoteActor = $this->cacheActorService->getFromAccount($account); diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index 373dab72..c832659c 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -43,6 +43,7 @@ use OCA\Social\Exceptions\NoteNotFoundException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\RequestContentException; use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; @@ -260,6 +261,7 @@ class NoteService { * @throws RequestServerException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ public function replyTo(Note $note, string $replyTo) { if ($replyTo === '') { @@ -401,6 +403,7 @@ class NoteService { * @throws RequestNetworkException * @throws RequestResultSizeException * @throws RequestServerException + * @throws RequestResultNotJsonException */ public function getAuthorFromPostId($noteId) { $note = $this->notesRequest->getNoteById($noteId); diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index 6ce7ddfc..face8629 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -43,6 +43,7 @@ use OCA\Social\Exceptions\LinkedDataSignatureMissingException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\RequestContentException; use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SignatureException; @@ -177,6 +178,7 @@ class SignatureService { * @throws SignatureIsGoneException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ public function checkRequest(IRequest $request, int &$time = 0): string { $dTime = new DateTime($request->getHeader('date')); @@ -210,6 +212,7 @@ class SignatureService { * @throws RequestServerException * @throws SocialAppConfigException * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ public function checkObject(ACore $object): bool { try { @@ -276,6 +279,7 @@ class SignatureService { * @throws ItemUnknownException * @throws RequestContentException * @throws RequestResultSizeException + * @throws RequestResultNotJsonException */ private function checkRequestSignature(IRequest $request): string { $signatureHeader = $request->getHeader('Signature'); @@ -365,6 +369,7 @@ class SignatureService { * @return string * @throws InvalidOriginException * @throws InvalidResourceException + * @throws ItemUnknownException * @throws MalformedArrayException * @throws RedundancyLimitException * @throws RequestContentException @@ -372,7 +377,7 @@ class SignatureService { * @throws RequestResultSizeException * @throws RequestServerException * @throws SocialAppConfigException - * @throws ItemUnknownException + * @throws RequestResultNotJsonException */ private function retrieveKey(string $keyId, bool $refresh = false): string { $actor = $this->cacheActorService->getFromId($keyId, $refresh);