feat:更新网络工具类返回的异常错误

This commit is contained in:
2025-11-25 19:33:00 +08:00
parent 5c6e772eff
commit c0f12dab7d
2 changed files with 23 additions and 19 deletions

View File

@@ -108,7 +108,7 @@ class HttpUtil {
// 没有转换器时尝试直接返回(可能不安全,建议提供转换器) // 没有转换器时尝试直接返回(可能不安全,建议提供转换器)
return response.data; return response.data;
} catch (e) { } catch (e) {
rethrow; throw Exception(_handleError(e));
} }
} }
@@ -171,7 +171,7 @@ class HttpUtil {
); );
// 错误处理 // 错误处理
void _handleError(dynamic error) { String _handleError(dynamic error) {
String errorMessage = '未知错误'; String errorMessage = '未知错误';
if (error is DioException) { if (error is DioException) {
switch (error.type) { switch (error.type) {
@@ -218,7 +218,7 @@ class HttpUtil {
} }
logger.e(errorMessage); logger.e(errorMessage);
// showErrorToast(errorMessage); return errorMessage;
} }
String _getHttpErrorMessage(int statusCode) { String _getHttpErrorMessage(int statusCode) {

View File

@@ -1,6 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart'; import 'package:logger/logger.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
@@ -9,6 +10,12 @@ late Logger logger;
// 初始化日志 // 初始化日志
Future<void> initLogger() async { Future<void> initLogger() async {
if (kIsWeb) {
logger = Logger(
printer: SimplePrinter(printTime: true),
output: ConsoleOutput(),
);
} else {
final directory = await getExternalStorageDirectory(); final directory = await getExternalStorageDirectory();
final logFile = File('${directory?.path}/logs/app.log'); final logFile = File('${directory?.path}/logs/app.log');
@@ -20,11 +27,8 @@ Future<void> initLogger() async {
printer: SimplePrinter(printTime: true), printer: SimplePrinter(printTime: true),
output: MultiOutput([ output: MultiOutput([
ConsoleOutput(), ConsoleOutput(),
FileOutput( FileOutput(file: logFile, overrideExisting: true, encoding: utf8),
file: logFile,
overrideExisting: true,
encoding: utf8
),
]), ]),
); );
}
} }