CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
MIT License
134 stars 67 forks source link

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

Closed Add00w closed 3 years ago

Add00w commented 3 years ago

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?

CodingAleCR commented 3 years ago

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.

stale[bot] commented 3 years ago

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.