onFailure property
错误消息显示回调(全局默认错误处理) 如果为 null,则不显示错误提示
回调参数:
httpStatusCodeHTTP 状态码(如 200, 404, 500 等,可能为 null)errorCode业务错误码(可能为 null,取决于 Response 实现)message错误消息(可能是国际化键,需要在回调中自行翻译)
注意:
- 如果用户使用了链式调用的 onFailure,此回调不会被调用(优先级:链式调用的 onFailure > 全局的 onFailure)
- 如果设置了 on401Unauthorized,401 错误不会调用此回调
- 若 unauthorizedRetry 已触发 UnauthorizedRetryConfig.onRefreshFailed,则本次「未授权形态」响应
不再调用本全局回调(避免与
onRefreshFailed重复);链式 onFailure 仍会先执行(若存在)
示例:
onFailure: (httpStatusCode, errorCode, message) {
print('HTTP 状态码: $httpStatusCode, 业务错误码: $errorCode, 错误消息: $message');
if (errorCode == 1001) {
// 处理业务错误码 1001
}
}
Implementation
final void Function(int? httpStatusCode, int? errorCode, String message)? onFailure;