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

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 **
Before After
before after

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 #

.future get api result directly full 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());
      },
    );
  });
}

Other Example #

Contribute #

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

Credits 🙏 #

Freezed

16
likes
130
points
46
downloads

Publisher

verified publishermatheer.com

Weekly Downloads

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

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

chopper, dio, freezed_annotation

More

Packages that depend on error_handler