Skip to main content

openlark_docs/common/
api_utils.rs

1/// API通用工具函数(re-export core canonical + 本域 error 构造器)。
2///
3/// `serialize_params` 已下沉到
4/// `openlark_core::api`(#330),本模块 re-export canonical copy;保留 docs 域专用的
5/// `missing_response_data_error`(bitable 等叶子直接复用其错误形状)。
6use openlark_core::error;
7
8// canonical HTTP 管道 helper(#330 下沉到 core)
9pub use openlark_core::api::serialize_params;
10
11const ERROR_COMPONENT: &str = "openlark-docs";
12
13fn attach_standard_error_context(
14    err: openlark_core::error::CoreError,
15    operation: &str,
16    resource: &str,
17    request_id: Option<String>,
18) -> openlark_core::error::CoreError {
19    err.with_operation(operation, ERROR_COMPONENT)
20        .map_context(|ctx| {
21            ctx.add_context("resource", resource);
22            if let Some(request_id) = request_id.filter(|value| !value.trim().is_empty()) {
23                ctx.set_request_id(request_id);
24            }
25        })
26}
27
28/// 创建“响应 data 为空”的标准错误。
29pub fn missing_response_data_error(
30    resource: &str,
31    request_id: Option<String>,
32) -> openlark_core::error::CoreError {
33    attach_standard_error_context(
34        error::validation_error("response.data", "服务器没有返回有效的数据"),
35        "extract_response_data",
36        resource,
37        request_id,
38    )
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use openlark_core::error::ErrorTrait;
45
46    #[test]
47    fn missing_response_data_error_attaches_context() {
48        let err = missing_response_data_error("获取多维表格", Some("req-docs-456".to_string()));
49        assert_eq!(err.context().operation(), Some("extract_response_data"));
50        assert_eq!(err.context().component(), Some(ERROR_COMPONENT));
51        assert_eq!(err.context().get_context("resource"), Some("获取多维表格"));
52        assert_eq!(err.context().request_id(), Some("req-docs-456"));
53    }
54}