Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/masreplay/error_handler.dart

error handler for all http client in dart like Dio, Chopper, Http and more
https://github.com/masreplay/error_handler.dart

dart dio error-handling flutter freezed http requests retrofit

Last synced: 17 days ago
JSON representation

error handler for all http client in dart like Dio, Chopper, Http and more

Awesome Lists containing this project

README

        

Welcome to [ErrorHandler](https://pub.dev/packages/error_handler), error handler with type-safety/streaming/freezed-functionality/cover-all-clients-exceptions

## Index
- [Motivation](#motivation)
- [Functionality](#functionality)
- [How to use](#how-to-use)
- [install](#install)
- [Example](#example)
- [```.future``` get api result directly full example](#future-get-api-result-directly-full-example)
- [```.stream``` provide Loading and Idle State full example](#stream-provide-loading-and-idle-state-full-example)
- [Advance login example for post request full example](#advance-login-example-for-post-request-full-example)
- [Other Example](#other-example)
- [Contribute](#contribute)
- [Credits 🙏](#credits-)

# 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](https://github.com/rrousselGit/freezed)
- **work with any http client like [chopper](https://pub.dev/packages/chopper),[dio](https://pub.dev/packages/chopper) and more **

| Before | After |
| ------------------------------- | ------------------------------ |
| ![before](readme/before.png) | ![after](readme/after.png) |

# How to use
## install

For a Flutter project:

```shell
flutter pub add error_handler
flutter pub add dio
```

For a Dart project:

```shell
flutter pub add error_handler
flutter pub add dio
```

## Example
### ```.future``` get api result directly [full example](example/error_handler_example.dart)
```dart
import 'package:dio/dio.dart';
import 'package:error_handler/error_handler.dart';

FutureResponse 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 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](example/error_handler_stream.dart)
```dart
/// 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](example/login_example.dart)
```dart
/// First create API call
FutureResponse 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 safeLogin(String gmail, String password) =>
errorHandler.stream(() => login(gmail, password));

void main() {
final event = safeLogin("[email protected]", "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
- [status code 401 unauthorized](example/unauthorized_401_example.dart)
- [status code 401 unauthorized advance example with ErrorFilter](example/unauthorized_401_example.dart)

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

## Credits 🙏
[Freezed](https://github.com/rrousselGit/freezed)