Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] latest 4.8.07 cause site error Call to a member function get() on false #974

Closed
ziming opened this issue Dec 9, 2024 · 16 comments
Closed
Assignees

Comments

@ziming
Copy link

ziming commented Dec 9, 2024

Describe the bug
After updating to the latest version. I keep get this error in Sentry and my site went down

Call to a member function get() on false

According to sentry, this is the line

$this->detect->isIE()

where detect is MobileDetect

User-agent/Device/Phone/Tablet (please complete the following information):

  • Chrome latest, but other browsers too on laptops and on phones

I'm also using Laravel 11 and php 8.3 latest version

@samliu999
Copy link

samliu999 commented Dec 9, 2024

same issue here.

this is my log, i using laravel 10 and php 8.3

    "class": "Error",
    "message": "Call to a member function get() on bool",
    "code": 0,
    "file": "/var/www/html/vendor/mobiledetect/mobiledetectlib/src/MobileDetect.php:1527"

and my version also is 4.8.07

@samliu999
Copy link

if i use depenlancy injection like
image
and i will get cache by laravel
image

but i need depenlancy injection MobileDetect class

so when laravel register i give this

image

then my cache will using Detection\Cache\Cache and anything be fine

english no good but i hope can help someone like me using laravel and MobileDetect

peace !!

@serbanghita
Copy link
Owner

@ziming how are you using this in Laravel?

@samliu999 it looks that I've overlooked (also in the tests) the fact that the change from

    public function __construct(
        Cache $cache = null,
        array $config = [],
    ) {

to

    public function __construct(
        ?CacheInterface $cache = null,
        array $config = [],
    ) {

makes $this->cache->get($cacheKey); return CacheItem|mixed|null as opposed to CacheItem|null

I will patch this asap

@ziming
Copy link
Author

ziming commented Dec 9, 2024

I'm using it in Middleware check if IE then if it is IE redirect them to a page to upgrade their browser

@serbanghita
Copy link
Owner

serbanghita commented Dec 9, 2024

I had to delete 4.8.07 temporary (from the packagist as well) until I have full coverage over the Cache classes.
I'm fixing this at approx 18:00 PM EET, meanwhile stick to 4.8.06

@serbanghita
Copy link
Owner

released it again https://github.com/serbanghita/Mobile-Detect/releases/tag/4.8.07

let me know if you have issues

@levpashaDJA
Copy link

levpashaDJA commented Dec 9, 2024

Same here, 4.8.07 (installed using Composer) breaks everything:

[09-Dec-2024 17:39:56 America/New_York] PHP Fatal error: Uncaught Error: Interface "Psr\Cache\CacheItemInterface" not found in /var/app/current/vendor/mobiledetect/mobiledetectlib/src/Cache/CacheItem.php:14
Stack trace:
#0 /var/app/current/vendor/composer/ClassLoader.php(576): include()
#1 /var/app/current/vendor/composer/ClassLoader.php(427): Composer\Autoload{closure}()
#2 /var/app/current/vendor/mobiledetect/mobiledetectlib/src/Cache/Cache.php(52): Composer\Autoload\ClassLoader->loadClass()
#3 /var/app/current/vendor/mobiledetect/mobiledetectlib/src/MobileDetect.php(1434): Detection\Cache\Cache->set()
#4 /var/app/current/lib/App/DefaultApp.php(35): Detection\MobileDetect->isMobile()
#5 /var/app/current/lib/Document/DefaultDocument.php(20): DJA\App\DefaultApp->__construct()
#6 /var/app/current/html/ajax/landing.php(8): DJA\Document\DefaultDocument->__construct()
#7 {main}
thrown in /var/app/current/vendor/mobiledetect/mobiledetectlib/src/Cache/CacheItem.php on line 14

@serbanghita
Copy link
Owner

https://github.com/serbanghita/Mobile-Detect/releases/tag/4.8.08

I have somehow missed the psr/cache dependency due to the latest Cache changes.
I'm taking some steps further to mitigate this kind of mishaps and have some additional "smoke tests" in the CI pipeline

Please check 4.8.08 and thank you!

@ziming
Copy link
Author

ziming commented Dec 10, 2024

After removing the lock on 4.8.06

i composer update, deploy and almost right away my prod server receive 36 sentry errors

The error is:

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'key' at row 1 (Connection: mysql, SQL: insert into cache (expiration, key, value) values ...

The cache table is from laravel

I know your package is not for laravel so I'm not sure if your package should handle it or laravel side should handle it.

For now i lock it back to 4.8.06

@serbanghita
Copy link
Owner

@ziming Great feedback! This is because by default I use base64 as the cache key encoding, I'll switch to sha1 as default because it has fixed length

@serbanghita
Copy link
Owner

cache keys are now by default encoded with sha1

<?php
require __DIR__ . '/vendor/autoload.php';

use Detection\MobileDetect;
$detect = new MobileDetect();

$detect->setUserAgent('Mozilla/5.0 (iPad; CPU OS 14_7 like Mac OS X) ...');

$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();

var_dump($isMobile);
var_dump($isTablet);
var_dump($detect->getCache()->getKeys());
/home/sghita/work/opensource/Mobile-Detect-composer/index.php:12:boolean true
/home/sghita/work/opensource/Mobile-Detect-composer/index.php:13:boolean true
/home/sghita/work/opensource/Mobile-Detect-composer/index.php:14:
array (size=2)
  0 => string '6280fb7abac03980a424411ad2f2b75ac578c7ff' (length=40)
  1 => string 'd4372bb70edd52a39420cfd82a3c6452a96f25f9' (length=40)

@serbanghita
Copy link
Owner

@serbanghita
Copy link
Owner

@ziming what Laravel package are you using or you're injecting it manually?

@ChrBad
Copy link

ChrBad commented Dec 12, 2024

@serbanghita Thank you for providing the MobileDetect class. I'm not aware about where @ziming is using your class, but it is part of the laravel/jetstream extension (as I just run into the problems with 4.8.07 (Error: "Psr\Cache\CacheItemInterface not found") and fixed it by updating to 4.8.09)

@ziming
Copy link
Author

ziming commented Dec 12, 2024

im aware jetstream is using it but the codebase where i got the error from is not using laravel jetstream hence why i haven't answer

@serbanghita
Copy link
Owner

@ChrBad thanks for the update, yes I had a release problem with 4.8.07, see https://github.com/serbanghita/Mobile-Detect/blob/4.8.x/CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants