]> git.mxchange.org Git - friendica-addons.git/commitdiff
[twitter] Add HTTP error code handling
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 21 Jun 2020 20:32:37 +0000 (16:32 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 21 Jun 2020 20:32:37 +0000 (16:32 -0400)
twitter/twitter.php

index d4702379fbf57095786970b49ec3e9f8c7c76be6..ff773c0a32682f34e3586aa4921026f0d7a139ef 100644 (file)
@@ -500,9 +500,15 @@ function twitter_action(App $a, $uid, $pid, $action)
                        break;
                case 'like':
                        $result = $connection->post('favorites/create', $post);
+                       if ($connection->getLastHttpCode() != 200) {
+                               Logger::error('Unable to create favorite', ['result' => $result]);
+                       }
                        break;
                case 'unlike':
                        $result = $connection->post('favorites/destroy', $post);
+                       if ($connection->getLastHttpCode() != 200) {
+                               Logger::error('Unable to destroy favorite', ['result' => $result]);
+                       }
                        break;
                default:
                        Logger::warning('Unhandled action', ['action' => $action]);
@@ -1048,26 +1054,28 @@ function twitter_get_relation($uid, $target, $contact = [])
 
        try {
                $status = $connection->get('friendships/show', $parameters);
-       } catch (TwitterOAuthException $e) {
-               Logger::info('Error fetching friendship status', ['user' => $uid, 'target' => $target, 'message' => $e->getMessage()]);
-               return $relation;
-       }
+               if ($connection->getLastHttpCode() !== 200) {
+                       throw new Exception($status->errors[0]->message ?? 'HTTP response code ' . $connection->getLastHttpCode(), $status->errors[0]->code ?? $connection->getLastHttpCode());
+               }
 
-       $following = $status->relationship->source->following;
-       $followed = $status->relationship->source->followed_by;
+               $following = $status->relationship->source->following;
+               $followed = $status->relationship->source->followed_by;
+
+               if ($following && !$followed) {
+                       $relation = Contact::SHARING;
+               } elseif (!$following && $followed) {
+                       $relation = Contact::FOLLOWER;
+               } elseif ($following && $followed) {
+                       $relation = Contact::FRIEND;
+               } elseif (!$following && !$followed) {
+                       $relation = 0;
+               }
 
-       if ($following && !$followed) {
-               $relation = Contact::SHARING;
-       } elseif (!$following && $followed) {
-               $relation = Contact::FOLLOWER;
-       } elseif ($following && $followed) {
-               $relation = Contact::FRIEND;
-       } elseif (!$following && !$followed) {
-               $relation = 0;
+               Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]);
+       } catch (Throwable $e) {
+               Logger::error('Error fetching friendship status', ['user' => $uid, 'target' => $target, 'message' => $e->getMessage()]);
        }
 
-       Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]);
-
        return $relation;
 }