From 24087e8441a481c741a9fc06a4ed604db5edb486 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 31 Oct 2021 22:21:44 +0100 Subject: [PATCH] feat(engine): display and notify an error if rendering fails. Fix #774 --- espanso-engine/src/event/effect.rs | 2 +- espanso-engine/src/event/mod.rs | 2 +- .../src/process/middleware/notification.rs | 2 ++ espanso-engine/src/process/middleware/render.rs | 15 ++++++++++++--- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/espanso-engine/src/event/effect.rs b/espanso-engine/src/event/effect.rs index 505c043..48314c1 100644 --- a/espanso-engine/src/event/effect.rs +++ b/espanso-engine/src/event/effect.rs @@ -30,7 +30,7 @@ pub struct CursorHintCompensationEvent { pub cursor_hint_back_count: usize, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct TextInjectRequest { pub text: String, pub force_mode: Option, diff --git a/espanso-engine/src/event/mod.rs b/espanso-engine/src/event/mod.rs index 841310e..2042588 100644 --- a/espanso-engine/src/event/mod.rs +++ b/espanso-engine/src/event/mod.rs @@ -49,7 +49,6 @@ impl Event { #[allow(clippy::upper_case_acronyms)] pub enum EventType { NOOP, - ProcessingError(String), ExitRequested(ExitMode), Exit(ExitMode), Heartbeat, @@ -77,6 +76,7 @@ pub enum EventType { DiscardPrevious(internal::DiscardPreviousEvent), DiscardBetween(internal::DiscardBetweenEvent), Undo(internal::UndoEvent), + RenderingError, Disabled, Enabled, diff --git a/espanso-engine/src/process/middleware/notification.rs b/espanso-engine/src/process/middleware/notification.rs index 31a94aa..e0e6aa9 100644 --- a/espanso-engine/src/process/middleware/notification.rs +++ b/espanso-engine/src/process/middleware/notification.rs @@ -22,6 +22,7 @@ use crate::event::{Event, EventType}; pub trait NotificationManager { fn notify_status_change(&self, enabled: bool); + fn notify_rendering_error(&self); } pub struct NotificationMiddleware<'a> { @@ -45,6 +46,7 @@ impl<'a> Middleware for NotificationMiddleware<'a> { match &event.etype { EventType::Enabled => self.notification_manager.notify_status_change(true), EventType::Disabled => self.notification_manager.notify_status_change(false), + EventType::RenderingError => self.notification_manager.notify_rendering_error(), _ => {} } diff --git a/espanso-engine/src/process/middleware/render.rs b/espanso-engine/src/process/middleware/render.rs index c227c0c..d8175e0 100644 --- a/espanso-engine/src/process/middleware/render.rs +++ b/espanso-engine/src/process/middleware/render.rs @@ -22,7 +22,7 @@ use std::collections::HashMap; use log::error; use super::super::Middleware; -use crate::event::{internal::RenderedEvent, Event, EventType}; +use crate::event::{effect::TextInjectRequest, internal::RenderedEvent, Event, EventType}; use anyhow::Result; use thiserror::Error; @@ -62,7 +62,7 @@ impl<'a> Middleware for RenderMiddleware<'a> { "render" } - fn next(&self, event: Event, _: &mut dyn FnMut(Event)) -> Event { + fn next(&self, event: Event, dispatch: &mut dyn FnMut(Event)) -> Event { if let EventType::RenderingRequested(m_event) = event.etype { match self.renderer.render( m_event.match_id, @@ -91,7 +91,16 @@ impl<'a> Middleware for RenderMiddleware<'a> { } _ => { error!("error during rendering: {:?}", err); - return Event::caused_by(event.source_id, EventType::ProcessingError("An error has occurred during rendering, please examine the logs or contact support.".to_string())); + + dispatch(Event::caused_by( + event.source_id, + EventType::TextInject(TextInjectRequest { + text: "[Espanso]: An error occurred during rendering, please examine the logs for more information.".to_string(), + ..Default::default() + }), + )); + + return Event::caused_by(event.source_id, EventType::RenderingError); } }, }