mirror of
https://seed.radicle.dev/rad:zwTxygwuz5LDGBq255RA2CbNGrz8.git
synced 2026-11-29 14:50:35 +08:00
feat(src/db.rs): handle JSON encoding errors without panic
Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user