]> git.mxchange.org Git - friendica.git/commitdiff
Move System::jsonError to BaseModule->jsonError
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 21 Sep 2023 16:27:15 +0000 (12:27 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 24 Sep 2023 11:08:15 +0000 (07:08 -0400)
- This will ensure headers set in BaseModule->run will be carried in jsonError scenarios
- Make BaseApi->checkThrottleLimit an object method to use BaseModule->jsonError
- Deprecate jsonError() method in Core\System

src/BaseModule.php
src/Core/System.php
src/Factory/Api/Mastodon/Error.php
src/Module/Api/Mastodon/Proofs.php
src/Module/Api/Mastodon/Statuses.php
src/Module/Api/Twitter/Statuses/Update.php
src/Module/BaseApi.php
src/Module/Circle.php
src/Module/Friendica.php
src/Module/NoScrape.php
src/Module/Profile/Profile.php

index 463954f000935d21b0a6bd1caf8f1044ceae27c3..0e645edf6a6ab1f9e2d545d1a8ff3137029b20c3 100644 (file)
@@ -510,4 +510,23 @@ abstract class BaseModule implements ICanHandleRequests
        {
                $this->httpExit(json_encode($content, $options), ICanCreateResponses::TYPE_JSON, $content_type);
        }
+
+       /**
+        * Display a non-200 HTTP code response using JSON to encode the content and exit
+        *
+        * @param int    $httpCode
+        * @param mixed  $content
+        * @param string $content_type
+        * @return void
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function jsonError(int $httpCode, $content, string $content_type = 'application/json')
+       {
+               if ($httpCode >= 400) {
+                       $this->logger->debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
+               }
+
+               $this->response->setStatus($httpCode);
+               $this->jsonExit($content, $content_type);
+       }
 }
index 6af4186a37ae0f3ae51188ebcb97f99615de6ea4..89363349cf663ab6c294853548abd33640269768 100644 (file)
@@ -373,6 +373,9 @@ class System
                self::exit();
        }
 
+       /**
+        * @deprecated since 2023.09 Use BaseModule->jsonError instead
+        */
        public static function jsonError($httpCode, $content, $content_type = 'application/json')
        {
                if ($httpCode >= 400) {
index 32ca03a62a55f481a16cad2de131fb2e0f4dd55b..68172d63233bbee766a5f2dc0539552c66dfac0f 100644 (file)
@@ -57,7 +57,7 @@ class Error extends BaseFactory
                $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
 
                $this->logError(404, $error);
-               System::jsonError(404, $errorObj->toArray());
+               $this->jsonError(404, $errorObj->toArray());
        }
 
        public function UnprocessableEntity(string $error = '')
@@ -67,7 +67,7 @@ class Error extends BaseFactory
                $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
 
                $this->logError(422, $error);
-               System::jsonError(422, $errorObj->toArray());
+               $this->jsonError(422, $errorObj->toArray());
        }
 
        public function Unauthorized(string $error = '', string $error_description = '')
@@ -76,7 +76,7 @@ class Error extends BaseFactory
                $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
 
                $this->logError(401, $error);
-               System::jsonError(401, $errorObj->toArray());
+               $this->jsonError(401, $errorObj->toArray());
        }
 
        public function Forbidden(string $error = '')
@@ -86,7 +86,7 @@ class Error extends BaseFactory
                $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
 
                $this->logError(403, $error);
-               System::jsonError(403, $errorObj->toArray());
+               $this->jsonError(403, $errorObj->toArray());
        }
 
        public function InternalError(string $error = '')
@@ -96,6 +96,6 @@ class Error extends BaseFactory
                $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
 
                $this->logError(500, $error);
-               System::jsonError(500, $errorObj->toArray());
+               $this->jsonError(500, $errorObj->toArray());
        }
 }
index 1cbe1c03d7b4fa48156a6fb2a782436c7f4e7a36..df878e5bb26f19c2705f2b871e748a420beb0dcf 100644 (file)
@@ -34,6 +34,6 @@ class Proofs extends BaseApi
         */
        protected function rawContent(array $request = [])
        {
-               System::jsonError(404, ['error' => 'Record not found']);
+               $this->jsonError(404, ['error' => 'Record not found']);
        }
 }
index b916f4b4637569a359c34ce530243b6efecc0ee1..0369d2026c7debe2c4acdf2c5c3942a99b2352c4 100644 (file)
@@ -263,7 +263,7 @@ class Statuses extends BaseApi
                        $item['gravity']     = Item::GRAVITY_COMMENT;
                        $item['object-type'] = Activity\ObjectType::COMMENT;
                } else {
-                       self::checkThrottleLimit();
+                       $this->checkThrottleLimit();
 
                        $item['gravity']     = Item::GRAVITY_PARENT;
                        $item['object-type'] = Activity\ObjectType::NOTE;
index 397e3bbd270f16108a11a97ad3ce1b852400af98..2151dabd6886fdfc7d8ae96cc32668b8030164d1 100644 (file)
@@ -121,7 +121,7 @@ class Update extends BaseApi
                        $item['gravity']     = Item::GRAVITY_COMMENT;
                        $item['object-type'] = Activity\ObjectType::COMMENT;
                } else {
-                       self::checkThrottleLimit();
+                       $this->checkThrottleLimit();
 
                        $item['gravity']     = Item::GRAVITY_PARENT;
                        $item['object-type'] = Activity\ObjectType::NOTE;
index d6e5f748f23d35976b5cf99eb84e2ddffb0ed520..c73f90aba62676a1eecd7f05c483d74b4bac9550 100644 (file)
@@ -434,7 +434,7 @@ class BaseApi extends BaseModule
                }
        }
 
-       public static function checkThrottleLimit()
+       public function checkThrottleLimit()
        {
                $uid = self::getCurrentUserID();
 
@@ -447,11 +447,11 @@ class BaseApi extends BaseModule
                        $posts_day = Post::countThread($condition);
 
                        if ($posts_day > $throttle_day) {
-                               Logger::notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
-                               $error = DI::l10n()->t('Too Many Requests');
-                               $error_description = DI::l10n()->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
+                               $this->logger->notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
+                               $error = $this->t('Too Many Requests');
+                               $error_description = $this->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
                                $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
-                               System::jsonError(429, $errorobj->toArray());
+                               $this->jsonError(429, $errorobj->toArray());
                        }
                }
 
@@ -464,10 +464,10 @@ class BaseApi extends BaseModule
 
                        if ($posts_week > $throttle_week) {
                                Logger::notice('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]);
-                               $error = DI::l10n()->t('Too Many Requests');
-                               $error_description = DI::l10n()->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
+                               $error = $this->t('Too Many Requests');
+                               $error_description = $this->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
                                $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
-                               System::jsonError(429, $errorobj->toArray());
+                               $this->jsonError(429, $errorobj->toArray());
                        }
                }
 
@@ -480,10 +480,10 @@ class BaseApi extends BaseModule
 
                        if ($posts_month > $throttle_month) {
                                Logger::notice('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]);
-                               $error = DI::l10n()->t('Too Many Requests');
-                               $error_description = DI::l10n()->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month);
+                               $error = $this->t('Too Many Requests');
+                               $error_description = $this->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month);
                                $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
-                               System::jsonError(429, $errorobj->toArray());
+                               $this->jsonError(429, $errorobj->toArray());
                        }
                }
        }
index e2b6f8977891284303edae82f6a5719d3b4420ba..6fd80a27beda33d0a79569f0c2e0ab9847964cde 100644 (file)
@@ -135,7 +135,7 @@ class Circle extends BaseModule
                        $this->jsonExit(['status' => 'OK', 'message' => $message]);
                } catch (\Exception $e) {
                        DI::sysmsg()->addNotice($e->getMessage());
-                       System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
+                       $this->jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
                }
        }
 
index 10a07f6e6587dbc0206ca3897713c0a2d7d24e12..e2a9d5ee65d3e777f9ae5d1e15f72cab150f550b 100644 (file)
@@ -144,7 +144,7 @@ class Friendica extends BaseModule
                                header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
                                $this->jsonExit($data, 'application/activity+json');
                        } catch (HTTPException\NotFoundException $e) {
-                               System::jsonError(404, ['error' => 'Record not found']);
+                               $this->jsonError(404, ['error' => 'Record not found']);
                        }
                }
 
index 8751b30bea32a2c1a183a74d1cf93446975261ec..cef7cdce72010dbbcaba891a05fc70e2e2ce0e9c 100644 (file)
@@ -45,13 +45,13 @@ class NoScrape extends BaseModule
                        // view infos about a known profile (needs a login)
                        $which = $a->getLoggedInUserNickname();
                } else {
-                       System::jsonError(403, 'Authentication required');
+                       $this->jsonError(403, 'Authentication required');
                }
 
                $owner = User::getOwnerDataByNick($which);
 
                if (empty($owner['uid'])) {
-                       System::jsonError(404, 'Profile not found');
+                       $this->jsonError(404, 'Profile not found');
                }
 
                $json_info = [
index 023cc38a5a93c645b9d4e7d5769a65a9e5ed836c..e639c6801bc19117cf90031b8a1bf457e27365a8 100644 (file)
@@ -89,7 +89,7 @@ class Profile extends BaseProfile
                                        header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
                                        $this->jsonExit($data, 'application/activity+json');
                                } catch (HTTPException\NotFoundException $e) {
-                                       System::jsonError(404, ['error' => 'Record not found']);
+                                       $this->jsonError(404, ['error' => 'Record not found']);
                                }
                        }
 
@@ -97,10 +97,10 @@ class Profile extends BaseProfile
                                // Known deleted user
                                $data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
 
-                               System::jsonError(410, $data);
+                               $this->jsonError(410, $data);
                        } else {
                                // Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
-                               System::jsonError(404, []);
+                               $this->jsonError(404, []);
                        }
                }
        }