Skip to main content

reverie/
error.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Error handling.
10
11pub use reverie_syscalls::Errno;
12use thiserror::Error;
13
14/// A general error.
15#[derive(Error, Debug)]
16pub enum Error {
17    /// A low-level errno.
18    #[error(transparent)]
19    Errno(#[from] Errno),
20
21    /// A generic error that may be produced by the tool.
22    #[error(transparent)]
23    Tool(#[from] anyhow::Error),
24
25    /// An I/O error.
26    #[error(transparent)]
27    Io(#[from] std::io::Error),
28}
29
30impl Error {
31    /// Extracts the errno from the error. If this is not an `Error::Errno`, then
32    /// returns `Err(Error)`. This is useful for capturing syscall errors and
33    /// propagating all other types of errors.
34    pub fn into_errno(self) -> Result<Errno, Self> {
35        if let Self::Errno(err) = self {
36            Ok(err)
37        } else {
38            Err(self)
39        }
40    }
41}
42
43impl From<nix::errno::Errno> for Error {
44    fn from(err: nix::errno::Errno) -> Self {
45        Self::Errno(Errno::new(err as i32))
46    }
47}