@@ -72,6 +72,26 @@ public function getServiceName(): string {
7272 }
7373 }
7474
75+ /**
76+ * @param mixed $models
77+ * @return boolean
78+ */
79+ private function isModelListValid ($ models ): bool {
80+ if (!is_array ($ models ) || !array_is_list ($ models )) {
81+ return false ;
82+ }
83+ if (count ($ models ) === 0 ) {
84+ return false ;
85+ }
86+ foreach ($ models as $ model ) {
87+ if (!isset ($ model ['id ' ])) {
88+ return false ;
89+ }
90+ }
91+
92+ return true ;
93+ }
94+
7595 /**
7696 * @param string $userId
7797 * @return array|string[]
@@ -111,11 +131,22 @@ public function getModels(string $userId): array {
111131 $ this ->areCredsValid = false ;
112132 throw $ e ;
113133 }
114- if (! isset ($ modelsResponse ['data ' ])) {
134+ if (isset ($ modelsResponse ['error ' ])) {
115135 $ this ->logger ->warning ('Error retrieving models: ' . \json_encode ($ modelsResponse ));
116136 $ this ->areCredsValid = false ;
117- throw new Exception ($ this ->l10n ->t ('Unknown models error ' ), Http::STATUS_INTERNAL_SERVER_ERROR );
137+ throw new Exception ($ modelsResponse ['error ' ], Http::STATUS_INTERNAL_SERVER_ERROR );
138+ }
139+ if (!isset ($ modelsResponse ['data ' ])) {
140+ // also consider responses without 'data' as valid
141+ $ modelsResponse = ['data ' => $ modelsResponse ];
118142 }
143+
144+ if (!$ this ->isModelListValid ($ modelsResponse ['data ' ])) {
145+ $ this ->logger ->warning ('Invalid models response: ' . \json_encode ($ modelsResponse ));
146+ $ this ->areCredsValid = false ;
147+ throw new Exception ($ this ->l10n ->t ('Invalid models response received ' ), Http::STATUS_INTERNAL_SERVER_ERROR );
148+ }
149+
119150 $ cache ->set ($ cacheKey , $ modelsResponse , Application::MODELS_CACHE_TTL );
120151 $ this ->modelsMemoryCache = $ modelsResponse ;
121152 $ this ->areCredsValid = true ;
0 commit comments