import 'package:dio/dio.dart'; import 'package:http2/http2.dart'; import 'package:isekai_wiki/api/base_api.dart'; import 'package:isekai_wiki/api/mw/mw_api.dart'; class ErrorUtils { static String getErrorMessage(dynamic err) { if (err is MWApiEmptyBodyException) { return "加载的数据为空"; } else if (err is MWApiErrorException) { return err.info ?? err.code; } else if (err is HttpResponseException) { return "服务器错误:${err.statusCode} ${err.statusText}"; } else if (err is TypeError) { // JSON解析错误 return "服务器返回的数据不正确"; } else if (err is DioError) { if (err.type == DioErrorType.connectTimeout || err.type == DioErrorType.sendTimeout || err.type == DioErrorType.receiveTimeout) { return "链接超时,请重试"; } else if (err.type == DioErrorType.response) { return "HTTP错误:${err.response?.statusCode} ${err.response?.statusMessage}"; } else if (err.type == DioErrorType.other) { var innerErr = err.error! as Exception; if (innerErr is StreamTransportException) { return innerErr.message; } } return err.message; } return err.toString(); } }