http_handler.dart 828 B

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