You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Errors can also be caught and handled at the client level using CallAdapters. For example:
410
+
411
+
```dart
412
+
class ErrorAdapter<T> extends CallAdapter<Future<T>, Future<T>> {
413
+
@override
414
+
Future<T> adapt(Future<T> Function() call) {
415
+
try {
416
+
return call();
417
+
} catch (exception) {
418
+
// Handle the exception and throw whatever exception you want
419
+
throw MyCustomException();
420
+
}
421
+
}
422
+
}
423
+
424
+
@RestApi(callAdapter: ErrorAdapter)
425
+
abstract class RestClient {
426
+
factory RestClient(Dio dio, {String? baseUrl}) = _RestClient;
427
+
428
+
@GET('/user')
429
+
Future<User> getUser();
430
+
}
431
+
```
432
+
433
+
If you need to handle errors individually per API method instead of at the client level:
434
+
435
+
``` dart
436
+
@RestApi()
437
+
abstract class RestClient {
438
+
factory RestClient(Dio dio, {String? baseUrl}) = _RestClient;
439
+
440
+
@GET('/user')
441
+
@UseCallAdapter(ErrorAdapter)
442
+
Future<User> getUser();
443
+
}
444
+
```
445
+
446
+
447
+
408
448
### Relative API baseUrl
409
449
410
450
If you want to use a relative `baseUrl` value in the `RestApi` annotation of the `RestClient`, you need to specify a `baseUrl` in `dio.options.baseUrl`.
0 commit comments