rain_core/
errors.rs

1#![allow(renamed_and_removed_lints)]
2
3use std::fmt;
4
5use types::TaskId;
6use utils::convert::ToCapnp;
7
8// Create the Error, ErrorKind, ResultExt, and Result types
9error_chain!{
10    types {
11        Error, ErrorKind, ResultExt;
12    }
13    foreign_links {
14        Io(::std::io::Error);
15        Capnp(::capnp::Error);
16        CapnpNotInSchema(::capnp::NotInSchema);
17        Timer(::tokio_timer::Error);
18        SessionErr(SessionError);
19        Utf8Err(::std::str::Utf8Error);
20        Json(::serde_json::Error);
21        Sqlite(::rusqlite::Error);
22    }
23
24    errors {
25        Ignored {
26            description("Request asked for ignored id")
27        }
28    }
29}
30
31// Explicit alias just to make the IDEs happier
32pub type Result<T> = ::std::result::Result<T, Error>;
33
34impl ::std::convert::From<Error> for ::capnp::Error {
35    fn from(e: Error) -> Self {
36        ::capnp::Error::failed(e.description().to_string())
37    }
38}
39
40#[derive(Debug, Clone)]
41pub struct SessionError {
42    message: String,
43    debug: String,
44    task_id: TaskId,
45}
46
47impl SessionError {
48    pub fn new(message: String, debug: String, task_id: TaskId) -> Self {
49        SessionError {
50            message,
51            debug,
52            task_id,
53        }
54    }
55
56    pub fn to_capnp(&self, builder: &mut ::common_capnp::error::Builder) {
57        builder.reborrow().set_message(&self.message);
58        builder.reborrow().set_debug(&self.debug);
59        self.task_id
60            .to_capnp(&mut builder.reborrow().get_task().unwrap());
61    }
62}
63
64impl ::std::error::Error for SessionError {
65    fn description(&self) -> &str {
66        &self.message
67    }
68
69    fn cause(&self) -> Option<&::std::error::Error> {
70        None
71    }
72}
73
74impl fmt::Display for SessionError {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        write!(f, "SessionError({:?})", self.message)
77    }
78}