use std::sync::Arc;
use bevy::prelude::*;
use bevy_event_bus::BusMessage;
use bevy_event_bus::resources::MessageMetadata;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EventBusErrorType {
Serialization, Connection, NotConfigured, Topic, Timeout, DeliveryFailure, Deserialization, ConsumerError, AckFailure, InvalidReadConfig, InvalidWriteConfig, Other, }
#[derive(Message, Debug, Clone)]
pub struct EventBusError<T: BusMessage> {
pub topic: String,
pub error_type: EventBusErrorType,
pub error_message: String,
pub timestamp: std::time::SystemTime,
pub original_event: Option<T>,
pub backend: Option<String>,
pub metadata: Option<MessageMetadata>,
}
impl<T: BusMessage> EventBusError<T> {
pub fn immediate(
topic: String,
error_type: EventBusErrorType,
error_message: String,
original_event: T,
) -> Self {
Self {
topic,
error_type,
error_message,
timestamp: std::time::SystemTime::now(),
original_event: Some(original_event),
backend: None,
metadata: None,
}
}
pub fn async_delivery(
topic: String,
error_message: String,
backend: Option<String>,
metadata: Option<MessageMetadata>,
) -> Self {
Self {
topic,
error_type: EventBusErrorType::DeliveryFailure,
error_message,
timestamp: std::time::SystemTime::now(),
original_event: None,
backend,
metadata,
}
}
}
#[derive(Debug, Clone)]
pub struct BusErrorContext {
pub backend: &'static str,
pub topic: String,
pub kind: EventBusErrorType,
pub message: String,
pub metadata: Option<MessageMetadata>,
pub original_bytes: Option<Vec<u8>>, }
pub type BusErrorCallback = Arc<dyn Fn(BusErrorContext) + Send + Sync + 'static>;
impl BusErrorContext {
pub fn new(
backend: &'static str,
topic: impl Into<String>,
kind: EventBusErrorType,
message: impl Into<String>,
metadata: Option<MessageMetadata>,
original_bytes: Option<Vec<u8>>,
) -> Self {
Self {
backend,
topic: topic.into(),
kind,
message: message.into(),
metadata,
original_bytes,
}
}
}