Skip to content

Commit 8e0adef

Browse files
committed
refactor: chatgpt ports to n0-error
1 parent efa24eb commit 8e0adef

File tree

17 files changed

+211
-310
lines changed

17 files changed

+211
-310
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ irpc = { version = "0.10.0", features = ["rpc", "quinn_endpoint_setup", "spans",
4545
iroh-metrics = { version = "0.37" }
4646
redb = { version = "2.6.3", optional = true }
4747
reflink-copy = { version = "0.1.24", optional = true }
48+
n0-error = "0.1.0"
4849

4950
[dev-dependencies]
5051
clap = { version = "4.5.31", features = ["derive"] }

src/api.rs

Lines changed: 41 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use std::{io, net::SocketAddr, ops::Deref};
1717
use bao_tree::io::EncodeError;
1818
use iroh::Endpoint;
1919
use irpc::rpc::{listen, RemoteService};
20-
use n0_snafu::SpanTrace;
20+
use n0_error::e;
21+
use n0_error::stack_error;
2122
use nested_enum_utils::common_fields;
2223
use proto::{Request, ShutdownRequest, SyncDbRequest};
2324
use ref_cast::RefCast;
2425
use serde::{Deserialize, Serialize};
25-
use snafu::{Backtrace, IntoError, Snafu};
2626
use tags::Tags;
2727

2828
pub mod blobs;
@@ -35,75 +35,71 @@ pub use crate::{store::util::Tag, util::temp_tag::TempTag};
3535

3636
pub(crate) type ApiClient = irpc::Client<proto::Request>;
3737

38-
#[common_fields({
39-
backtrace: Option<Backtrace>,
40-
#[snafu(implicit)]
41-
span_trace: SpanTrace,
42-
})]
4338
#[allow(missing_docs)]
4439
#[non_exhaustive]
45-
#[derive(Debug, Snafu)]
40+
#[stack_error(derive, add_meta)]
4641
pub enum RequestError {
4742
/// Request failed due to rpc error.
48-
#[snafu(display("rpc error: {source}"))]
43+
#[error("rpc error: {source}")]
4944
Rpc { source: irpc::Error },
5045
/// Request failed due an actual error.
51-
#[snafu(display("inner error: {source}"))]
52-
Inner { source: Error },
46+
#[error("inner error: {source}")]
47+
Inner { #[error(std_err)] source: Error },
5348
}
5449

5550
impl From<irpc::Error> for RequestError {
5651
fn from(value: irpc::Error) -> Self {
57-
RpcSnafu.into_error(value)
52+
e!(RequestError::Rpc, value)
5853
}
5954
}
6055

6156
impl From<Error> for RequestError {
6257
fn from(value: Error) -> Self {
63-
InnerSnafu.into_error(value)
58+
e!(RequestError::Inner, value)
6459
}
6560
}
6661

6762
impl From<io::Error> for RequestError {
6863
fn from(value: io::Error) -> Self {
69-
InnerSnafu.into_error(value.into())
64+
e!(RequestError::Inner, value.into())
7065
}
7166
}
7267

7368
impl From<irpc::channel::mpsc::RecvError> for RequestError {
7469
fn from(value: irpc::channel::mpsc::RecvError) -> Self {
75-
RpcSnafu.into_error(value.into())
70+
e!(RequestError::Rpc, value.into())
7671
}
7772
}
7873

7974
pub type RequestResult<T> = std::result::Result<T, RequestError>;
8075

81-
#[common_fields({
82-
backtrace: Option<Backtrace>,
83-
#[snafu(implicit)]
84-
span_trace: SpanTrace,
85-
})]
8676
#[allow(missing_docs)]
8777
#[non_exhaustive]
88-
#[derive(Debug, Snafu)]
78+
#[stack_error(derive, add_meta, from_sources)]
8979
pub enum ExportBaoError {
90-
#[snafu(display("send error: {source}"))]
80+
#[error("send error")]
9181
Send { source: irpc::channel::SendError },
92-
#[snafu(display("mpsc recv error: {source}"))]
82+
#[error("mpsc recv error")]
9383
MpscRecv {
9484
source: irpc::channel::mpsc::RecvError,
9585
},
96-
#[snafu(display("oneshot recv error: {source}"))]
86+
#[error("oneshot recv error")]
9787
OneshotRecv {
9888
source: irpc::channel::oneshot::RecvError,
9989
},
100-
#[snafu(display("request error: {source}"))]
90+
#[error("request error")]
10191
Request { source: irpc::RequestError },
102-
#[snafu(display("io error: {source}"))]
103-
ExportBaoIo { source: io::Error },
104-
#[snafu(display("encode error: {source}"))]
105-
ExportBaoInner { source: bao_tree::io::EncodeError },
106-
#[snafu(display("client error: {source}"))]
92+
#[error("io error")]
93+
ExportBaoIo {
94+
#[error(std_err)]
95+
source: io::Error,
96+
},
97+
#[error("encode error")]
98+
ExportBaoInner {
99+
#[error(std_err)]
100+
source: bao_tree::io::EncodeError,
101+
},
102+
#[error("client error")]
107103
ClientError { source: ProgressError },
108104
}
109105

@@ -124,57 +120,15 @@ impl From<ExportBaoError> for Error {
124120
impl From<irpc::Error> for ExportBaoError {
125121
fn from(e: irpc::Error) -> Self {
126122
match e {
127-
irpc::Error::MpscRecv(e) => MpscRecvSnafu.into_error(e),
128-
irpc::Error::OneshotRecv(e) => OneshotRecvSnafu.into_error(e),
129-
irpc::Error::Send(e) => SendSnafu.into_error(e),
130-
irpc::Error::Request(e) => RequestSnafu.into_error(e),
131-
irpc::Error::Write(e) => ExportBaoIoSnafu.into_error(e.into()),
123+
irpc::Error::MpscRecv { source: e, .. } => e!(ExportBaoError::MpscRecv, e),
124+
irpc::Error::OneshotRecv { source: e, .. } => e!(ExportBaoError::OneshotRecv, e),
125+
irpc::Error::Send { source: e, .. } => e!(ExportBaoError::Send, e),
126+
irpc::Error::Request { source: e, .. } => e!(ExportBaoError::Request, e),
127+
irpc::Error::Write { source: e, .. } => e!(ExportBaoError::ExportBaoIo, e.into()),
132128
}
133129
}
134130
}
135131

136-
impl From<io::Error> for ExportBaoError {
137-
fn from(value: io::Error) -> Self {
138-
ExportBaoIoSnafu.into_error(value)
139-
}
140-
}
141-
142-
impl From<irpc::channel::mpsc::RecvError> for ExportBaoError {
143-
fn from(value: irpc::channel::mpsc::RecvError) -> Self {
144-
MpscRecvSnafu.into_error(value)
145-
}
146-
}
147-
148-
impl From<irpc::channel::oneshot::RecvError> for ExportBaoError {
149-
fn from(value: irpc::channel::oneshot::RecvError) -> Self {
150-
OneshotRecvSnafu.into_error(value)
151-
}
152-
}
153-
154-
impl From<irpc::channel::SendError> for ExportBaoError {
155-
fn from(value: irpc::channel::SendError) -> Self {
156-
SendSnafu.into_error(value)
157-
}
158-
}
159-
160-
impl From<irpc::RequestError> for ExportBaoError {
161-
fn from(value: irpc::RequestError) -> Self {
162-
RequestSnafu.into_error(value)
163-
}
164-
}
165-
166-
impl From<bao_tree::io::EncodeError> for ExportBaoError {
167-
fn from(value: bao_tree::io::EncodeError) -> Self {
168-
ExportBaoInnerSnafu.into_error(value)
169-
}
170-
}
171-
172-
impl From<ProgressError> for ExportBaoError {
173-
fn from(value: ProgressError) -> Self {
174-
ClientSnafu.into_error(value)
175-
}
176-
}
177-
178132
pub type ExportBaoResult<T> = std::result::Result<T, ExportBaoError>;
179133

180134
#[derive(Debug, derive_more::Display, derive_more::From, Serialize, Deserialize)]
@@ -196,13 +150,11 @@ impl Error {
196150
E: Into<Box<dyn std::error::Error + Send + Sync>>,
197151
{
198152
Self::Io(io::Error::other(msg.into()))
199-
}
153+
}
200154
}
201155

202156
impl From<irpc::Error> for Error {
203-
fn from(e: irpc::Error) -> Self {
204-
Self::Io(e.into())
205-
}
157+
fn from(e: irpc::Error) -> Self { Self::Io(e.into()) }
206158
}
207159

208160
impl From<RequestError> for Error {
@@ -215,27 +167,19 @@ impl From<RequestError> for Error {
215167
}
216168

217169
impl From<irpc::channel::mpsc::RecvError> for Error {
218-
fn from(e: irpc::channel::mpsc::RecvError) -> Self {
219-
Self::Io(e.into())
220-
}
170+
fn from(e: irpc::channel::mpsc::RecvError) -> Self { Self::Io(e.into()) }
221171
}
222172

223173
impl From<irpc::rpc::WriteError> for Error {
224-
fn from(e: irpc::rpc::WriteError) -> Self {
225-
Self::Io(e.into())
226-
}
174+
fn from(e: irpc::rpc::WriteError) -> Self { Self::Io(e.into()) }
227175
}
228176

229177
impl From<irpc::RequestError> for Error {
230-
fn from(e: irpc::RequestError) -> Self {
231-
Self::Io(e.into())
232-
}
178+
fn from(e: irpc::RequestError) -> Self { Self::Io(e.into()) }
233179
}
234180

235181
impl From<irpc::channel::SendError> for Error {
236-
fn from(e: irpc::channel::SendError) -> Self {
237-
Self::Io(e.into())
238-
}
182+
fn from(e: irpc::channel::SendError) -> Self { Self::Io(e.into()) }
239183
}
240184

241185
impl std::error::Error for Error {
@@ -255,6 +199,10 @@ impl From<EncodeError> for Error {
255199
}
256200
}
257201

202+
impl From<io::Error> for Error {
203+
fn from(e: io::Error) -> Self { Self::Io(e) }
204+
}
205+
258206
pub type Result<T> = std::result::Result<T, Error>;
259207

260208
/// The main entry point for the store API.

0 commit comments

Comments
 (0)