openlark_docs/common/
api_utils.rs1use openlark_core::error;
7
8pub 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
28pub 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}