scopedb-client 0.4.2

The official Rust client for ScopeDB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright 2024 ScopeDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;
use std::time::Duration;

use reqwest::StatusCode;
use uuid::Uuid;

use crate::protocol::AppendErrorDetails;
use crate::protocol::AppendState;
use crate::protocol::StatementErrorDetails;

/// ErrorKind is all kinds of Error of ScopeDB client.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
    /// The client don't know what happened here, and no actions other than just
    /// returning it back.
    Unexpected,

    /// The config for ScopeDB client is invalid.
    ConfigInvalid,

    /// A table append was rejected or its commit outcome is unknown.
    AppendRowsFailed,

    /// A statement reached a failed terminal state.
    StatementFailed,

    /// A statement reached the cancelled terminal state.
    StatementCancelled,

    /// A declarative catalog operation failed.
    CatalogFailed,
}

/// The client-side reason an in-flight write was interrupted.
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum WriteErrorReason {
    /// A client or transport deadline expired.
    Timeout,
}

impl fmt::Display for WriteErrorReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Timeout => "timeout",
        })
    }
}

/// Details of an interrupted write. An unknown outcome may have committed and
/// must not be automatically replayed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct WriteErrorDetails {
    pub reason: WriteErrorReason,
    pub outcome: AppendState,
}

impl ErrorKind {
    /// Convert self into static str.
    pub fn into_static(self) -> &'static str {
        self.into()
    }
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.into_static())
    }
}

impl From<ErrorKind> for &'static str {
    fn from(v: ErrorKind) -> &'static str {
        match v {
            ErrorKind::Unexpected => "Unexpected",
            ErrorKind::ConfigInvalid => "ConfigInvalid",
            ErrorKind::AppendRowsFailed => "AppendRowsFailed",
            ErrorKind::StatementFailed => "StatementFailed",
            ErrorKind::StatementCancelled => "StatementCancelled",
            ErrorKind::CatalogFailed => "CatalogFailed",
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ErrorStatus {
    /// Permanent means without external changes, the error never changes.
    ///
    /// For example, underlying services returns a not found error.
    ///
    /// Users SHOULD never retry this operation.
    Permanent,
    /// Temporary means this error is returned for temporary.
    ///
    /// For example, underlying services is rate limited or unavailable for temporary.
    ///
    /// Users CAN retry the operation to resolve it.
    Temporary,
    /// Persistent means the error is not classified as retryable.
    ///
    /// For example, a retry budget was exhausted or a write has an unknown outcome.
    ///
    /// Append streams may explicitly replay transient unknown outcomes under
    /// their delivery policy. Client timeouts are never automatically replayed.
    Persistent,
}

#[derive(Debug, Default, Clone)]
struct HttpErrorMetadata {
    status: Option<StatusCode>,
    request_id: Option<String>,
    retry_after: Option<Duration>,
    retryable: Option<bool>,
}

#[derive(Debug, Clone)]
struct ApiErrorDetails {
    code: Option<String>,
    details: Option<serde_json::Value>,
}

#[derive(Debug, Default, Clone)]
struct StatementErrorMetadata {
    id: Option<Uuid>,
    details: Option<StatementErrorDetails>,
}

impl fmt::Display for ErrorStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorStatus::Permanent => write!(f, "permanent"),
            ErrorStatus::Temporary => write!(f, "temporary"),
            ErrorStatus::Persistent => write!(f, "persistent"),
        }
    }
}

/// A failure with a message, optional HTTP diagnostics, and operation details.
///
/// Start with [`Self::message`], [`Self::http_status`], and [`Self::request_id`].
/// Use [`Self::statement_details`] for a failed query, [`Self::append_details`]
/// for a rejected or uncertain append, and [`Self::write_details`] for a write
/// timeout. A timed-out write may already have committed; reconcile it before
/// replaying.
///
/// [`Self::is_retryable`] describes the failed operation. For a query that was
/// already submitted, resume its [`Self::statement_id`] through
/// [`crate::Client::statement_handle`] instead of submitting another query.
#[derive(Clone)]
pub struct Error {
    kind: ErrorKind,
    message: String,

    status: ErrorStatus,
    context: Vec<(&'static str, String)>,
    api_error: Option<Box<ApiErrorDetails>>,
    append_details: Option<AppendErrorDetails>,
    write_details: Option<WriteErrorDetails>,
    statement_metadata: Option<Box<StatementErrorMetadata>>,
    http_metadata: Option<Box<HttpErrorMetadata>>,

    source: Option<std::sync::Arc<anyhow::Error>>,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.kind, self.status)?;

        if !self.context.is_empty() {
            write!(f, ", context: {{ ")?;
            write!(
                f,
                "{}",
                self.context
                    .iter()
                    .map(|(k, v)| format!("{k}: {v}"))
                    .collect::<Vec<_>>()
                    .join(", ")
            )?;
            write!(f, " }}")?;
        }

        if !self.message.is_empty() {
            write!(f, " => {}", self.message)?;
        }

        if let Some(code) = self.code() {
            write!(f, ", code: {code}")?;
        }
        if let Some(id) = self.statement_id() {
            write!(f, ", statement_id: {id}")?;
        }
        if let Some(status) = self.http_status() {
            write!(f, ", http_status: {}", status.as_u16())?;
        }
        if let Some(request_id) = self.request_id() {
            write!(f, ", request_id: {request_id}")?;
        }
        if let Some(retry_after) = self.retry_after() {
            write!(f, ", retry_after: {retry_after:?}")?;
        }

        if let Some(details) = self.write_details() {
            write!(
                f,
                ", write_reason: {}, write_outcome: {}",
                details.reason, details.outcome
            )?;
        }

        if let Some(source) = &self.source {
            write!(f, ", source: {source}")?;
        }

        Ok(())
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // If alternate has been specified, we will print like Debug.
        if f.alternate() {
            let mut de = f.debug_struct("Error");
            de.field("kind", &self.kind);
            de.field("message", &self.message);
            de.field("api_error", &self.api_error);
            de.field("status", &self.status);
            de.field("context", &self.context);
            de.field("append_details", &self.append_details);
            de.field("write_details", &self.write_details);
            de.field("statement_id", &self.statement_id());
            de.field("statement_details", &self.statement_details());
            de.field("http_metadata", &self.http_metadata);
            de.field("source", &self.source);
            return de.finish();
        }

        write!(f, "{} ({})", self.kind, self.status)?;
        if !self.message.is_empty() {
            write!(f, " => {}", self.message)?;
        }
        if let Some(code) = self.code() {
            write!(f, ", code: {code}")?;
        }
        if let Some(id) = self.statement_id() {
            write!(f, ", statement_id: {id}")?;
        }
        if let Some(status) = self.http_status() {
            write!(f, ", http_status: {}", status.as_u16())?;
        }
        if let Some(request_id) = self.request_id() {
            write!(f, ", request_id: {request_id}")?;
        }
        if let Some(retry_after) = self.retry_after() {
            write!(f, ", retry_after: {retry_after:?}")?;
        }
        if let Some(details) = self.write_details() {
            write!(
                f,
                ", write_reason: {}, write_outcome: {}",
                details.reason, details.outcome
            )?;
        }
        writeln!(f)?;

        if !self.context.is_empty() {
            writeln!(f)?;
            writeln!(f, "Context:")?;
            for (k, v) in self.context.iter() {
                writeln!(f, "   {k}: {v}")?;
            }
        }
        if let Some(source) = &self.source {
            writeln!(f)?;
            writeln!(f, "Source:")?;
            writeln!(f, "   {source:#}")?;
        }

        Ok(())
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source.as_ref().map(|v| v.as_ref().as_ref())
    }
}

impl Error {
    /// Create a new Error with error kind and message.
    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),

            status: ErrorStatus::Permanent,
            context: Vec::default(),
            api_error: None,
            append_details: None,
            write_details: None,
            statement_metadata: None,
            http_metadata: None,
            source: None,
        }
    }

    /// Add more context in error.
    pub fn with_context(mut self, key: &'static str, value: impl ToString) -> Self {
        self.context.push((key, value.to_string()));
        self
    }

    /// Set source for error.
    ///
    /// # Notes
    ///
    /// If the source has been set, we will raise a panic here.
    pub fn set_source(mut self, src: impl Into<anyhow::Error>) -> Self {
        debug_assert!(self.source.is_none(), "the source error has been set");

        self.source = Some(std::sync::Arc::new(src.into()));
        self
    }

    pub(crate) fn set_write_timeout(mut self) -> Self {
        self.write_details = Some(WriteErrorDetails {
            reason: WriteErrorReason::Timeout,
            outcome: AppendState::Unknown,
        });
        self
    }

    /// Returns client timeout details for a table append after sending began.
    /// Existing error kinds, sources, and HTTP diagnostics remain available.
    pub fn write_details(&self) -> Option<&WriteErrorDetails> {
        self.write_details.as_ref()
    }

    /// Return structured table-append failure details, when available.
    pub fn append_details(&self) -> Option<&AppendErrorDetails> {
        self.append_details.as_ref()
    }

    pub(crate) fn set_append_details(mut self, details: AppendErrorDetails) -> Self {
        self.append_details = Some(details);
        self
    }

    /// Return structured statement failure details, when available.
    pub fn statement_details(&self) -> Option<&StatementErrorDetails> {
        self.statement_metadata
            .as_ref()
            .and_then(|m| m.details.as_ref())
    }

    pub(crate) fn set_statement_details(mut self, details: StatementErrorDetails) -> Self {
        self.statement_metadata
            .get_or_insert_with(Default::default)
            .details = Some(details);
        self
    }

    /// Returns the statement identifier, when known. Use [`crate::Client::statement_handle`]
    /// to inspect the existing execution instead of resubmitting it. An identifier
    /// supplied before submission does not confirm that the server accepted it.
    pub fn statement_id(&self) -> Option<Uuid> {
        self.statement_metadata.as_ref().and_then(|m| m.id)
    }

    pub(crate) fn set_statement_id(mut self, id: Uuid) -> Self {
        self.statement_metadata
            .get_or_insert_with(Default::default)
            .id = Some(id);
        self
    }

    pub(crate) fn set_http_status(mut self, status: StatusCode) -> Self {
        self.http_metadata_mut().status = Some(status);
        self
    }

    pub(crate) fn set_request_id(mut self, request_id: String) -> Self {
        self.http_metadata_mut().request_id = Some(request_id);
        self
    }

    pub(crate) fn set_retry_after(mut self, retry_after: Duration) -> Self {
        self.http_metadata_mut().retry_after = Some(retry_after);
        self
    }

    pub(crate) fn set_server_retryable(mut self, retryable: Option<bool>) -> Self {
        self.http_metadata_mut().retryable = retryable;
        self
    }

    pub(crate) fn server_retryable(&self) -> Option<bool> {
        self.http_metadata
            .as_ref()
            .and_then(|metadata| metadata.retryable)
    }

    fn http_metadata_mut(&mut self) -> &mut HttpErrorMetadata {
        self.http_metadata
            .get_or_insert_with(|| Box::new(HttpErrorMetadata::default()))
    }

    /// Set permanent status for error.
    pub fn set_permanent(mut self) -> Self {
        self.status = ErrorStatus::Permanent;
        self
    }

    /// Set temporary status for error.
    ///
    /// By set temporary, we indicate this error is retryable.
    pub fn set_temporary(mut self) -> Self {
        self.status = ErrorStatus::Temporary;
        self
    }

    /// Set persistent status for error.
    ///
    /// By setting persistent, we indicate the retry should be stopped.
    pub fn set_persistent(mut self) -> Self {
        self.status = ErrorStatus::Persistent;
        self
    }

    /// Return the kind of this error.
    pub fn kind(&self) -> ErrorKind {
        self.kind
    }

    /// Returns the server error code, including codes introduced by newer servers.
    pub fn code(&self) -> Option<&str> {
        self.api_error
            .as_deref()
            .and_then(|e| e.code.as_deref())
            .or_else(|| self.statement_details().map(|e| e.code.as_str()))
    }

    /// Returns the original structured server details, when provided.
    pub fn details(&self) -> Option<&serde_json::Value> {
        self.api_error
            .as_deref()
            .and_then(|e| e.details.as_ref())
            .or_else(|| self.statement_details().and_then(|e| e.details.as_ref()))
    }

    pub(crate) fn set_api_error(
        mut self,
        code: Option<String>,
        details: Option<serde_json::Value>,
    ) -> Self {
        if code.is_some() || details.is_some() {
            self.api_error = Some(Box::new(ApiErrorDetails { code, details }));
        }
        self
    }

    /// Return the error message.
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Return the HTTP response status when the error came from a server.
    pub fn http_status(&self) -> Option<StatusCode> {
        self.http_metadata
            .as_ref()
            .and_then(|metadata| metadata.status)
    }

    /// Return the request identifier supplied by ScopeDB or an intermediary.
    pub fn request_id(&self) -> Option<&str> {
        self.http_metadata
            .as_ref()
            .and_then(|metadata| metadata.request_id.as_deref())
    }

    /// Return the delay suggested by the HTTP `Retry-After` header.
    pub fn retry_after(&self) -> Option<Duration> {
        self.http_metadata
            .as_ref()
            .and_then(|metadata| metadata.retry_after)
    }

    /// Return whether retrying this operation is currently appropriate.
    /// For statement handles, retry on the same handle. Errors from `query()` or
    /// `execute()` after submission never indicate that resubmitting is safe.
    pub fn is_retryable(&self) -> bool {
        self.is_temporary()
    }

    /// Check if this error is temporary.
    pub fn is_temporary(&self) -> bool {
        self.status == ErrorStatus::Temporary
    }

    /// Check if this error is permanent.
    pub fn is_permanent(&self) -> bool {
        self.status == ErrorStatus::Permanent
    }

    /// Check if this error is persistent.
    pub fn is_persistent(&self) -> bool {
        self.status == ErrorStatus::Persistent
    }
}