use rocketmq_error::{fields, ErrorContext, ErrorDescriptor, ViewValueRef};
use crate::{ClientError, MQClientException};
#[track_caller]
pub(crate) fn assert_error(error: &ClientError, descriptor: &'static ErrorDescriptor) {
assert_eq!(error.code(), descriptor.code());
assert_eq!(
error.to_string(),
format!("{}: {}", descriptor.code(), descriptor.public_message())
);
error.public_view().expect("valid public error context");
error.diagnostic_view().expect("valid diagnostic error context");
}
#[track_caller]
pub(crate) fn assert_context_field(error: &ClientError, name: &str, expected: ViewValueRef<'_>) {
let view = error.diagnostic_view().expect("valid diagnostic error context");
let actual = view
.fields()
.find(|field| field.name() == name)
.map(|field| field.value());
assert_eq!(actual, Some(expected), "unexpected context field {name}");
}
#[track_caller]
pub(crate) fn assert_invalid_argument(error: &ClientError) {
assert_error(error, &rocketmq_error::CORE_ARGUMENT_INVALID);
assert_context_field(error, fields::MESSAGE_PRESENT.schema().name(), ViewValueRef::Redacted);
assert_eq!(error.public_view().unwrap().fields().count(), 0);
}
#[track_caller]
pub(crate) fn assert_not_initialized(error: &ClientError, component: &str) {
assert_error(error, &rocketmq_error::CORE_LIFECYCLE_NOT_INITIALIZED);
assert_eq!(
error.context(),
&ErrorContext::new().with_text(fields::COMPONENT_NAME, component)
);
}
#[track_caller]
pub(crate) fn assert_invalid_state(error: &ClientError, expected: &str, actual: &str) {
assert_error(error, &rocketmq_error::CLIENT_LIFECYCLE_INVALID_STATE);
assert_eq!(
error.context(),
&ErrorContext::new()
.with_text(fields::EXPECTED_STATE, expected)
.with_text(fields::ACTUAL_STATE, actual)
);
}
#[track_caller]
pub(crate) fn client_exception(error: &ClientError) -> &MQClientException {
assert_invalid_argument(error);
error
.source_ref::<MQClientException>()
.expect("retained Java client exception")
}