verbb / snipcart

A Craft CMS plugin to integrate with Snipcart.
Other
21 stars 9 forks source link

"Subscriptions" button error in the Dashboard #59

Closed scandella closed 6 months ago

scandella commented 6 months ago

Describe the bug

Clicking on the "Subscriptions" button in the Dashboard raises an error: Attempt to read property "totalItems" on null

Here's the complete error:

 in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/verbb/snipcart/src/services/Subscriptions.phpat line 29
20212223242526272829303132333435363738        $params['offset'] = ($page - 1) * $limit;
        $params['limit'] = $limit;

        $response = Snipcart::$plugin->getApi()->get('subscriptions', $params);

        $items = $response->items ?? [];

        return (object) [
            'items' => ModelHelper::safePopulateArrayWithModels($items, Subscription::class),
            'totalItems' => $response->totalItems,
            'offset' => $response->offset,
            'limit' => $limit,
        ];
    }

    public function getSubscription(string $subscriptionId): ?Subscription
    {
        if ($subscriptionData = Snipcart::$plugin->getApi()->get("subscriptions/$subscriptionId")) {
            return ModelHelper::safePopulateModel((array)$subscriptionData, Subscription::class);
2. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/craftcms/cms/src/web/ErrorHandler.php at line 79– yii\base\ErrorHandler::handleError(2, 'Attempt to read property "totalI...', '/Users/Scandella/Documents/dev_r...', 29)
73747576777879808182838485    {
        // Because: https://bugs.php.net/bug.php?id=74980
        if (str_contains($message, 'Narrowing occurred during type inference. Please file a bug report')) {
            return true;
        }

        return parent::handleError($code, $message, $file, $line);
    }

    /**
     * @inheritdoc
     */
    public function getExceptionName($exception): ?string
3. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/verbb/snipcart/src/services/Subscriptions.php at line 29– craft\web\ErrorHandler::handleError(2, 'Attempt to read property "totalI...', '/Users/Scandella/Documents/dev_r...', 29)
23242526272829303132333435        $response = Snipcart::$plugin->getApi()->get('subscriptions', $params);

        $items = $response->items ?? [];

        return (object) [
            'items' => ModelHelper::safePopulateArrayWithModels($items, Subscription::class),
            'totalItems' => $response->totalItems,
            'offset' => $response->offset,
            'limit' => $limit,
        ];
    }

    public function getSubscription(string $subscriptionId): ?Subscription
4. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/verbb/snipcart/src/controllers/SubscriptionsController.php at line 19– verbb\snipcart\services\Subscriptions::listSubscriptions(1)
13141516171819202122232425    // Public Methods
    // =========================================================================

    public function actionIndex(): Response
    {
        $page = Craft::$app->getRequest()->getPageNum();
        $subscriptions = Snipcart::$plugin->getSubscriptions()->listSubscriptions($page);
        $totalPages = ceil($subscriptions->totalItems / $subscriptions->limit);

        return $this->renderTemplate('snipcart/cp/subscriptions/index', [
            'pageNumber' => $page,
            'totalPages' => $totalPages,
            'totalItems' => $subscriptions->totalItems,
5. verbb\snipcart\controllers\SubscriptionsController::actionIndex()
6. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/yiisoft/yii2/base/InlineAction.php at line 57– call_user_func_array([verbb\snipcart\controllers\SubscriptionsController, 'actionIndex'], [])
7. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/yiisoft/yii2/base/Controller.php at line 178– yii\base\InlineAction::runWithParams(['p' => 'admin/snipcart/subscriptions'])
8. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/yiisoft/yii2/base/Module.php at line 552– yii\base\Controller::runAction('index', ['p' => 'admin/snipcart/subscriptions'])
9. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/craftcms/cms/src/web/Application.php at line 305– yii\base\Module::runAction('snipcart/subscriptions/index', ['p' => 'admin/snipcart/subscriptions'])
299300301302303304305306307308309310311     * @param string $route
     * @param array $params
     * @return Response|null The result of the action, normalized into a Response object
     */
    public function runAction($route, $params = []): ?BaseResponse
    {
        $result = parent::runAction($route, $params);

        if ($result === null || $result instanceof Response) {
            return $result;
        }

        $response = $this->getResponse();
10. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/yiisoft/yii2/web/Application.php at line 103– craft\web\Application::runAction('snipcart/subscriptions/index', ['p' => 'admin/snipcart/subscriptions'])
11. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/craftcms/cms/src/web/Application.php at line 290– yii\web\Application::handleRequest(craft\web\Request)
284285286287288289290291292293294295296        if (($response = $this->_processActionRequest($request)) !== null) {
            return $response;
        }

        // If we’re still here, finally let Yii do its thing.
        try {
            return parent::handleRequest($request);
        } catch (Throwable $e) {
            $this->_unregisterDebugModule();
            throw $e;
        }
    }

12. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/vendor/yiisoft/yii2/base/Application.php at line 384– craft\web\Application::handleRequest(craft\web\Request)
13. in /Users/Scandella/Documents/dev_repo/dev_web/manufacture/dev/web/index.php at line 12– yii\base\Application::run()
6789101112// Load shared bootstrap
require dirname(__DIR__) . '/bootstrap.php';

// Load and run Craft
/** @var craft\web\Application $app */
$app = require CRAFT_VENDOR_PATH . '/craftcms/cms/bootstrap/web.php';
$app->run();

Steps to reproduce

  1. Click on the "Subscriptions" button in the Dashboard.
  2. An error is raised because the subscription list is empty.

Craft CMS version

Craft Pro 4.5.13

Plugin version

2.1.2

Multi-site?

No

Additional context

No response

scandella commented 6 months ago

Well, it works fine in production.