CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to catch all socket exceptions in place for this package?

Add00w opened this issue Β· comments

I use this package for intercepting all my requests and I need way to handle all internet exceptions in one place not in all repositories as this example shows

class WeatherRepository {

    Future<Map<String, dynamic>> fetchCityWeather(int id) async {
    var parsedWeather;
    try {
      WeatherApiInterceptor http = HttpWithInterceptor.build(interceptors: [
          Logger(),
      ]);
      final response =
          await http.get("$baseUrl/weather".toUri(), params: {'id': "$id"});
      if (response.statusCode == 200) {
        parsedWeather = json.decode(response.body);
      } else {
        return Future.error(
          "Error while fetching.",
          StackTrace.fromString("${response.body}"),
        );
      }
    } on SocketException {
      return Future.error('No Internet connection πŸ˜‘');
    } on FormatException {
      return Future.error('Bad response format πŸ‘Ž');
    } on Exception {
      return Future.error('Unexpected error 😒');
    }

    return parsedWeather;
  }

}

can any one tell me how to at one place?

Hi, as of right now the only way to achieve this would be to wrap your interceptor within another class that implements the behavior you desire, in fact, the whole http is built this way so you can customize your needs.

The wrapper class would be the one that you use on the different repositories and also handle all internet exceptions.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.