udi/
errors.rs

1//
2// Copyright (c) 2011-2017, UDI Contributors
3// All rights reserved.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at http://mozilla.org/MPL/2.0/.
8//
9#![deny(warnings)]
10
11extern crate serde_cbor;
12
13use std::error::Error as StdError;
14use std::io;
15use std::sync;
16
17use super::Process;
18use super::Thread;
19
20#[derive(Debug, ErrorChain)]
21pub enum ErrorKind {
22    #[error_chain(foreign)]
23    Io(io::Error),
24
25    #[error_chain(foreign)]
26    #[error_chain(display = "cbor_error_display")]
27    Cbor(serde_cbor::Error),
28
29    #[error_chain(custom)]
30    #[error_chain(description = "library_error_description")]
31    #[error_chain(display = "library_error_display")]
32    Library(String),
33
34    #[error_chain(custom)]
35    #[error_chain(description = "request_error_description")]
36    #[error_chain(display = "request_error_display")]
37    Request(String)
38}
39
40fn cbor_error_display(f: &mut ::std::fmt::Formatter, e: &serde_cbor::Error)
41    -> ::std::fmt::Result {
42
43    match *e {
44        serde_cbor::Error::Custom(ref s) => write!(f, "CBOR error: {}", s),
45        _ => write!(f, "CBOR error: {}", e.description())
46    }
47}
48
49fn library_error_description(_: &str) -> &str {
50    "library error"
51}
52
53fn library_error_display(f: &mut ::std::fmt::Formatter, s: &str)
54    -> ::std::fmt::Result {
55
56    write!(f, "library error: {}", s)
57}
58
59fn request_error_description(_: &str) -> &str {
60    "invalid request"
61}
62
63fn request_error_display(f: &mut ::std::fmt::Formatter, s: &str)
64    -> ::std::fmt::Result {
65
66    write!(f, "invalid request: {}", s)
67}
68
69impl<'a> From<sync::PoisonError<sync::MutexGuard<'a, Process>>> for Error {
70    fn from(err: sync::PoisonError<sync::MutexGuard<'a, Process>>) -> Error {
71        ErrorKind::Library(err.description().to_owned()).into()
72    }
73}
74
75impl<'a> From<sync::PoisonError<sync::MutexGuard<'a, Thread>>> for Error {
76    fn from(err: sync::PoisonError<sync::MutexGuard<'a, Thread>>) -> Error {
77        ErrorKind::Library(err.description().to_owned()).into()
78    }
79}