Files
social-app/apps/lib/core/api/api_exception.dart
T
qzl 8539f05a66 feat: 增强 HomeScreen 录音交互与 ChatBloc 状态管理
- 新增录音启动延迟处理,解决权限未就绪时的竞态问题
- 实现历史分页滚动位置保持,提升加载体验
- 添加文本输入框点击键盘显示与焦点管理
- 优化 ChatBloc provider 到 MultiBlocProvider 支持
- 修复 ApiException 429 错误详情解析(支持 JSON 字符串 body)
- 改进 LocalNotificationService 精确闹钟权限请求
- 优化 UiSchemaRenderer GridView children 生成
- 支持导航 action 的 replace 参数
- 移除 Agent router 速率限制逻辑(_allow_run_request, _allow_transcribe_request)
- 补充相关单元测试与集成测试
2026-03-18 17:03:22 +08:00

124 lines
3.5 KiB
Dart

import 'dart:convert';
import 'package:dio/dio.dart';
abstract class ApiException implements Exception {
final String message;
final int? statusCode;
const ApiException(this.message, {this.statusCode});
@override
String toString() => message;
factory ApiException.fromDioError(Object error) {
if (error is ApiException) return error;
if (error is DioException) {
final response = error.response;
final statusCode = response?.statusCode;
final data = response?.data;
String detail;
final decodedData = _normalizeErrorData(data);
if (decodedData is Map<String, dynamic>) {
detail =
(decodedData['detail'] ??
decodedData['message'] ??
decodedData['error'])
?.toString() ??
'请求失败';
} else {
detail = _networkErrorMessage(error);
}
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('网络错误');
}
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;
}
static String _localizeError(String detail, int? statusCode) {
if (statusCode == 403) {
return '没有权限执行此操作';
}
if (statusCode == 404) {
return '请求的资源不存在';
}
if (statusCode == 429) {
final normalized = detail.trim();
if (normalized.isEmpty || normalized == '请求失败') {
return '请求过于频繁,请稍后再试';
}
return detail;
}
if (statusCode != null && statusCode >= 500) {
return '服务器错误,请稍后再试';
}
return detail;
}
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 '请求失败';
}
}
class ServerException extends ApiException {
const ServerException(super.message, {super.statusCode});
}
class UnauthorizedException extends ApiException {
const UnauthorizedException([super.message = '请重新登录'])
: super(statusCode: 401);
}
class ValidationException extends ApiException {
final Map<String, dynamic>? errors;
const ValidationException(super.message, {this.errors, super.statusCode});
}