Chopper错误处理完全手册异常捕获、重试机制和超时配置【免费下载链接】chopperChopper is an http client generator using source_gen and inspired from Retrofit.项目地址: https://gitcode.com/gh_mirrors/ch/chopper在Flutter和Dart开发中Chopper作为一个强大的HTTP客户端生成器提供了完整的错误处理解决方案。本文将详细介绍Chopper的异常捕获机制、重试策略和超时配置帮助开发者构建更加健壮的网络请求应用。为什么需要专业的错误处理网络请求在移动应用中常常面临各种挑战不稳定的网络连接、服务器超时、认证失败等。Chopper通过内置的错误处理机制让开发者能够优雅地处理这些异常情况提升应用的用户体验和稳定性。Chopper异常类型详解1. ChopperException - 基础异常类ChopperException是所有Chopper相关异常的基类它包含了详细的错误信息、请求和响应数据try { final response await service.getData(); } on ChopperException catch (e) { print(错误信息: ${e.message}); print(请求详情: ${e.request}); print(响应详情: ${e.response}); }2. ChopperHttpException - HTTP错误处理当HTTP响应状态码不在200-299范围内时Chopper会抛出ChopperHttpExceptiontry { final response await service.getData(); return response.bodyOrThrow; } on ChopperHttpException catch (e) { // 处理HTTP错误如404、500等 print(HTTP错误: ${e.response.statusCode}); print(错误详情: ${e.response.error}); }3. ChopperTimeoutException - 超时异常网络请求超时是移动应用中的常见问题Chopper提供了专门的超时异常处理try { final response await service.getData(); } on ChopperTimeoutException catch (e) { print(请求超时: ${e.duration}); // 实现重试逻辑或显示用户友好的错误信息 }拦截器中的错误处理拦截器是Chopper错误处理的核心组件可以在请求链的任何阶段处理异常请求拦截器错误处理在请求发送前进行验证和错误拦截class AuthInterceptor implements Interceptor { override FutureOrResponseBodyType interceptBodyType(ChainBodyType chain) async { final token await _getToken(); if (token null) { // 抛出异常中断请求链 throw ChopperException(未找到认证令牌); } final request applyHeader( chain.request, Authorization, Bearer $token, ); return chain.proceed(request); } }响应拦截器错误处理在响应返回后统一处理错误状态class ErrorHandlingInterceptor implements Interceptor { override FutureOrResponseBodyType interceptBodyType(ChainBodyType chain) async { try { final response await chain.proceed(chain.request); // 检查HTTP状态码 if (!response.isSuccessful) { // 可以在这里进行统一错误处理 _handleHttpError(response); // 或者抛出异常让上层处理 throw ChopperHttpException(response); } return response; } catch (e) { // 捕获并处理所有异常 _logError(e); rethrow; // 重新抛出让上层处理 } } }实现智能重试机制基础重试拦截器通过拦截器实现简单的重试逻辑class RetryInterceptor implements Interceptor { final int maxRetries; final Duration retryDelay; RetryInterceptor({this.maxRetries 3, this.retryDelay const Duration(seconds: 1)}); override FutureResponseBodyType interceptBodyType(ChainBodyType chain) async { for (int attempt 0; attempt maxRetries; attempt) { try { return await chain.proceed(chain.request); } on SocketException catch (_) { // 网络错误时重试 if (attempt maxRetries) { await Future.delayed(retryDelay * (attempt 1)); continue; } rethrow; } on ChopperTimeoutException catch (_) { // 超时时重试 if (attempt maxRetries) { await Future.delayed(retryDelay * (attempt 1)); continue; } rethrow; } on ChopperHttpException catch (e) { // 特定HTTP状态码重试如503服务不可用 if (e.response.statusCode 503 attempt maxRetries) { await Future.delayed(retryDelay * (attempt 1)); continue; } rethrow; } } throw ChopperException(达到最大重试次数); } }指数退避重试策略对于网络不稳定的情况使用指数退避策略class ExponentialBackoffRetryInterceptor implements Interceptor { final int maxRetries; final Duration initialDelay; final double backoffFactor; ExponentialBackoffRetryInterceptor({ this.maxRetries 3, this.initialDelay const Duration(seconds: 1), this.backoffFactor 2.0, }); override FutureResponseBodyType interceptBodyType(ChainBodyType chain) async { Duration delay initialDelay; for (int attempt 0; attempt maxRetries; attempt) { try { return await chain.proceed(chain.request); } catch (e) { if (_shouldRetry(e) attempt maxRetries) { await Future.delayed(delay); delay Duration( milliseconds: (delay.inMilliseconds * backoffFactor).toInt(), ); continue; } rethrow; } } throw ChopperException(重试失败); } bool _shouldRetry(dynamic error) { return error is SocketException || error is ChopperTimeoutException || (error is ChopperHttpException error.response.statusCode 500); } }超时配置与管理客户端级超时配置通过自定义http.Client实现全局超时设置final chopper ChopperClient( baseUrl: Uri.parse(https://api.example.com), client: http.Client() ..connectionTimeout Duration(seconds: 10) ..idleTimeout Duration(seconds: 5), interceptors: [ HttpLoggingInterceptor(), ], services: [ MyService.create(), ], );请求级超时控制为特定请求设置独立的超时时间class TimeoutInterceptor implements Interceptor { final Duration defaultTimeout; TimeoutInterceptor({this.defaultTimeout const Duration(seconds: 30)}); override FutureResponseBodyType interceptBodyType(ChainBodyType chain) async { final request chain.request; // 从请求头获取自定义超时时间 final timeoutHeader request.headers[X-Timeout]; final timeout timeoutHeader ! null ? Duration(seconds: int.parse(timeoutHeader)) : defaultTimeout; try { return await chain.proceed(request) .timeout(timeout, onTimeout: () { throw ChopperTimeoutException(请求超时, timeout); }); } on TimeoutException catch (e) { throw ChopperTimeoutException(请求超时: $timeout, timeout); } } }错误转换与统一处理错误响应转换器将错误响应转换为统一的错误模型class ErrorResponseConverter implements ErrorConverter { override FutureOrResponse convertErrorBodyType, InnerType(Response response) { // 解析错误响应体 final errorBody _parseErrorBody(response); // 创建统一的错误模型 final error ApiError( statusCode: response.statusCode, message: errorBody[message] ?? 未知错误, code: errorBody[code], timestamp: DateTime.now(), ); // 返回包含错误信息的响应 return response.copyWithBodyType(error: error); } MapString, dynamic _parseErrorBody(Response response) { try { if (response.body is String) { return jsonDecode(response.body as String); } return {}; } catch (_) { return {}; } } }全局错误处理器创建统一的错误处理中心class ErrorHandler { static void handleError(dynamic error, StackTrace stackTrace) { if (error is ChopperHttpException) { _handleHttpError(error); } else if (error is ChopperTimeoutException) { _handleTimeoutError(error); } else if (error is ChopperException) { _handleChopperError(error); } else { _handleUnknownError(error, stackTrace); } } static void _handleHttpError(ChopperHttpException error) { final statusCode error.response.statusCode; switch (statusCode) { case 401: // 处理认证错误 _showLoginRequired(); break; case 403: // 处理权限错误 _showPermissionDenied(); break; case 404: // 处理资源不存在 _showResourceNotFound(); break; case 500: // 处理服务器错误 _showServerError(); break; default: _showGenericError(HTTP错误: $statusCode); } } static void _handleTimeoutError(ChopperTimeoutException error) { // 显示网络超时提示 _showNetworkTimeout(error.duration); } }最佳实践与配置示例完整的错误处理配置final chopper ChopperClient( baseUrl: Uri.parse(https://api.example.com), client: http.Client() ..connectionTimeout Duration(seconds: 15) ..idleTimeout Duration(seconds: 10), interceptors: [ // 认证拦截器 AuthInterceptor(), // 日志拦截器 HttpLoggingInterceptor(), // 重试拦截器 ExponentialBackoffRetryInterceptor( maxRetries: 3, initialDelay: Duration(seconds: 1), backoffFactor: 2.0, ), // 超时拦截器 TimeoutInterceptor(defaultTimeout: Duration(seconds: 30)), // 错误处理拦截器 ErrorHandlingInterceptor(), ], errorConverter: ErrorResponseConverter(), services: [ ApiService.create(), ], );服务层错误处理class ApiService { final ChopperClient _client; ApiService(this._client); FutureDataModel fetchData() async { try { final response await _client .getServiceMyService() .getData(); if (response.isSuccessful) { return response.body!; } else { // 处理业务逻辑错误 throw ApiException( message: 数据获取失败, code: response.statusCode, data: response.error, ); } } on ChopperHttpException catch (e) { // 转换HTTP错误为业务错误 throw ApiException.fromHttpError(e); } on ChopperTimeoutException catch (e) { // 处理超时错误 throw NetworkException(请求超时请检查网络连接); } catch (e) { // 处理其他未知错误 throw UnknownException(未知错误: $e); } } }监控与日志记录错误日志记录class ErrorLoggerInterceptor implements Interceptor { final Logger _logger Logger(ChopperError); override FutureResponseBodyType interceptBodyType(ChainBodyType chain) async { final startTime DateTime.now(); try { final response await chain.proceed(chain.request); final duration DateTime.now().difference(startTime); _logger.info(请求成功: ${chain.request.url} - ${duration.inMilliseconds}ms); return response; } catch (error, stackTrace) { final duration DateTime.now().difference(startTime); _logger.severe( 请求失败: ${chain.request.url}, error, stackTrace, ); // 记录错误详情 _logErrorDetails(chain.request, error, duration); rethrow; } } }总结Chopper提供了全面的错误处理机制从基础的异常捕获到高级的重试策略和超时配置。通过合理使用拦截器、转换器和自定义错误处理开发者可以构建出健壮、可靠的网络请求层。关键要点使用正确的异常类型根据错误类型选择合适的异常类实现智能重试针对不同错误类型采用不同的重试策略配置合理的超时根据网络环境和业务需求设置超时时间统一错误处理创建统一的错误处理中心简化错误处理逻辑完善的日志记录记录详细的错误信息便于问题排查通过本文介绍的技术您可以构建出能够优雅处理各种网络异常的Flutter应用提升用户体验和应用的稳定性。【免费下载链接】chopperChopper is an http client generator using source_gen and inspired from Retrofit.项目地址: https://gitcode.com/gh_mirrors/ch/chopper创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考