Skip to main content

floe_core/
errors.rs

1use std::fmt;
2
3use crate::run::events::{event_time_ms, is_observer_set, RunEvent};
4
5#[derive(Debug)]
6pub struct ConfigError(pub String);
7
8impl fmt::Display for ConfigError {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        write!(f, "{}", self.0)
11    }
12}
13
14impl std::error::Error for ConfigError {}
15
16#[derive(Debug)]
17pub struct RunError(pub String);
18
19impl fmt::Display for RunError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        write!(f, "{}", self.0)
22    }
23}
24
25impl std::error::Error for RunError {}
26
27#[derive(Debug)]
28pub struct StorageError(pub String);
29
30impl fmt::Display for StorageError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{}", self.0)
33    }
34}
35
36impl std::error::Error for StorageError {}
37
38#[derive(Debug)]
39pub struct IoError(pub String);
40
41impl fmt::Display for IoError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(f, "{}", self.0)
44    }
45}
46
47impl std::error::Error for IoError {}
48
49fn emit_stderr(message: &str) {
50    eprintln!("error: {message}");
51}
52
53pub fn emit(
54    run_id: &str,
55    entity: Option<&str>,
56    input: Option<&str>,
57    code: Option<&str>,
58    message: &str,
59) {
60    if is_observer_set() {
61        let observer = crate::run::events::default_observer();
62        observer.on_event(RunEvent::Log {
63            run_id: run_id.to_string(),
64            log_level: "error".to_string(),
65            code: code.map(ToString::to_string),
66            message: message.to_string(),
67            entity: entity.map(ToString::to_string),
68            input: input.map(ToString::to_string),
69            ts_ms: event_time_ms(),
70        });
71        return;
72    }
73
74    emit_stderr(message);
75}