| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- import 'dart:async';
- import 'package:location/data/repositories/account_repository.dart';
- import '../base/base_response.dart';
- import '../data/consts/error_code.dart';
- class HttpHandler {
- HttpHandler._();
- static FutureOr<T> Function(BaseResponse<T> value) handle<T>(
- bool allowEmptyData) {
- return (BaseResponse<T> response) {
- if (response.code == 0) {
- if (response.data != null || allowEmptyData) {
- return response.data == null ? Future.value() : response.data!;
- } else {
- throw Exception('data is null');
- }
- } else {
- if (response.code == ErrorCode.noLoginError) {
- AccountRepository.getInstance().logout();
- }
- throw ServerErrorException(response.code, response.message);
- }
- };
- }
- }
- class ServerErrorException implements Exception {
- final int? code;
- final String? message;
- ServerErrorException(this.code, this.message);
- @override
- String toString() {
- return 'ServerErrorException: code: $code, message: $message';
- }
- }
|