circuit/
error.rs

1// Copyright 2022 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 thiserror::Error;
6
7pub type Result<T, E = Error> = std::result::Result<T, E>;
8
9#[derive(Error, Debug)]
10pub enum Error {
11    #[error("Received invalid stream ID")]
12    BadStreamId,
13    #[error("String `{0}` is too long to be encoded on the wire")]
14    StringTooBig(String),
15    #[error("Not enough data in buffer, need {0}")]
16    BufferTooShort(usize),
17    #[error("Got BufferTooShort({0}) from user callback after giving buffer of size {1}")]
18    CallbackRejectedBuffer(usize, usize),
19    #[error("Bad characters in UTF8 String `{0}`")]
20    BadUTF8(String),
21    // Change this to be more explicit that the connection closed is coming from the target.
22    // Example: "Connection closed from the target. Reason: "remote_control_runner closed the
23    // connection"."
24    #[error("Connection closed from the target. Reason: \"{}\"", .0.as_deref().unwrap_or("not given"))]
25    ConnectionClosed(Option<String>),
26    #[error("Version mismatch")]
27    VersionMismatch,
28    #[error("Protocol mismatch")]
29    ProtocolMismatch,
30    #[error("Link speed value 255 is reserved")]
31    InvalidSpeed,
32    #[error("An internal channel closed unexpectedly")]
33    InternalPipeBroken,
34    #[error("Could not find peer with node ID `{0}`")]
35    NoSuchPeer(String),
36    #[error("IO Error")]
37    IO(#[from] std::io::Error),
38    #[error("Loopback connections are unsupported")]
39    LoopbackUnsupported,
40}
41
42pub(crate) trait ExtendBufferTooShortError {
43    fn extend_buffer_too_short(self, by: usize) -> Self;
44}
45
46impl<T> ExtendBufferTooShortError for Result<T> {
47    fn extend_buffer_too_short(self, by: usize) -> Self {
48        match self {
49            Err(Error::BufferTooShort(s)) => Err(Error::BufferTooShort(s + by)),
50            other => other,
51        }
52    }
53}