http_handler.dart 822 B

12345678910111213141516171819202122232425262728293031323334
  1. import 'dart:async';
  2. import '../base/base_response.dart';
  3. class HttpHandler {
  4. HttpHandler._();
  5. static FutureOr<T> Function(BaseResponse<T> value) handle<T>(
  6. bool allowEmptyData) {
  7. return (BaseResponse<T> response) {
  8. if (response.code == 0) {
  9. if (response.data != null || allowEmptyData) {
  10. return response.data == null ? Future.value() : response.data!;
  11. } else {
  12. throw Exception('data is null');
  13. }
  14. } else {
  15. throw ServerErrorException(response.code, response.message);
  16. }
  17. };
  18. }
  19. }
  20. class ServerErrorException implements Exception {
  21. final int? code;
  22. final String? message;
  23. ServerErrorException(this.code, this.message);
  24. @override
  25. String toString() {
  26. return 'ServerErrorException: code: $code, message: $message';
  27. }
  28. }