Browse Source

Enhance error detection and fallback triggering

- Added more comprehensive error detection for API responses
- Check for empty/null responses, JSON decode failures, and various error strings
- Ensure fallback method is called in all failure scenarios
- Added HTTP response failure handling
- Made fallback more aggressive to handle edge cases
mgomon 8 months ago
parent
commit
f4dbf5abdc
1 changed files with 22 additions and 6 deletions
  1. 22 6
      api/homepage/userWatchStats.php

+ 22 - 6
api/homepage/userWatchStats.php

@@ -715,15 +715,27 @@ trait HomepageUserWatchStats
             $response = Requests::get($apiURL, [], $options);
             
             if ($response->success) {
-                $data = json_decode($response->body, true);
+                $responseBody = $response->body;
                 
-                // Check if response contains an error (SQLite exception)
-                if (is_string($data) || (is_array($data) && isset($data['error'])) || 
-                    (is_string($response->body) && strpos($response->body, 'SQLiteException') !== false)) {
+                // Check if response contains SQLite exception or other error indicators
+                if (is_string($responseBody) && (
+                    strpos($responseBody, 'SQLiteException') !== false ||
+                    strpos($responseBody, 'error') !== false ||
+                    strpos($responseBody, 'Error') !== false ||
+                    !trim($responseBody) || 
+                    $responseBody === 'null'
+                )) {
                     // Fall back to recently created items if DatePlayed sorting fails
                     return $this->getEmbyFallbackMostWatched($url, $token);
                 }
                 
+                $data = json_decode($responseBody, true);
+                
+                // Check if JSON decode failed or returned invalid data
+                if ($data === null || !is_array($data) || !isset($data['Items'])) {
+                    return $this->getEmbyFallbackMostWatched($url, $token);
+                }
+                
                 $items = $data['Items'] ?? [];
                 
                 $mostWatched = [];
@@ -746,13 +758,17 @@ trait HomepageUserWatchStats
                 }
                 
                 return $mostWatched;
+            } else {
+                // HTTP request failed, use fallback
+                return $this->getEmbyFallbackMostWatched($url, $token);
             }
         } catch (Exception $e) {
-            // Fall back to recently created items if DatePlayed sorting fails
+            // Any exception triggers fallback
             return $this->getEmbyFallbackMostWatched($url, $token);
         }
         
-        return [];
+        // Fallback if we somehow get here
+        return $this->getEmbyFallbackMostWatched($url, $token);
     }
     
     /**