jacquard-common 0.10.0

Core AT Protocol types and utilities for Jacquard
Documentation
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Error types for XRPC client operations

use crate::xrpc::EncodeError;
use alloc::boxed::Box;
use alloc::string::ToString;
use bytes::Bytes;
use smol_str::SmolStr;

#[cfg(feature = "std")]
use miette::Diagnostic;

/// Boxed error type for wrapping arbitrary errors
pub type BoxError = Box<dyn core::error::Error + Send + Sync + 'static>;

/// Client error type for all XRPC client operations
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "std", derive(Diagnostic))]
#[error("{kind}")]
pub struct ClientError {
    #[cfg_attr(feature = "std", diagnostic_source)]
    kind: ClientErrorKind,
    #[source]
    source: Option<BoxError>,
    #[cfg_attr(feature = "std", help)]
    help: Option<SmolStr>,
    context: Option<SmolStr>,
    url: Option<SmolStr>,
    details: Option<SmolStr>,
    location: Option<SmolStr>,
}

/// Error categories for client operations
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "std", derive(Diagnostic))]
#[non_exhaustive]
pub enum ClientErrorKind {
    /// HTTP transport error (connection, timeout, etc.)
    #[error("transport error")]
    #[cfg_attr(feature = "std", diagnostic(code(jacquard::client::transport)))]
    Transport,

    /// Request validation/construction failed
    #[error("invalid request: {0}")]
    #[cfg_attr(
        feature = "std",
        diagnostic(
            code(jacquard::client::invalid_request),
            help("check request parameters and format")
        )
    )]
    InvalidRequest(SmolStr),

    /// Request serialization failed
    #[error("encode error: {0}")]
    #[cfg_attr(
        feature = "std",
        diagnostic(
            code(jacquard::client::encode),
            help("check request body format and encoding")
        )
    )]
    Encode(SmolStr),

    /// Response deserialization failed
    #[error("decode error: {0}")]
    #[cfg_attr(
        feature = "std",
        diagnostic(
            code(jacquard::client::decode),
            help("check response format and encoding")
        )
    )]
    Decode(SmolStr),

    /// HTTP error response (non-200 status)
    #[error("HTTP {status}")]
    #[cfg_attr(feature = "std", diagnostic(code(jacquard::client::http)))]
    Http {
        /// HTTP status code
        status: http::StatusCode,
    },

    /// Authentication/authorization error
    #[error("auth error: {0}")]
    #[cfg_attr(feature = "std", diagnostic(code(jacquard::client::auth)))]
    Auth(AuthError),

    /// Identity resolution error (handle→DID, DID→Doc)
    #[error("identity resolution failed")]
    #[cfg_attr(
        feature = "std",
        diagnostic(
            code(jacquard::client::identity_resolution),
            help("check handle/DID is valid and network is accessible")
        )
    )]
    IdentityResolution,

    /// Storage/persistence error
    #[error("storage error")]
    #[cfg_attr(
        feature = "std",
        diagnostic(
            code(jacquard::client::storage),
            help("check storage backend is accessible and has sufficient permissions")
        )
    )]
    Storage,
}

impl ClientError {
    /// Create a new error with the given kind and optional source
    pub fn new(kind: ClientErrorKind, source: Option<BoxError>) -> Self {
        Self {
            kind,
            source,
            help: None,
            context: None,
            url: None,
            details: None,
            location: None,
        }
    }

    /// Get the error kind
    pub fn kind(&self) -> &ClientErrorKind {
        &self.kind
    }

    /// Get the source error if present
    pub fn source_err(&self) -> Option<&BoxError> {
        self.source.as_ref()
    }

    /// Get the context string if present
    pub fn context(&self) -> Option<&str> {
        self.context.as_ref().map(|s| s.as_str())
    }

    /// Get the URL if present
    pub fn url(&self) -> Option<&str> {
        self.url.as_ref().map(|s| s.as_str())
    }

    /// Get the details if present
    pub fn details(&self) -> Option<&str> {
        self.details.as_ref().map(|s| s.as_str())
    }

    /// Get the location if present
    pub fn location(&self) -> Option<&str> {
        self.location.as_ref().map(|s| s.as_str())
    }

    /// Add help text to this error
    pub fn with_help(mut self, help: impl Into<SmolStr>) -> Self {
        self.help = Some(help.into());
        self
    }

    /// Add context to this error
    pub fn with_context(mut self, context: impl Into<SmolStr>) -> Self {
        self.context = Some(context.into());
        self
    }

    /// Add URL to this error
    pub fn with_url(mut self, url: impl Into<SmolStr>) -> Self {
        self.url = Some(url.into());
        self
    }

    /// Add details to this error
    pub fn with_details(mut self, details: impl Into<SmolStr>) -> Self {
        self.details = Some(details.into());
        self
    }

    /// Add location to this error
    pub fn with_location(mut self, location: impl Into<SmolStr>) -> Self {
        self.location = Some(location.into());
        self
    }

    /// Append additional context to existing context string.
    ///
    /// If context already exists, appends with ": " separator.
    /// If no context exists, sets it directly.
    pub fn append_context(mut self, additional: impl AsRef<str>) -> Self {
        self.context = Some(match self.context.take() {
            Some(existing) => smol_str::format_smolstr!("{}: {}", existing, additional.as_ref()),
            None => additional.as_ref().into(),
        });
        self
    }

    /// Add NSID context for XRPC operations.
    ///
    /// Appends the NSID in brackets to existing context, e.g. `"network timeout: [com.atproto.repo.getRecord]"`.
    pub fn for_nsid(self, nsid: &str) -> Self {
        self.append_context(smol_str::format_smolstr!("[{}]", nsid))
    }

    /// Add collection context for record operations.
    ///
    /// Use this when a record operation fails to indicate the target collection.
    pub fn for_collection(self, operation: &str, collection_nsid: &str) -> Self {
        self.append_context(smol_str::format_smolstr!(
            "{} [{}]",
            operation,
            collection_nsid
        ))
    }

    // Constructors for each kind

    /// Create a transport error
    pub fn transport(source: impl core::error::Error + Send + Sync + 'static) -> Self {
        Self::new(ClientErrorKind::Transport, Some(Box::new(source)))
    }

    /// Create an invalid request error
    pub fn invalid_request(msg: impl Into<SmolStr>) -> Self {
        Self::new(ClientErrorKind::InvalidRequest(msg.into()), None)
    }

    /// Create an encode error
    pub fn encode(msg: impl Into<SmolStr>) -> Self {
        Self::new(ClientErrorKind::Encode(msg.into()), None)
    }

    /// Create a decode error
    pub fn decode(msg: impl Into<SmolStr>) -> Self {
        Self::new(ClientErrorKind::Decode(msg.into()), None)
    }

    /// Create an HTTP error with status code and optional body
    pub fn http(status: http::StatusCode, body: Option<Bytes>) -> Self {
        let http_err = HttpError { status, body };
        Self::new(ClientErrorKind::Http { status }, Some(Box::new(http_err)))
    }

    /// Create an authentication error
    pub fn auth(auth_error: AuthError) -> Self {
        Self::new(ClientErrorKind::Auth(auth_error), None)
    }

    /// Create an identity resolution error
    pub fn identity_resolution(source: impl core::error::Error + Send + Sync + 'static) -> Self {
        Self::new(ClientErrorKind::IdentityResolution, Some(Box::new(source)))
    }

    /// Create a storage error
    pub fn storage(source: impl core::error::Error + Send + Sync + 'static) -> Self {
        Self::new(ClientErrorKind::Storage, Some(Box::new(source)))
    }
}

/// Result type for client operations
pub type XrpcResult<T> = Result<T, ClientError>;

// ============================================================================
// Old error types (deprecated)
// ============================================================================

/// Response deserialization errors
///
/// Preserves detailed error information from various deserialization backends.
/// Can be converted to string for serialization while maintaining the full error context.
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "std", derive(Diagnostic))]
#[non_exhaustive]
pub enum DecodeError {
    /// JSON deserialization failed
    #[error("Failed to deserialize JSON: {0}")]
    Json(
        #[from]
        #[source]
        serde_json::Error,
    ),
    /// CBOR deserialization failed (local I/O)
    #[cfg(feature = "std")]
    #[error("Failed to deserialize CBOR: {0}")]
    CborLocal(
        #[from]
        #[source]
        serde_ipld_dagcbor::DecodeError<std::io::Error>,
    ),
    /// CBOR deserialization failed (remote/reqwest)
    #[error("Failed to deserialize CBOR: {0}")]
    CborRemote(
        #[from]
        #[source]
        serde_ipld_dagcbor::DecodeError<HttpError>,
    ),
    /// DAG-CBOR deserialization failed (in-memory, e.g., WebSocket frames)
    #[error("Failed to deserialize DAG-CBOR: {0}")]
    DagCborInfallible(
        #[from]
        #[source]
        serde_ipld_dagcbor::DecodeError<core::convert::Infallible>,
    ),
    /// CBOR header deserialization failed (framed WebSocket messages)
    #[cfg(all(feature = "websocket", feature = "std"))]
    #[error("Failed to deserialize cbor header: {0}")]
    CborHeader(
        #[from]
        #[source]
        ciborium::de::Error<std::io::Error>,
    ),

    /// CBOR header deserialization failed (framed WebSocket messages, no_std)
    #[cfg(all(feature = "websocket", not(feature = "std")))]
    #[error("Failed to deserialize cbor header: {0}")]
    CborHeader(
        #[from]
        #[source]
        ciborium::de::Error<core::convert::Infallible>,
    ),

    /// Unknown event type in framed message
    #[cfg(feature = "websocket")]
    #[error("Unknown event type: {0}")]
    UnknownEventType(smol_str::SmolStr),
}

/// HTTP error response (non-200 status codes outside of XRPC error handling)
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "std", derive(Diagnostic))]
pub struct HttpError {
    /// HTTP status code
    pub status: http::StatusCode,
    /// Response body if available
    pub body: Option<Bytes>,
}

impl core::fmt::Display for HttpError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "HTTP {}", self.status)?;
        if let Some(body) = &self.body {
            if let Ok(s) = core::str::from_utf8(body) {
                write!(f, ":\n{}", s)?;
            }
        }
        Ok(())
    }
}

/// Authentication and authorization errors
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "std", derive(Diagnostic))]
#[non_exhaustive]
pub enum AuthError {
    /// Access token has expired (use refresh token to get a new one)
    #[error("Access token expired")]
    TokenExpired,

    /// Access token is invalid or malformed
    #[error("Invalid access token")]
    InvalidToken,

    /// Token refresh request failed
    #[error("Token refresh failed")]
    RefreshFailed,

    /// Request requires authentication but none was provided
    #[error("No authentication provided, but endpoint requires auth")]
    NotAuthenticated,

    /// DPoP proof construction failed (key or signing issue)
    #[error("DPoP proof construction failed")]
    DpopProofFailed,

    /// DPoP nonce retry failed (server rejected proof even after nonce update)
    #[error("DPoP nonce negotiation failed")]
    DpopNonceFailed,

    /// Other authentication error
    #[error("Authentication error: {0:?}")]
    Other(http::HeaderValue),
}

impl crate::IntoStatic for AuthError {
    type Output = AuthError;

    fn into_static(self) -> Self::Output {
        match self {
            AuthError::TokenExpired => AuthError::TokenExpired,
            AuthError::InvalidToken => AuthError::InvalidToken,
            AuthError::RefreshFailed => AuthError::RefreshFailed,
            AuthError::NotAuthenticated => AuthError::NotAuthenticated,
            AuthError::DpopProofFailed => AuthError::DpopProofFailed,
            AuthError::DpopNonceFailed => AuthError::DpopNonceFailed,
            AuthError::Other(header) => AuthError::Other(header),
        }
    }
}

// ============================================================================
// Conversions from old to new
// ============================================================================

impl From<DecodeError> for ClientError {
    fn from(e: DecodeError) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Decode(msg), Some(Box::new(e)))
            .with_context("response deserialization failed")
    }
}

impl From<HttpError> for ClientError {
    fn from(e: HttpError) -> Self {
        Self::http(e.status, e.body)
    }
}

impl From<AuthError> for ClientError {
    fn from(e: AuthError) -> Self {
        Self::auth(e)
    }
}

impl From<EncodeError> for ClientError {
    fn from(e: EncodeError) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Encode(msg), Some(Box::new(e)))
            .with_context("request encoding failed")
    }
}

// Platform-specific conversions
#[cfg(feature = "reqwest-client")]
impl From<reqwest::Error> for ClientError {
    #[cfg(not(target_arch = "wasm32"))]
    fn from(e: reqwest::Error) -> Self {
        Self::transport(e)
    }

    #[cfg(target_arch = "wasm32")]
    fn from(e: reqwest::Error) -> Self {
        Self::transport(e)
    }
}

// Serde error conversions
impl From<serde_json::Error> for ClientError {
    fn from(e: serde_json::Error) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Decode(msg), Some(Box::new(e)))
            .with_context("JSON deserialization failed")
    }
}

#[cfg(feature = "std")]
impl From<serde_ipld_dagcbor::DecodeError<std::io::Error>> for ClientError {
    fn from(e: serde_ipld_dagcbor::DecodeError<std::io::Error>) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Decode(msg), Some(Box::new(e)))
            .with_context("DAG-CBOR deserialization failed (local I/O)")
    }
}

impl From<serde_ipld_dagcbor::DecodeError<HttpError>> for ClientError {
    fn from(e: serde_ipld_dagcbor::DecodeError<HttpError>) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Decode(msg), Some(Box::new(e)))
            .with_context("DAG-CBOR deserialization failed (remote)")
    }
}

impl From<serde_ipld_dagcbor::DecodeError<core::convert::Infallible>> for ClientError {
    fn from(e: serde_ipld_dagcbor::DecodeError<core::convert::Infallible>) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Decode(msg), Some(Box::new(e)))
            .with_context("DAG-CBOR deserialization failed (in-memory)")
    }
}

#[cfg(all(feature = "websocket", feature = "std"))]
impl From<ciborium::de::Error<std::io::Error>> for ClientError {
    fn from(e: ciborium::de::Error<std::io::Error>) -> Self {
        let msg = smol_str::format_smolstr!("{:?}", e);
        Self::new(ClientErrorKind::Decode(msg), Some(Box::new(e)))
            .with_context("CBOR header deserialization failed")
    }
}

// Session store errors
impl From<crate::session::SessionStoreError> for ClientError {
    fn from(e: crate::session::SessionStoreError) -> Self {
        Self::storage(e)
    }
}

// fluent_uri parse errors
impl From<crate::deps::fluent_uri::ParseError> for ClientError {
    fn from(e: crate::deps::fluent_uri::ParseError) -> Self {
        Self::invalid_request(e.to_string())
    }
}