use openlark_core::error;
pub use openlark_core::api::serialize_params;
const ERROR_COMPONENT: &str = "openlark-workflow";
fn attach_standard_error_context(
err: openlark_core::error::CoreError,
operation: &str,
resource: &str,
request_id: Option<String>,
) -> openlark_core::error::CoreError {
err.with_operation(operation, ERROR_COMPONENT)
.map_context(|ctx| {
ctx.add_context("resource", resource);
if let Some(request_id) = request_id.filter(|value| !value.trim().is_empty()) {
ctx.set_request_id(request_id);
}
})
}
pub fn missing_response_data_error(
resource: &str,
request_id: Option<String>,
) -> openlark_core::error::CoreError {
attach_standard_error_context(
error::validation_error("response.data", "服务器没有返回有效的数据"),
"extract_response_data",
resource,
request_id,
)
}
pub fn request_serialization_error(
resource: &str,
source: impl std::fmt::Display,
) -> openlark_core::error::CoreError {
attach_standard_error_context(
error::validation_error("request.params", format!("无法序列化请求参数: {source}")),
"serialize_params",
resource,
None,
)
}
#[cfg(test)]
mod tests {
use super::*;
use openlark_core::error::ErrorTrait;
#[test]
fn missing_response_data_error_attaches_context() {
let err = missing_response_data_error("审批任务查询", Some("req-wf-456".to_string()));
assert_eq!(err.context().operation(), Some("extract_response_data"));
assert_eq!(err.context().component(), Some(ERROR_COMPONENT));
assert_eq!(err.context().get_context("resource"), Some("审批任务查询"));
assert_eq!(err.context().request_id(), Some("req-wf-456"));
}
#[test]
fn request_serialization_error_attaches_context() {
let err = request_serialization_error("退回审批任务", "boom");
assert_eq!(err.context().operation(), Some("serialize_params"));
assert_eq!(err.context().get_context("resource"), Some("退回审批任务"));
}
}