fuchsia_bluetooth/
error.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_bluetooth as bt;
6use thiserror::Error;
7
8/// Errors that occur in the fuchsia-bluetooth crate.
9#[derive(Debug, Error)]
10pub enum Error {
11    #[error("Error using the `bredr.Profile` resource: {}", .0)]
12    Profile(String),
13
14    #[error("Error using the `sys` resource: {}", .0)]
15    Sys(String),
16
17    #[error("Error using the `le` resource: {}", .0)]
18    LE(String),
19
20    #[error("Conversion to/from a type failed: {}", .0)]
21    FailedConversion(String),
22
23    #[error("Mandatory field {} is missing", .0)]
24    MissingRequired(String),
25
26    #[error("FIDL Error: {0}")]
27    Fidl(#[from] fidl::Error),
28
29    /// An error from another source
30    #[error(transparent)]
31    Other(#[from] anyhow::Error),
32}
33
34impl Error {
35    pub fn profile(msg: impl Into<String>) -> Self {
36        Self::Profile(msg.into())
37    }
38
39    pub fn sys(msg: impl Into<String>) -> Self {
40        Self::Sys(msg.into())
41    }
42
43    pub fn le(msg: impl Into<String>) -> Self {
44        Self::LE(msg.into())
45    }
46
47    pub fn other(msg: impl Into<String>) -> Self {
48        Self::Other(anyhow::format_err!("{}", msg.into()))
49    }
50
51    pub fn external(e: impl Into<anyhow::Error>) -> Self {
52        Self::Other(e.into())
53    }
54
55    pub fn missing(msg: impl Into<String>) -> Self {
56        Self::MissingRequired(msg.into())
57    }
58
59    pub fn conversion(msg: impl Into<String>) -> Self {
60        Self::FailedConversion(msg.into())
61    }
62}
63
64impl From<bt::Error> for Error {
65    fn from(err: bt::Error) -> Error {
66        let message = err.description.unwrap_or_else(|| "unknown Bluetooth FIDL error".to_string());
67        Error::other(message)
68    }
69}
70
71impl From<bt::ErrorCode> for Error {
72    fn from(err: bt::ErrorCode) -> Error {
73        let message = format!("Bluetooth Error Code {err:?}");
74        Error::other(message)
75    }
76}