os_bridge/common/
errors.rs

1use std::error::Error;
2use std::fmt::{self};
3use std::fmt::{Debug, Display};
4
5#[derive(Debug, thiserror::Error)]
6pub enum BridgeError {
7  // IO error
8  #[error(transparent)]
9  Io(#[from] std::io::Error),
10
11  // custom error
12  #[allow(unused)]
13  #[error("{0}")]
14  WithMsg(String),
15}
16
17#[allow(unused)]
18impl BridgeError {
19  pub fn new(msg: &str) -> Self {
20    BridgeError::WithMsg(msg.to_string())
21  }
22}
23
24impl From<&str> for BridgeError {
25  fn from(msg: &str) -> Self {
26    BridgeError::new(msg)
27  }
28}
29
30impl From<String> for BridgeError {
31  fn from(msg: String) -> Self {
32    BridgeError::new(&msg)
33  }
34}
35
36#[derive(Debug)]
37pub struct ErrMsg {
38  pub code: u32,
39  pub msg: String,
40}
41
42//  AppError ro std::fmt::Display traits
43impl fmt::Display for ErrMsg {
44  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45    write!(f, "ErrMsg: {{code:{},msg :{} }}", self.code, self.msg) // user-facing output
46  }
47}
48
49#[allow(unused)]
50pub fn print_err<E>(e: E)
51where
52  E: Display + Debug + Error,
53{
54  print!("{}", e);
55}