2026-03-18 17:03:22 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2026-02-25 18:00:02 +08:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
|
2026-02-25 14:36:03 +08:00
|
|
|
abstract class ApiException implements Exception {
|
|
|
|
|
final String message;
|
|
|
|
|
final int? statusCode;
|
|
|
|
|
|
|
|
|
|
const ApiException(this.message, {this.statusCode});
|
|
|
|
|
|
2026-03-11 20:51:56 +08:00
|
|
|
@override
|
|
|
|
|
String toString() => message;
|
|
|
|
|
|
2026-02-25 14:36:03 +08:00
|
|
|
factory ApiException.fromDioError(Object error) {
|
|
|
|
|
if (error is ApiException) return error;
|
2026-02-25 18:00:02 +08:00
|
|
|
if (error is DioException) {
|
|
|
|
|
final response = error.response;
|
|
|
|
|
final statusCode = response?.statusCode;
|
|
|
|
|
final data = response?.data;
|
|
|
|
|
|
|
|
|
|
String detail;
|
2026-03-18 17:03:22 +08:00
|
|
|
final decodedData = _normalizeErrorData(data);
|
|
|
|
|
|
|
|
|
|
if (decodedData is Map<String, dynamic>) {
|
2026-02-25 18:00:02 +08:00
|
|
|
detail =
|
2026-03-18 17:03:22 +08:00
|
|
|
(decodedData['detail'] ??
|
|
|
|
|
decodedData['message'] ??
|
|
|
|
|
decodedData['error'])
|
|
|
|
|
?.toString() ??
|
2026-02-25 18:00:02 +08:00
|
|
|
'请求失败';
|
|
|
|
|
} else {
|
2026-03-17 00:13:41 +08:00
|
|
|
detail = _networkErrorMessage(error);
|
2026-02-25 18:00:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final localized = _localizeError(detail, statusCode);
|
|
|
|
|
|
|
|
|
|
if (statusCode == 401) {
|
|
|
|
|
return UnauthorizedException(localized);
|
|
|
|
|
}
|
|
|
|
|
if (statusCode == 422) {
|
|
|
|
|
return ValidationException(
|
|
|
|
|
localized,
|
|
|
|
|
errors: data,
|
|
|
|
|
statusCode: statusCode,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return ServerException(localized, statusCode: statusCode);
|
|
|
|
|
}
|
|
|
|
|
return const ServerException('网络错误');
|
2026-02-25 14:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 17:03:22 +08:00
|
|
|
static Map<String, dynamic>? _normalizeErrorData(dynamic data) {
|
|
|
|
|
if (data is Map<String, dynamic>) {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
if (data is Map) {
|
|
|
|
|
return data.map((key, value) => MapEntry(key.toString(), value));
|
|
|
|
|
}
|
|
|
|
|
if (data is String && data.trim().isNotEmpty) {
|
|
|
|
|
try {
|
|
|
|
|
final decoded = jsonDecode(data);
|
|
|
|
|
if (decoded is Map<String, dynamic>) {
|
|
|
|
|
return decoded;
|
|
|
|
|
}
|
|
|
|
|
if (decoded is Map) {
|
|
|
|
|
return decoded.map((key, value) => MapEntry(key.toString(), value));
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 18:00:02 +08:00
|
|
|
static String _localizeError(String detail, int? statusCode) {
|
|
|
|
|
if (statusCode == 403) {
|
|
|
|
|
return '没有权限执行此操作';
|
|
|
|
|
}
|
|
|
|
|
if (statusCode == 404) {
|
|
|
|
|
return '请求的资源不存在';
|
|
|
|
|
}
|
|
|
|
|
if (statusCode == 429) {
|
2026-03-18 17:03:22 +08:00
|
|
|
final normalized = detail.trim();
|
|
|
|
|
if (normalized.isEmpty || normalized == '请求失败') {
|
|
|
|
|
return '请求过于频繁,请稍后再试';
|
|
|
|
|
}
|
|
|
|
|
return detail;
|
2026-02-25 18:00:02 +08:00
|
|
|
}
|
|
|
|
|
if (statusCode != null && statusCode >= 500) {
|
|
|
|
|
return '服务器错误,请稍后再试';
|
|
|
|
|
}
|
|
|
|
|
return detail;
|
|
|
|
|
}
|
2026-03-17 00:13:41 +08:00
|
|
|
|
|
|
|
|
static String _networkErrorMessage(DioException error) {
|
|
|
|
|
if (error.type == DioExceptionType.connectionTimeout ||
|
|
|
|
|
error.type == DioExceptionType.sendTimeout ||
|
|
|
|
|
error.type == DioExceptionType.receiveTimeout) {
|
|
|
|
|
return '网络超时,请确认手机与服务端在同一网络后重试';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error.type == DioExceptionType.connectionError ||
|
|
|
|
|
error.type == DioExceptionType.unknown) {
|
|
|
|
|
return '无法连接服务器。请在 iPhone 设置中为本应用开启“无线数据(WLAN与蜂窝网络)”,并确认本地网络权限已开启。';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '请求失败';
|
|
|
|
|
}
|
2026-02-25 14:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ServerException extends ApiException {
|
|
|
|
|
const ServerException(super.message, {super.statusCode});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UnauthorizedException extends ApiException {
|
2026-02-25 18:00:02 +08:00
|
|
|
const UnauthorizedException([super.message = '请重新登录'])
|
2026-02-25 14:36:03 +08:00
|
|
|
: super(statusCode: 401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ValidationException extends ApiException {
|
|
|
|
|
final Map<String, dynamic>? errors;
|
|
|
|
|
const ValidationException(super.message, {this.errors, super.statusCode});
|
|
|
|
|
}
|