Browse Source

Fix exception handling for Emby DatePlayed SQLite errors

- Improved error detection to catch SQLite exceptions from DatePlayed sorting
- Added fallback to DateCreated sorting when DatePlayed fails
- Expanded exception handling from Requests_Exception to all Exception types
- Added check for empty DatePlayed results to trigger fallback
- Ensures most_watched array gets populated even when DatePlayed API fails
mgomon 8 months ago
parent
commit
9437dd2b01
1 changed files with 14 additions and 1 deletions
  1. 14 1
      api/homepage/userWatchStats.php

+ 14 - 1
api/homepage/userWatchStats.php

@@ -716,6 +716,14 @@ trait HomepageUserWatchStats
             
             if ($response->success) {
                 $data = json_decode($response->body, true);
+                
+                // 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)) {
+                    // Fall back to recently created items if DatePlayed sorting fails
+                    return $this->getEmbyFallbackMostWatched($url, $token);
+                }
+                
                 $items = $data['Items'] ?? [];
                 
                 $mostWatched = [];
@@ -732,9 +740,14 @@ trait HomepageUserWatchStats
                     }
                 }
                 
+                // If no items with DatePlayed, fall back to recently created
+                if (empty($mostWatched)) {
+                    return $this->getEmbyFallbackMostWatched($url, $token);
+                }
+                
                 return $mostWatched;
             }
-        } catch (Requests_Exception $e) {
+        } catch (Exception $e) {
             // Fall back to recently created items if DatePlayed sorting fails
             return $this->getEmbyFallbackMostWatched($url, $token);
         }