fuchsia_bluetooth/
error.rs

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
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_fuchsia_bluetooth as bt;
use thiserror::Error;

/// Errors that occur in the fuchsia-bluetooth crate.
#[derive(Debug, Error)]
pub enum Error {
    #[error("Error using the `bredr.Profile` resource: {}", .0)]
    Profile(String),

    #[error("Error using the `sys` resource: {}", .0)]
    Sys(String),

    #[error("Error using the `le` resource: {}", .0)]
    LE(String),

    #[error("Conversion to/from a type failed: {}", .0)]
    FailedConversion(String),

    #[error("Mandatory field {} is missing", .0)]
    MissingRequired(String),

    #[error("FIDL Error: {0}")]
    Fidl(#[from] fidl::Error),

    /// An error from another source
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl Error {
    pub fn profile(msg: impl Into<String>) -> Self {
        Self::Profile(msg.into())
    }

    pub fn sys(msg: impl Into<String>) -> Self {
        Self::Sys(msg.into())
    }

    pub fn le(msg: impl Into<String>) -> Self {
        Self::LE(msg.into())
    }

    pub fn other(msg: impl Into<String>) -> Self {
        Self::Other(anyhow::format_err!("{}", msg.into()))
    }

    pub fn external(e: impl Into<anyhow::Error>) -> Self {
        Self::Other(e.into())
    }

    pub fn missing(msg: impl Into<String>) -> Self {
        Self::MissingRequired(msg.into())
    }

    pub fn conversion(msg: impl Into<String>) -> Self {
        Self::FailedConversion(msg.into())
    }
}

impl From<bt::Error> for Error {
    fn from(err: bt::Error) -> Error {
        let message = err.description.unwrap_or_else(|| "unknown Bluetooth FIDL error".to_string());
        Error::other(message)
    }
}

impl From<bt::ErrorCode> for Error {
    fn from(err: bt::ErrorCode) -> Error {
        let message = format!("Bluetooth Error Code {err:?}");
        Error::other(message)
    }
}