8
8
use App \Backends \Common \ClientInterface as iClient ;
9
9
use App \Backends \Common \Context ;
10
10
use App \Backends \Common \GuidInterface as iGuid ;
11
+ use App \Backends \Common \Response ;
11
12
use App \Backends \Emby \Action \Backup ;
12
13
use App \Backends \Emby \Action \Export ;
13
14
use App \Backends \Emby \Action \GetIdentifier ;
42
43
/**
43
44
* Class EmbyClient
44
45
*
45
- * This class represents a client for the Emby backend.
46
- * It implements the iClient interface.
46
+ * This class is responsible for facilitating communication with Emby Server backend.
47
+ *
48
+ * @implements iClient
47
49
*/
48
50
class EmbyClient implements iClient
49
51
{
@@ -200,10 +202,7 @@ public function parseWebhook(ServerRequestInterface $request): iState
200
202
}
201
203
202
204
if (false === $ response ->isSuccessful ()) {
203
- throw new HttpException (
204
- ag ($ response ->extra , 'message ' , fn () => $ response ->error ->format ()),
205
- ag ($ response ->extra , 'http_code ' , 400 ),
206
- );
205
+ $ this ->throwError ($ response , HttpException::class, ag ($ response ->extra , 'http_code ' , 400 ));
207
206
}
208
207
209
208
return $ response ->response ;
@@ -229,7 +228,7 @@ public function pull(iImport $mapper, iDate|null $after = null): array
229
228
}
230
229
231
230
if (false === $ response ->isSuccessful ()) {
232
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
231
+ $ this -> throwError ( $ response );
233
232
}
234
233
235
234
return $ response ->response ;
@@ -252,7 +251,7 @@ public function backup(iImport $mapper, SplFileObject|null $writer = null, array
252
251
}
253
252
254
253
if (false === $ response ->isSuccessful ()) {
255
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
254
+ $ this -> throwError ( $ response );
256
255
}
257
256
258
257
return $ response ->response ;
@@ -279,7 +278,7 @@ public function export(iImport $mapper, QueueRequests $queue, iDate|null $after
279
278
}
280
279
281
280
if (false === $ response ->isSuccessful ()) {
282
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
281
+ $ this -> throwError ( $ response );
283
282
}
284
283
285
284
return $ response ->response ;
@@ -302,7 +301,7 @@ public function push(array $entities, QueueRequests $queue, iDate|null $after =
302
301
}
303
302
304
303
if (false === $ response ->isSuccessful ()) {
305
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
304
+ $ this -> throwError ( $ response );
306
305
}
307
306
308
307
return [];
@@ -326,7 +325,7 @@ public function progress(array $entities, QueueRequests $queue, iDate|null $afte
326
325
}
327
326
328
327
if (false === $ response ->isSuccessful ()) {
329
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
328
+ $ this -> throwError ( $ response );
330
329
}
331
330
332
331
return [];
@@ -349,7 +348,7 @@ public function search(string $query, int $limit = 25, array $opts = []): array
349
348
}
350
349
351
350
if (false === $ response ->isSuccessful ()) {
352
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
351
+ $ this -> throwError ( $ response );
353
352
}
354
353
355
354
return $ response ->response ;
@@ -367,7 +366,7 @@ public function searchId(string|int $id, array $opts = []): array
367
366
}
368
367
369
368
if (false === $ response ->isSuccessful ()) {
370
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
369
+ $ this -> throwError ( $ response );
371
370
}
372
371
373
372
return $ response ->response ;
@@ -385,7 +384,7 @@ public function getMetadata(string|int $id, array $opts = []): array
385
384
);
386
385
387
386
if (false === $ response ->isSuccessful ()) {
388
- throw new RuntimeException (message: $ response -> error -> format (), previous: $ response-> error -> previous );
387
+ $ this -> throwError ( $ response );
389
388
}
390
389
391
390
return $ response ->response ;
@@ -403,7 +402,7 @@ public function getLibrary(string|int $id, array $opts = []): array
403
402
}
404
403
405
404
if (false === $ response ->isSuccessful ()) {
406
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
405
+ $ this -> throwError ( $ response );
407
406
}
408
407
409
408
return $ response ->response ;
@@ -439,9 +438,7 @@ public function getUsersList(array $opts = []): array
439
438
$ this ->logger ->log ($ response ->error ->level (), $ response ->error ->message , $ response ->error ->context );
440
439
}
441
440
442
- throw new RuntimeException (
443
- ag ($ response ->extra , 'message ' , fn () => $ response ->error ->format ())
444
- );
441
+ $ this ->throwError ($ response );
445
442
}
446
443
447
444
return $ response ->response ;
@@ -475,7 +472,7 @@ public function listLibraries(array $opts = []): array
475
472
}
476
473
477
474
if (false === $ response ->isSuccessful ()) {
478
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
475
+ $ this -> throwError ( $ response );
479
476
}
480
477
481
478
return $ response ->response ;
@@ -493,7 +490,7 @@ public function getInfo(array $opts = []): array
493
490
}
494
491
495
492
if (false === $ response ->isSuccessful ()) {
496
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
493
+ $ this -> throwError ( $ response );
497
494
}
498
495
499
496
return $ response ->response ;
@@ -511,7 +508,7 @@ public function getVersion(array $opts = []): string
511
508
}
512
509
513
510
if (false === $ response ->isSuccessful ()) {
514
- throw new RuntimeException ( ag ( $ response -> extra , ' message ' , fn () => $ response-> error -> format ()) );
511
+ $ this -> throwError ( $ response );
515
512
}
516
513
517
514
return $ response ->response ;
@@ -524,4 +521,21 @@ public static function manage(array $backend, array $opts = []): array
524
521
{
525
522
return Container::get (EmbyManage::class)->manage (backend: $ backend , opts: $ opts );
526
523
}
524
+
525
+ /**
526
+ * Throws an exception with the specified message and previous exception.
527
+ *
528
+ * @template T
529
+ * @param Response $response The response object containing the error details.
530
+ * @param class-string<T> $className The exception class name.
531
+ * @param int $code The exception code.
532
+ */
533
+ private function throwError (Response $ response , string $ className = RuntimeException::class, int $ code = 0 ): void
534
+ {
535
+ throw new $ className (
536
+ message: ag ($ response ->extra , 'message ' , fn () => $ response ->error ->format ()),
537
+ code: $ code ,
538
+ previous: $ response ->error ->previous
539
+ );
540
+ }
527
541
}
0 commit comments