1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Copyright (c) 2011-2017, UDI Contributors
// All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
#![deny(warnings)]

extern crate serde_cbor;

use std::error::Error as StdError;
use std::io;
use std::sync;

use super::Process;
use super::Thread;

#[derive(Debug, ErrorChain)]
pub enum ErrorKind {
    #[error_chain(foreign)]
    Io(io::Error),

    #[error_chain(foreign)]
    #[error_chain(display = "cbor_error_display")]
    Cbor(serde_cbor::Error),

    #[error_chain(custom)]
    #[error_chain(description = "library_error_description")]
    #[error_chain(display = "library_error_display")]
    Library(String),

    #[error_chain(custom)]
    #[error_chain(description = "request_error_description")]
    #[error_chain(display = "request_error_display")]
    Request(String)
}

fn cbor_error_display(f: &mut ::std::fmt::Formatter, e: &serde_cbor::Error)
    -> ::std::fmt::Result {

    match *e {
        serde_cbor::Error::Custom(ref s) => write!(f, "CBOR error: {}", s),
        _ => write!(f, "CBOR error: {}", e.description())
    }
}

fn library_error_description(_: &str) -> &str {
    "library error"
}

fn library_error_display(f: &mut ::std::fmt::Formatter, s: &str)
    -> ::std::fmt::Result {

    write!(f, "library error: {}", s)
}

fn request_error_description(_: &str) -> &str {
    "invalid request"
}

fn request_error_display(f: &mut ::std::fmt::Formatter, s: &str)
    -> ::std::fmt::Result {

    write!(f, "invalid request: {}", s)
}

impl<'a> From<sync::PoisonError<sync::MutexGuard<'a, Process>>> for Error {
    fn from(err: sync::PoisonError<sync::MutexGuard<'a, Process>>) -> Error {
        ErrorKind::Library(err.description().to_owned()).into()
    }
}

impl<'a> From<sync::PoisonError<sync::MutexGuard<'a, Thread>>> for Error {
    fn from(err: sync::PoisonError<sync::MutexGuard<'a, Thread>>) -> Error {
        ErrorKind::Library(err.description().to_owned()).into()
    }
}