Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat(src/db.rs): handle JSON encoding errors without panic
Lars Wirzenius committed 1 year ago
commit 1bf620feca12126ad3700a82202ac92281056f9f
parent 573bc76a20b3210605596a4cf744dda4aa0d3b98
1 file changed +9 -1
modified src/db.rs
@@ -248,7 +248,7 @@ impl Db {

    /// Add a new event to the event queue, returning its id.
    pub fn push_queued_event(&self, event: BrokerEvent) -> Result<QueueId, DbError> {
-
        let json = serde_json::to_string(&event).expect("serialize BrokerEvent to JSON");
+
        let json = serde_json::to_string(&event).map_err(DbError::event_to_json)?;

        let id = QueueId::default();
        let ts = now();
@@ -558,6 +558,10 @@ pub enum DbError {
    #[error("failed to parse queued event as JSON: {0}")]
    EventFromJson(String, #[source] serde_json::Error),

+
    /// Can't convert broker event into JSON.
+
    #[error("failed to convert broker event into JSON")]
+
    EventToJson(#[source] serde_json::Error),
+

    #[error("failed to insert an event into queue")]
    PushEvent(String, #[source] sqlite::Error),

@@ -639,6 +643,10 @@ impl DbError {
        Self::EventFromJson(json.into(), e)
    }

+
    fn event_to_json(e: serde_json::Error) -> Self {
+
        Self::EventToJson(e)
+
    }
+

    fn push_event(sql: &str, e: sqlite::Error) -> Self {
        Self::PushEvent(sql.into(), e)
    }