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
use failure::Error;

pub type Result<T> = ::std::result::Result<T, Error>;

use failure::Backtrace;
use std::fmt::Display;

#[derive(Debug, Fail)]
pub enum SectorBuilderErr {
    #[fail(
        display = "number of bytes in piece ({}) exceeds maximum ({})",
        num_bytes_in_piece, max_bytes_per_sector
    )]
    OverflowError {
        num_bytes_in_piece: u64,
        max_bytes_per_sector: u64,
    },

    #[fail(
        display = "number of bytes written ({}) does not match bytes in piece ({})",
        num_bytes_written, num_bytes_in_piece
    )]
    IncompleteWriteError {
        num_bytes_written: u64,
        num_bytes_in_piece: u64,
    },

    #[fail(display = "no piece with key {} found", _0)]
    PieceNotFound(String),

    #[fail(display = "unrecoverable error: {}", _0)]
    Unrecoverable(String, Backtrace),
}

pub fn err_piecenotfound(piece_key: String) -> SectorBuilderErr {
    SectorBuilderErr::PieceNotFound(piece_key)
}

pub fn err_unrecov<S: Display>(msg: S) -> SectorBuilderErr {
    let backtrace = failure::Backtrace::new();
    SectorBuilderErr::Unrecoverable(format!("{}", msg), backtrace)
}

pub fn err_overflow(num_bytes_in_piece: u64, max_bytes_per_sector: u64) -> SectorBuilderErr {
    SectorBuilderErr::OverflowError {
        num_bytes_in_piece,
        max_bytes_per_sector,
    }
}

pub fn err_inc_write(num_bytes_written: u64, num_bytes_in_piece: u64) -> SectorBuilderErr {
    SectorBuilderErr::IncompleteWriteError {
        num_bytes_written,
        num_bytes_in_piece,
    }
}

#[derive(Debug, Fail)]
pub enum SectorManagerErr {
    #[fail(display = "unclassified error: {}", _0)]
    UnclassifiedError(String),

    #[fail(display = "caller error: {}", _0)]
    CallerError(String),

    #[fail(display = "receiver error: {}", _0)]
    ReceiverError(String),
}