error_handler 1.0.4 copy "error_handler: ^1.0.4" to clipboard
error_handler: ^1.0.4 copied to clipboard

outdated

error handler for all http client in dart like dio, chopper http and more

Welcome to ErrorHandler, error handler with type-safety/streaming/freezed-functionality/cover-all-clients-exceptions

Index #

Motivation #

try{}catch(e){} or then((){}).catch((){}) make code hard to read and modify

Functionality #

  • handle all api possible state init/loading/data/error easily
  • logging the state states
  • built above freezed
  • **work with any http client like chopper,dio and more **

How to use #

install #

For a Flutter project:

flutter pub add error_handler
flutter pub add dio

For a Dart project:

flutter pub add error_handler
flutter pub add dio

Example #

import 'package:dio/dio.dart';
import 'package:error_handler/error_handler.dart';

FutureResponse<Post> getPost() async {
  const path = "https://jsonplaceholder.typicode.com/posts/1";
  final response = await Dio().get(path);
  return response.convert(Post.fromJson);
}

/// wrap the api call with [ErrorHandler.future]
Future<void> main() async {
  final state = await ErrorHandler().future(getPost);

  state.whenOrNull(
    data: (post, response) {
      print("title: ${post.title}");
    },
    error: (error) {
      print(getErrorMessage(error));
    },
  );
}
  • errorHandler.future((){...}) return safe data

.stream provide Loading and Idle State full example #

/// wrap the api call with [ErrorHandler.stream]
///
/// to handle loading state
void main() {
  final event = errorHandler.stream(getPost);

  event.listen((state) {
    state.whenOrNull(
      
      loading: () {
        print("loading");
      },
      data: (post, response) {
        print("title: ${post.title}");
      },
      error: (error) {
        print(getErrorMessage(error));
      },
    );
  });
}
  • errorHandler.stream((){...}) first return loading and then return data or error

Advance login example for post request full example #

/// First create API call
FutureResponse<User> login(String gmail, String password) async {
  final body = {"gmail": gmail, "password": password};

  final response = await Dio().post("http://your.domain.com/login", data: body);

  return response.convert(User.fromJson);
}

/// Wrap it with [ErrorHandler.stream] or [ErrorHandler.future]
StreamState<User> safeLogin(String gmail, String password) =>
    errorHandler.stream(() => login(gmail, password));

void main() {
  final event = safeLogin("example@domain.com", "password");
  event.listen((event) {
    event.whenOrNull(
      loading: () {
        print("please wait");
      },
      data: (data, response) {
        print("login successfully");
        print(data);
      },
      error: (exception) {
        print(exception.defaultErrorMessage());
      },
    );
  });
}

Contribute #

please fork the repo and be part of maintainers team ❤️‍🔥

Credits 🙏 #

Freezed

17
likes
0
pub points
52%
popularity

Publisher

verified publishermatheer.com

error handler for all http client in dart like dio, chopper http and more

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

chopper, dio, freezed_annotation

More

Packages that depend on error_handler