openlark-client 0.15.0

OpenLark 高级客户端 - 统一入口点和轻量级服务注册表
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//! OpenLark Client 错误类型定义
//!
//! 基于 openlark-core 的现代化错误处理系统
//! 直接使用 CoreError,提供类型安全和用户友好的错误管理

use crate::registry::RegistryError;
use openlark_core::error::{
    ApiError, CoreError, ErrorCategory, ErrorCode, ErrorContext, ErrorTrait, ErrorType,
};

/// 🚨 OpenLark 客户端错误类型
///
/// 直接类型别名,充分利用 CoreError 的强大功能
pub type Error = CoreError;

/// 📦 客户端结果类型别名
pub type Result<T> = std::result::Result<T, Error>;

// ============================================================================
// 便利错误创建函数(重新导出核心函数)
// ============================================================================

/// 创建网络错误
pub fn network_error(message: impl Into<String>) -> Error {
    openlark_core::error::network_error(message)
}

/// 创建认证错误
pub fn authentication_error(message: impl Into<String>) -> Error {
    openlark_core::error::authentication_error(message)
}

/// 创建访问令牌格式/内容无效错误
pub fn token_invalid_error(detail: impl Into<String>) -> Error {
    openlark_core::error::token_invalid_error(detail)
}

/// 创建访问令牌过期错误(飞书通用码 99991677)
pub fn token_expired_error(detail: impl Into<String>) -> Error {
    openlark_core::error::token_expired_error(detail)
}

/// 创建缺少权限 scope 的错误
pub fn permission_missing_error(scopes: &[impl AsRef<str>]) -> Error {
    openlark_core::error::permission_missing_error(scopes)
}

/// 创建 SSO 令牌无效错误
pub fn sso_token_invalid_error(detail: impl Into<String>) -> Error {
    openlark_core::error::sso_token_invalid_error(detail)
}

/// 创建身份标识非法错误
pub fn user_identity_invalid_error(desc: impl Into<String>) -> Error {
    openlark_core::error::user_identity_invalid_error(desc)
}

/// 基于飞书通用 `code` 的统一错误映射(客户端自定义解析时可复用)
pub fn from_feishu_response(
    code: i32,
    endpoint: impl Into<String>,
    message: impl Into<String>,
    request_id: Option<String>,
) -> Error {
    let mapped = ErrorCode::from_feishu_code(code).unwrap_or_else(|| ErrorCode::from_code(code));

    let mut ctx = ErrorContext::new();
    ctx.add_context("feishu_code", code.to_string());
    if let Some(req) = request_id {
        ctx.set_request_id(req);
    }

    let status = mapped
        .http_status()
        .unwrap_or_else(|| match mapped.category() {
            ErrorCategory::RateLimit => 429,
            ErrorCategory::Authentication
            | ErrorCategory::Permission
            | ErrorCategory::Parameter => 400,
            ErrorCategory::Resource => 404,
            _ => 500,
        });

    CoreError::Api(Box::new(ApiError {
        status,
        endpoint: endpoint.into().into(),
        message: message.into(),
        source: None,
        code: mapped,
        ctx: Box::new(ctx),
    }))
}

/// 创建API错误
pub fn api_error(
    status: u16,
    endpoint: impl Into<String>,
    message: impl Into<String>,
    request_id: Option<String>,
) -> Error {
    openlark_core::error::api_error(status, endpoint, message, request_id)
}

/// 创建验证错误
pub fn validation_error(field: impl Into<String>, message: impl Into<String>) -> Error {
    openlark_core::error::validation_error(field, message)
}

/// 创建配置错误
pub fn configuration_error(message: impl Into<String>) -> Error {
    openlark_core::error::configuration_error(message)
}

/// 创建序列化错误
pub fn serialization_error(message: impl Into<String>) -> Error {
    openlark_core::error::serialization_error(message, None::<serde_json::Error>)
}

/// 创建业务逻辑错误
pub fn business_error(_code: impl Into<String>, message: impl Into<String>) -> Error {
    openlark_core::error::business_error(message)
}

/// 创建超时错误
pub fn timeout_error(operation: impl Into<String>) -> Error {
    use std::time::Duration;
    openlark_core::error::timeout_error(Duration::from_secs(30), Some(operation.into()))
}

/// 创建限流错误
pub fn rate_limit_error(retry_after: Option<u64>) -> Error {
    use std::time::Duration;
    openlark_core::error::rate_limit_error(
        100,
        Duration::from_secs(60),
        retry_after.map(Duration::from_secs),
    )
}

/// 创建服务不可用错误
pub fn service_unavailable_error(service: impl Into<String>) -> Error {
    use std::time::Duration;
    openlark_core::error::service_unavailable_error(service, Some(Duration::from_secs(60)))
}

/// 创建内部错误
pub fn internal_error(message: impl Into<String>) -> Error {
    openlark_core::error::api_error(500, "internal", message, None::<String>)
}

/// 创建注册表错误
pub fn registry_error(err: RegistryError) -> Error {
    internal_error(format!("服务注册表错误: {}", err))
}

// ============================================================================
// 错误扩展功能
// ============================================================================

/// 客户端错误扩展特征,提供错误恢复建议和步骤
pub trait ClientErrorExt {
    /// 获取错误建议
    fn suggestion(&self) -> &'static str;

    /// 获取错误恢复步骤
    fn recovery_steps(&self) -> Vec<&'static str>;
}

impl ClientErrorExt for Error {
    fn suggestion(&self) -> &'static str {
        match self.error_type() {
            ErrorType::Network => "检查网络连接,确认防火墙设置",
            ErrorType::Authentication => "验证应用凭据,检查令牌有效性",
            ErrorType::Api => "检查API参数,确认请求格式正确",
            ErrorType::Validation => "验证输入参数格式和范围",
            ErrorType::Configuration => "检查应用配置文件和环境变量",
            ErrorType::Serialization => "确认数据格式正确,检查JSON结构",
            ErrorType::Business => "确认业务逻辑条件,检查相关权限",
            ErrorType::Timeout => "增加超时时间或优化请求内容",
            ErrorType::RateLimit => "稍后重试,考虑降低请求频率",
            ErrorType::ServiceUnavailable => "稍后重试,检查服务状态",
            ErrorType::Internal => "联系技术支持,提供错误详情",
        }
    }

    fn recovery_steps(&self) -> Vec<&'static str> {
        match self.error_type() {
            ErrorType::Network => vec![
                "检查网络连接状态",
                "确认代理设置正确",
                "验证防火墙规则",
                "尝试切换网络环境",
            ],
            ErrorType::Authentication => vec![
                "验证应用ID和密钥正确性",
                "检查令牌是否过期",
                "确认应用权限配置",
                "重新生成访问令牌",
            ],
            ErrorType::Api => vec![
                "检查请求参数格式",
                "确认API端点正确",
                "验证请求体结构",
                "查阅API文档",
            ],
            ErrorType::Validation => vec![
                "检查必填字段",
                "验证数据格式和范围",
                "确认字段类型正确",
                "参考输入示例",
            ],
            ErrorType::Configuration => vec![
                "检查环境变量设置",
                "验证配置文件格式",
                "确认应用权限配置",
                "重新加载配置",
            ],
            ErrorType::Serialization => vec![
                "检查JSON格式正确性",
                "验证数据结构匹配",
                "确认字段类型一致",
                "使用在线JSON验证工具",
            ],
            ErrorType::Business => vec![
                "检查业务规则约束",
                "确认用户权限充分",
                "验证数据完整性",
                "联系业务负责人",
            ],
            ErrorType::Timeout => vec![
                "增加请求超时时间",
                "优化网络环境",
                "减少请求数据量",
                "考虑分批处理",
            ],
            ErrorType::RateLimit => vec![
                "等待后重试",
                "降低请求频率",
                "实施退避策略",
                "联系技术支持提高限额",
            ],
            ErrorType::ServiceUnavailable => vec![
                "稍后重试请求",
                "检查服务状态页面",
                "切换到备用方案",
                "联系技术支持",
            ],
            ErrorType::Internal => vec![
                "记录详细错误信息",
                "检查系统日志",
                "重启相关服务",
                "联系技术支持",
            ],
        }
    }
}

// ============================================================================
// 类型转换
// ============================================================================

// 注意: reqwest::Error -> CoreError 转换已在 openlark-core 中实现
// 这里不需要重复实现,直接使用 CoreError 的转换机制

// 注意: 不能为外部类型实现 From,因为这些类型由 CoreError 定义在 openlark-core 中
// 请使用对应的函数来进行错误转换

// 从注册表错误转换
impl From<RegistryError> for Error {
    fn from(err: RegistryError) -> Self {
        registry_error(err)
    }
}

// ============================================================================
// 便利函数
// ============================================================================

/// 🔧 从 openlark-core SDKResult 转换为客户端 Result 的便利函数
///
/// 这个函数现在只是类型转换,因为我们直接使用 CoreError
///
/// # 示例
///
/// ```rust
/// use openlark_client::error::from_sdk_result;
///
/// let core_result: openlark_core::SDKResult<String> = Ok("success".to_string());
/// let client_result = from_sdk_result(core_result);
/// assert!(client_result.is_ok());
/// ```
pub fn from_sdk_result<T>(result: openlark_core::SDKResult<T>) -> Result<T> {
    result
}

/// 🔧 创建带有上下文的错误的便利函数
pub fn with_context<T>(
    result: Result<T>,
    context_key: impl Into<String>,
    context_value: impl Into<String>,
) -> Result<T> {
    let key = context_key.into();
    let value = context_value.into();
    result.map_err(|err| err.with_context_kv(key, value))
}

/// 🔧 创建带有操作上下文的错误的便利函数
pub fn with_operation_context<T>(
    result: Result<T>,
    operation: impl Into<String>,
    component: impl Into<String>,
) -> Result<T> {
    let operation = operation.into();
    let component = component.into();
    result.map_err(|err| err.with_operation(operation, component))
}

// ============================================================================
// 错误分析和报告
// ============================================================================

/// 错误分析器,提供详细的错误信息和恢复建议
#[derive(Debug)]
pub struct ErrorAnalyzer<'a> {
    error: &'a Error,
}

impl<'a> ErrorAnalyzer<'a> {
    /// 创建错误分析器
    pub fn new(error: &'a Error) -> Self {
        Self { error }
    }

    /// 获取详细的错误报告
    pub fn detailed_report(&self) -> String {
        let mut report = String::new();

        report.push_str("🚨 错误分析报告\n");
        report.push_str("================\n\n");

        // 基本信息
        report.push_str("📋 基本信息:\n");
        report.push_str(&format!("  错误类型: {:?}\n", self.error.error_type()));
        report.push_str(&format!("  错误代码: {:?}\n", self.error.error_code()));
        report.push_str(&format!("  严重程度: {:?}\n", self.error.severity()));
        report.push_str(&format!("  可重试: {}\n", self.error.is_retryable()));

        if let Some(request_id) = self.error.context().request_id() {
            report.push_str(&format!("  请求ID: {}\n", request_id));
        }

        report.push('\n');

        // 错误消息
        report.push_str("💬 错误消息:\n");
        report.push_str(&format!("  技术消息: {}\n", self.error));
        report.push_str(&format!(
            "  用户消息: {}\n",
            self.error.user_message().unwrap_or("未知错误")
        ));

        report.push('\n');

        // 建议和恢复步骤
        report.push_str("💡 建议:\n");
        report.push_str(&format!("  {}\n", self.error.suggestion()));

        report.push_str("\n🔧 恢复步骤:\n");
        for (i, step) in self.error.recovery_steps().iter().enumerate() {
            report.push_str(&format!("  {}. {}\n", i + 1, step));
        }

        report.push('\n');

        // 上下文信息
        if self.error.context().context_len() > 0 {
            report.push_str("📊 上下文信息:\n");
            for (key, value) in self.error.context().all_context() {
                report.push_str(&format!("  {}: {}\n", key, value));
            }
            report.push('\n');
        }

        // 时间戳
        if let Some(timestamp) = self.error.context().timestamp() {
            report.push_str(&format!("⏰ 发生时间: {:?}\n", timestamp));
        }
        report
    }

    /// 获取适合日志记录的错误摘要
    pub fn log_summary(&self) -> String {
        format!(
            "Error[{:?}:{:?}] {} - {}",
            self.error.error_type(),
            self.error.error_code(),
            self.error.user_message().unwrap_or("未知错误"),
            if self.error.is_retryable() {
                "(可重试)"
            } else {
                "(不可重试)"
            }
        )
    }

    /// 获取用户友好的错误消息,包含恢复建议
    pub fn user_friendly_with_suggestion(&self) -> String {
        format!(
            "{}\n\n💡 建议: {}\n\n🔧 可以尝试:\n{}",
            self.error.user_message().unwrap_or("未知错误"),
            self.error.suggestion(),
            self.error
                .recovery_steps()
                .iter()
                .enumerate()
                .map(|(i, step)| format!("{}. {}", i + 1, step))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }
}

// 注意: 不能为外部类型 CoreError 定义 inherent impl
// 请使用 ClientErrorExt trait 来获得扩展功能

// ============================================================================
// 测试
// ============================================================================

#[cfg(test)]
#[allow(unused_imports)]
mod tests {
    use super::*;

    #[test]
    fn test_error_convenience_functions() {
        let network_err = network_error("连接失败");
        assert!(network_err.is_network_error());
        assert!(network_err.is_retryable());

        let auth_err = authentication_error("令牌无效");
        assert!(auth_err.is_auth_error());
        assert!(!auth_err.is_retryable());

        let validation_err = validation_error("email", "邮箱格式不正确");
        assert!(validation_err.is_validation_error());
        assert!(!validation_err.is_retryable());
    }

    #[test]
    fn test_error_analyzer() {
        let error = api_error(404, "/users", "用户不存在", Some("req-123".to_string()));
        let analyzer = ErrorAnalyzer::new(&error);

        let report = analyzer.detailed_report();
        assert!(report.contains("错误分析报告"));
        assert!(report.contains("API错误"));
        assert!(report.contains("req-123"));

        let summary = analyzer.log_summary();
        assert!(summary.contains("Error"));
        assert!(summary.contains("Api"));

        let user_msg = analyzer.user_friendly_with_suggestion();
        assert!(user_msg.contains("建议"));
        assert!(user_msg.contains("可以尝试"));
    }

    #[test]
    fn test_client_error_ext() {
        let error = timeout_error("数据同步");

        assert!(!error.is_network_error());
        assert!(!error.is_auth_error());
        assert!(!error.is_business_error());
        assert!(error.is_retryable());

        let suggestion = error.suggestion();
        assert!(!suggestion.is_empty());

        let steps = error.recovery_steps();
        assert!(!steps.is_empty());
        assert!(steps.contains(&"增加请求超时时间"));
    }

    #[test]
    fn test_error_conversions() {
        // 测试 JSON 错误转换
        let json_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let error: Error = json_err.into();
        assert!(error.is_serialization_error());

        // 测试 tokio 超时错误转换
        // let timeout_err = tokio::time::error::Elapsed {}; // Private field
        // let error: Error = timeout_err.into();
        // assert!(error.is_timeout_error());
        // assert!(error.is_retryable());
    }

    #[test]
    fn test_context_functions() {
        let result: Result<i32> = Err(validation_error("age", "年龄不能为负数"));

        let contextual_result = with_context(result, "user_id", "12345");
        assert!(contextual_result.is_err());

        let error = contextual_result.unwrap_err();
        // 我们现在使用结构化上下文,验证上下文内容而不是字符串
        // assert!(error.to_string().contains("user_id: 12345"));
        assert_eq!(error.context().get_context("user_id"), Some("12345"));
    }

    #[test]
    fn test_sdk_result_conversion() {
        // 成功情况
        let core_result: openlark_core::SDKResult<String> = Ok("success".to_string());
        let client_result: Result<String> = from_sdk_result(core_result);
        assert!(client_result.is_ok());
        assert_eq!(client_result.unwrap(), "success");

        // 失败情况
        let core_result: openlark_core::SDKResult<String> = Err(network_error("网络错误"));
        let client_result: Result<String> = from_sdk_result(core_result);
        assert!(client_result.is_err());
        assert!(client_result.unwrap_err().is_network_error());
    }

    #[test]
    fn test_api_error_function() {
        let error = api_error(
            500,
            "/api/test",
            "服务器内部错误",
            Some("req-456".to_string()),
        );
        assert!(error.is_api_error());
        let analyzer = ErrorAnalyzer::new(&error);
        let report = analyzer.detailed_report();
        assert!(report.contains("服务器内部错误"));
    }

    #[test]
    fn test_validation_error_function() {
        let error = validation_error("field_name", "字段值为空");
        assert!(error.is_validation_error());
        let analyzer = ErrorAnalyzer::new(&error);
        let user_msg = analyzer.user_friendly_with_suggestion();
        assert!(user_msg.contains("建议"));
    }

    #[test]
    fn test_configuration_error_function() {
        let error = configuration_error("配置文件缺失");
        assert!(error.is_config_error());
    }

    #[test]
    fn test_serialization_error_function() {
        let error = serialization_error("JSON解析失败");
        assert!(error.is_serialization_error());
    }

    #[test]
    fn test_business_error_function() {
        let error = business_error("ERR_001", "业务规则验证失败");
        assert!(error.is_business_error());
    }

    #[test]
    fn test_timeout_error_function() {
        let error = timeout_error("数据库查询超时");
        assert!(error.is_timeout_error());
        assert!(error.is_retryable());
    }

    #[test]
    fn test_rate_limit_error_function() {
        let error = rate_limit_error(Some(60));
        assert!(error.is_rate_limited());
    }

    #[test]
    fn test_service_unavailable_error_function() {
        let error = service_unavailable_error("支付服务");
        assert!(error.is_service_unavailable_error());
    }

    #[test]
    fn test_internal_error_function() {
        let error = internal_error("系统内部错误");
        assert!(!error.is_user_error());
    }

    #[test]
    fn test_token_invalid_error_function() {
        let error = token_invalid_error("token格式不正确");
        assert!(error.is_auth_error());
    }

    #[test]
    fn test_token_expired_error_function() {
        let error = token_expired_error("token已过期");
        assert!(error.is_auth_error());
    }

    #[test]
    fn test_permission_missing_error_function() {
        let scopes = vec!["read:user", "write:docs"];
        let error = permission_missing_error(&scopes);
        assert!(error.is_auth_error());
    }

    #[test]
    fn test_sso_token_invalid_error_function() {
        let error = sso_token_invalid_error("SSO令牌无效");
        assert!(error.is_auth_error());
    }

    #[test]
    fn test_user_identity_invalid_error_function() {
        let error = user_identity_invalid_error("用户身份标识非法");
        assert!(error.is_auth_error());
    }

    #[test]
    fn test_from_feishu_response_function() {
        let error = from_feishu_response(
            99991677,
            "/api/test",
            "token过期",
            Some("req-789".to_string()),
        );
        // 错误可能是认证错误或其他类型,只需确保能正确创建
        assert!(!error.to_string().is_empty());
        let error2 = from_feishu_response(400, "/api/test", "参数错误", None);
        assert!(!error2.to_string().is_empty());
    }

    #[test]
    fn test_registry_error_conversion() {
        let registry_err = crate::registry::RegistryError::ServiceNotFound {
            name: "test".to_string(),
        };
        let error: Error = registry_err.into();
        assert!(!error.is_user_error());
    }

    #[test]
    fn test_error_analyzer_log_summary() {
        let error = network_error("连接超时");
        let analyzer = ErrorAnalyzer::new(&error);
        let summary = analyzer.log_summary();
        assert!(summary.contains("Network"));
        assert!(summary.contains("可重试") || summary.contains("不可重试"));
    }

    #[test]
    fn test_error_analyzer_user_friendly() {
        let error = api_error(404, "/users/123", "用户不存在", None);
        let analyzer = ErrorAnalyzer::new(&error);
        let friendly = analyzer.user_friendly_with_suggestion();
        assert!(friendly.contains("建议"));
        assert!(friendly.contains("可以尝试"));
    }

    #[test]
    fn test_with_operation_context() {
        let result: Result<i32> = Err(network_error("网络错误"));
        let contextual_result = with_operation_context(result, "test_operation", "TestComponent");
        assert!(contextual_result.is_err());
        let error = contextual_result.unwrap_err();
        assert_eq!(
            error.context().get_context("operation"),
            Some("test_operation")
        );
        assert_eq!(
            error.context().get_context("component"),
            Some("TestComponent")
        );
    }

    #[test]
    fn test_with_operation_context_updates_timeout_operation_field() {
        use std::time::Duration;

        let result: Result<i32> = Err(openlark_core::error::timeout_error(
            Duration::from_secs(3),
            Some("old_operation".to_string()),
        ));

        let contextual_result = with_operation_context(result, "new_operation", "ClientLayer");
        assert!(contextual_result.is_err());

        match contextual_result.unwrap_err() {
            CoreError::Timeout {
                operation, ref ctx, ..
            } => {
                assert_eq!(operation.as_deref(), Some("new_operation"));
                assert_eq!(ctx.operation(), Some("new_operation"));
                assert_eq!(ctx.component(), Some("ClientLayer"));
            }
            other => panic!("expected timeout error, got {:?}", other.error_type()),
        }
    }

    #[test]
    fn test_all_error_types_suggestion() {
        let error_types = vec![
            (network_error("test"), "检查网络连接"),
            (authentication_error("test"), "验证应用凭据"),
            (api_error(500, "/test", "test", None), "检查API参数"),
            (validation_error("field", "test"), "验证输入参数"),
            (configuration_error("test"), "检查应用配置"),
            (serialization_error("test"), "确认数据格式"),
            (business_error("code", "test"), "确认业务逻辑"),
            (timeout_error("test"), "增加超时时间"),
            (rate_limit_error(None), "稍后重试"),
            (service_unavailable_error("svc"), "稍后重试"),
            (internal_error("test"), "联系技术支持"),
        ];

        for (error, expected_keyword) in error_types {
            let suggestion = error.suggestion();
            assert!(
                suggestion.contains(expected_keyword) || !suggestion.is_empty(),
                "Error type {:?} should have meaningful suggestion",
                error.error_type()
            );
        }
    }

    #[test]
    fn test_all_error_types_recovery_steps() {
        let error_types = vec![
            network_error("test"),
            authentication_error("test"),
            api_error(500, "/test", "test", None),
            validation_error("field", "test"),
            configuration_error("test"),
            serialization_error("test"),
            business_error("code", "test"),
            timeout_error("test"),
            rate_limit_error(None),
            service_unavailable_error("svc"),
            internal_error("test"),
        ];

        for error in error_types {
            let steps = error.recovery_steps();
            assert!(
                !steps.is_empty(),
                "Error type {:?} should have recovery steps",
                error.error_type()
            );
            assert!(
                steps.len() >= 3,
                "Error type {:?} should have at least 3 recovery steps",
                error.error_type()
            );
        }
    }
}