vibe-ready 0.2.0

Composable runtime, logging, scheduling, and storage foundations for vibe-coding Rust projects.
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use crate::log::log_def::CODE_STR;
use crate::log_e;
use crate::store::db::sql_def::DbError;
use log::error;
use std::backtrace::Backtrace;
use std::fmt;

#[repr(i32)]
#[derive(Clone, Copy, Debug, PartialEq)]
/// Stable numeric error codes returned by vibe-ready.
pub enum VibeEngineErrorCode {
    /// Unknown or unmapped error.
    Unknown = -1,

    /// Operation completed successfully.
    Success = 200,

    /// Engine has already been dropped.
    EngineDropped = 100001,

    /// Database has not been opened yet.
    DatabaseNotOpened = 100002,

    /// Database open operation failed.
    DatabaseOpenFailed = 100003,
    /// Log database open operation failed.
    LogDatabaseOpenFailed = 100223,

    /// Work database open operation failed.
    WorkDatabaseOpenFailed = 132003,

    /// Database I/O operation failed.
    DatabaseIOError = 100004,

    /// Requested database target was not found.
    DatabaseTargetNotFound = 100005,

    /// Database worker thread failed.
    DatabaseThreadError = 100006,

    /// Task could not be posted to the runtime.
    PostError = 100007,
    /// Task was cancelled before it could complete (B9 scheduler).
    Cancelled = 100008,

    /// Required parameter is empty.
    ParameterEmpty = 100012,
    /// OAuth flow failed.
    OAuthError = 100013,
    /// Engine configuration is invalid.
    ConfigError = 100014,

    /// Standard I/O error.
    IOError = 100017,
    /// HTTP or protocol bad-request error.
    BadRequest = 100018,
    /// Request operation failed.
    RequestError = 100019,
    /// Remote service returned an internal server error.
    InternalServerError = 100020,
    /// Network operation failed.
    NetworkError = 100021,
    /// Requested operation is unsupported.
    UnsupportedError = 100022,

    /// Operation timed out.
    TimeoutError = 100023,
    /// Connection attempt failed.
    ConnectError = 100024,
    /// TLS connection attempt failed.
    TlsConnectError = 100025,
    /// Options parsing failed.
    OptionsParseError = 100026,

    /// Deserialization failed.
    SerdeDeserializeError = 100027,
    /// Serialization failed.
    SerdeSerializeError = 100028,

    /// Log information argument is invalid.
    InvalidArgumentLogInfo = 100029,

    /// MPSC channel send failed.
    MPSCSendError = 100037,

    /// QR generation failed.
    GenerateQRError = 10005,
    /// Runtime operation failed.
    RuntimeError = 10006,
    /// Internal beaver subsystem failed.
    BeaverError = 10007,
    /// Socket receive operation timed out.
    SocketRecvTimeout = 10008,
    /// Socket has been closed.
    SocketClosed = 10009,
    /// Protocol parsing failed.
    ProtocParseError = 10010,
    /// Socket has not been opened.
    SocketNotOpened = 10011,
    /// Task was interrupted.
    TaskInterruptionError = 10012,

    /// Connection is closed.
    ConnectionClosed = 30001,

    /// Connection already exists.
    ConnectionExists = 34001,

    /// Connection is currently closing.
    ConnectionClosing = 30027,

    /// Internal operation failed.
    InternalError = 32002,
    /// User is not logged in.
    NotLoggedInError = 32003,
    /// Page token is invalid.
    PageTokenError = 32004,
    /// Clipboard initialization failed.
    ClipboardInitializeError = 32005,

    /// Feature or backend support has not been implemented yet.
    NotSupportedYet = 999999,
}

impl VibeEngineErrorCode {
    /// Returns the stable numeric code associated with this variant.
    ///
    /// # Returns
    ///
    /// The `i32` code used in serialized errors and logs.
    ///
    /// # Examples
    ///
    /// ```
    /// use vibe_ready::VibeErrorCode;
    ///
    /// assert_eq!(VibeErrorCode::Success.code(), 200);
    /// ```
    pub fn code(&self) -> i32 {
        *self as i32
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
/// High-level category for a [`VibeEngineError`].
pub enum VibeErrorKind {
    /// Configuration or argument error.
    Config,
    /// Runtime or executor error.
    Runtime,
    /// Task scheduling or cancellation error.
    Task,
    /// Database or storage error.
    Database,
    /// Network or connection error.
    Network,
    /// Logging backend error.
    Log,
    /// Unsupported operation error.
    Unsupported,
    /// Internal SDK error.
    Internal,
    /// Unknown or uncategorized error.
    Unknown,
}

impl VibeErrorKind {
    fn from_code(code: VibeEngineErrorCode) -> Self {
        match code {
            VibeEngineErrorCode::ConfigError
            | VibeEngineErrorCode::ParameterEmpty
            | VibeEngineErrorCode::InvalidArgumentLogInfo => Self::Config,
            VibeEngineErrorCode::RuntimeError
            | VibeEngineErrorCode::EngineDropped
            | VibeEngineErrorCode::MPSCSendError => Self::Runtime,
            VibeEngineErrorCode::PostError
            | VibeEngineErrorCode::TaskInterruptionError
            | VibeEngineErrorCode::Cancelled => Self::Task,
            VibeEngineErrorCode::DatabaseNotOpened
            | VibeEngineErrorCode::DatabaseOpenFailed
            | VibeEngineErrorCode::LogDatabaseOpenFailed
            | VibeEngineErrorCode::WorkDatabaseOpenFailed
            | VibeEngineErrorCode::DatabaseIOError
            | VibeEngineErrorCode::DatabaseTargetNotFound
            | VibeEngineErrorCode::DatabaseThreadError => Self::Database,
            VibeEngineErrorCode::NetworkError
            | VibeEngineErrorCode::BadRequest
            | VibeEngineErrorCode::RequestError
            | VibeEngineErrorCode::InternalServerError
            | VibeEngineErrorCode::TimeoutError
            | VibeEngineErrorCode::ConnectError
            | VibeEngineErrorCode::TlsConnectError
            | VibeEngineErrorCode::SocketRecvTimeout
            | VibeEngineErrorCode::SocketClosed
            | VibeEngineErrorCode::SocketNotOpened
            | VibeEngineErrorCode::ConnectionClosed
            | VibeEngineErrorCode::ConnectionExists
            | VibeEngineErrorCode::ConnectionClosing => Self::Network,
            VibeEngineErrorCode::UnsupportedError | VibeEngineErrorCode::NotSupportedYet => {
                Self::Unsupported
            }
            VibeEngineErrorCode::InternalError
            | VibeEngineErrorCode::IOError
            | VibeEngineErrorCode::SerdeDeserializeError
            | VibeEngineErrorCode::SerdeSerializeError
            | VibeEngineErrorCode::OptionsParseError
            | VibeEngineErrorCode::GenerateQRError
            | VibeEngineErrorCode::BeaverError
            | VibeEngineErrorCode::ProtocParseError
            | VibeEngineErrorCode::OAuthError
            | VibeEngineErrorCode::NotLoggedInError
            | VibeEngineErrorCode::PageTokenError
            | VibeEngineErrorCode::ClipboardInitializeError => Self::Internal,
            VibeEngineErrorCode::Success | VibeEngineErrorCode::Unknown => Self::Unknown,
        }
    }
}

#[derive(Debug, Clone)]
/// Error returned by vibe-ready operations.
pub struct VibeEngineError {
    code: i32,
    kind: VibeErrorKind,
    message: String,
    source: Option<String>,
    context: Vec<String>,
}

impl fmt::Display for VibeEngineError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "vibe-ready error [{:?}/{}]: {}",
            self.kind, self.code, self.message
        )?;
        if let Some(source) = &self.source {
            write!(f, "; source: {source}")?;
        }
        if !self.context.is_empty() {
            write!(f, "; context: {}", self.context.join("; "))?;
        }
        Ok(())
    }
}

impl std::error::Error for VibeEngineError {}

impl From<DbError> for VibeEngineError {
    fn from(err: DbError) -> Self {
        error!("DbError: {:?}, {:?}", err, Backtrace::capture());
        let db_error = err.clone();
        let mut error = match err {
            DbError::OpenFailed => {
                VibeEngineError::from_code(VibeEngineErrorCode::DatabaseOpenFailed)
            }
            DbError::DatabaseIOError => {
                VibeEngineError::from_code(VibeEngineErrorCode::DatabaseIOError)
            }
            DbError::NotOpen => VibeEngineError::from_code(VibeEngineErrorCode::DatabaseOpenFailed),
            DbError::TargetNotFound => {
                VibeEngineError::from_code(VibeEngineErrorCode::DatabaseTargetNotFound)
            }
            DbError::DatabaseThreadError => {
                VibeEngineError::from_code(VibeEngineErrorCode::DatabaseThreadError)
            }
            DbError::DatabaseUnlockError => {
                VibeEngineError::from_code(VibeEngineErrorCode::DatabaseOpenFailed)
            }
            DbError::NotSupportedYet => {
                VibeEngineError::from_code(VibeEngineErrorCode::DatabaseIOError)
            }
            DbError::JoinError => VibeEngineError::from_code(VibeEngineErrorCode::DatabaseIOError),
            _ => VibeEngineError::from_code(VibeEngineErrorCode::Unknown),
        };
        error.source = Some(format!("{db_error:?}"));
        error
    }
}

impl VibeEngineError {
    /// Returns the numeric error code.
    ///
    /// # Returns
    ///
    /// The stable `i32` code associated with this error.
    pub fn code(&self) -> i32 {
        self.code
    }

    /// Returns the high-level error category.
    ///
    /// # Returns
    ///
    /// A [`VibeErrorKind`] derived from the numeric code.
    pub fn kind(&self) -> VibeErrorKind {
        self.kind
    }

    /// Returns the human-readable error message.
    ///
    /// # Returns
    ///
    /// A borrowed message string.
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Returns the optional source message.
    ///
    /// # Returns
    ///
    /// `Some(&str)` when a lower-level source was attached.
    pub fn source_message(&self) -> Option<&str> {
        self.source.as_deref()
    }

    /// Returns contextual breadcrumbs attached to the error.
    ///
    /// # Returns
    ///
    /// A borrowed slice of context strings.
    pub fn context(&self) -> &[String] {
        &self.context
    }

    /// Attaches a source message to this error.
    ///
    /// # Returns
    ///
    /// The updated error value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vibe_ready::{VibeEngineError, VibeErrorCode};
    ///
    /// let error = VibeEngineError::from_error_code(VibeErrorCode::RuntimeError)
    ///     .with_source("worker stopped");
    /// assert_eq!(error.source_message(), Some("worker stopped"));
    /// ```
    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }

    /// Adds contextual information to this error.
    ///
    /// # Returns
    ///
    /// The updated error value.
    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.context.push(context.into());
        self
    }

    /// Creates the canonical success value.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] whose code is [`VibeEngineErrorCode::Success`].
    pub fn from_success() -> Self {
        Self::from_error_code(VibeEngineErrorCode::Success)
    }

    /// Creates an error from an unsigned 16-bit raw code.
    ///
    /// # Returns
    ///
    /// A raw-code error with [`VibeErrorKind::Unknown`].
    pub fn from_u16(code: u16) -> Self {
        VibeEngineError::from_raw_code(code as i32)
    }

    /// Creates an error from an unsigned 32-bit raw code.
    ///
    /// # Returns
    ///
    /// A raw-code error with [`VibeErrorKind::Unknown`].
    pub fn from_u32(code: u32) -> Self {
        VibeEngineError::from_raw_code(code as i32)
    }

    /// Checks whether this error represents success.
    ///
    /// # Returns
    ///
    /// `true` when the code is [`VibeEngineErrorCode::Success`].
    pub fn is_success(&self) -> bool {
        self.code == VibeEngineErrorCode::Success.code()
    }

    /// Clones this error while preserving the same code and context.
    ///
    /// # Returns
    ///
    /// A clone of this error.
    pub fn same_code(&self) -> Self {
        self.clone()
    }

    /// Creates an error from a stable code variant.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with default kind and message for the code.
    pub fn from_error_code(value: VibeEngineErrorCode) -> Self {
        VibeEngineError {
            code: value.code(),
            kind: VibeErrorKind::from_code(value),
            message: default_message(value).to_string(),
            source: None,
            context: Vec::new(),
        }
    }

    /// Creates an error from a stable code variant.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with default kind and message for the code.
    pub fn from_code(value: VibeEngineErrorCode) -> Self {
        Self::from_error_code(value)
    }

    /// Creates an error from a code variant and custom message.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] using `msg` as the display message.
    pub fn from_error_code_msg(value: VibeEngineErrorCode, msg: String) -> Self {
        VibeEngineError {
            message: msg,
            ..Self::from_error_code(value)
        }
    }

    /// Creates the standard task-interruption error.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with [`VibeEngineErrorCode::TaskInterruptionError`].
    pub fn from_task_interruption_error() -> Self {
        Self::from_error_code(VibeEngineErrorCode::TaskInterruptionError)
    }

    /// Creates the standard internal error.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with [`VibeEngineErrorCode::InternalError`].
    pub fn from_internal_error() -> Self {
        Self::from_error_code(VibeEngineErrorCode::InternalError)
    }

    /// Creates the standard MPSC send error.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with [`VibeEngineErrorCode::MPSCSendError`].
    pub fn from_mpsc_send_error() -> Self {
        Self::from_error_code(VibeEngineErrorCode::MPSCSendError)
    }

    /// Creates the standard empty-parameter error.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with [`VibeEngineErrorCode::ParameterEmpty`].
    pub fn from_parameter_empty() -> Self {
        Self::from_error_code(VibeEngineErrorCode::ParameterEmpty)
    }
    /// Logs and creates the standard empty-parameter error.
    ///
    /// # Returns
    ///
    /// A [`VibeEngineError`] with [`VibeEngineErrorCode::ParameterEmpty`].
    pub fn from_parameter_empty_log(tag: &str) -> Self {
        let code = VibeEngineErrorCode::ParameterEmpty.code();
        log_e!(tag, CODE_STR, code);
        Self::from_error_code(VibeEngineErrorCode::ParameterEmpty)
    }

    fn from_raw_code(code: i32) -> Self {
        VibeEngineError {
            code,
            kind: VibeErrorKind::Unknown,
            message: format!("unknown error code {code}"),
            source: None,
            context: Vec::new(),
        }
    }
}

fn default_message(code: VibeEngineErrorCode) -> &'static str {
    match code {
        VibeEngineErrorCode::Unknown => "unknown error",
        VibeEngineErrorCode::Success => "success",
        VibeEngineErrorCode::EngineDropped => "engine has been dropped",
        VibeEngineErrorCode::DatabaseNotOpened => "database is not opened",
        VibeEngineErrorCode::DatabaseOpenFailed => "database open failed",
        VibeEngineErrorCode::LogDatabaseOpenFailed => "log database open failed",
        VibeEngineErrorCode::WorkDatabaseOpenFailed => "work database open failed",
        VibeEngineErrorCode::DatabaseIOError => "database I/O error",
        VibeEngineErrorCode::DatabaseTargetNotFound => "database target not found",
        VibeEngineErrorCode::DatabaseThreadError => "database worker thread error",
        VibeEngineErrorCode::PostError => "task post failed",
        VibeEngineErrorCode::Cancelled => "task was cancelled",
        VibeEngineErrorCode::ParameterEmpty => "required parameter is empty",
        VibeEngineErrorCode::OAuthError => "OAuth error",
        VibeEngineErrorCode::ConfigError => "configuration error",
        VibeEngineErrorCode::IOError => "I/O error",
        VibeEngineErrorCode::BadRequest => "bad request",
        VibeEngineErrorCode::RequestError => "request error",
        VibeEngineErrorCode::InternalServerError => "internal server error",
        VibeEngineErrorCode::NetworkError => "network error",
        VibeEngineErrorCode::UnsupportedError => "unsupported operation",
        VibeEngineErrorCode::TimeoutError => "operation timed out",
        VibeEngineErrorCode::ConnectError => "connection failed",
        VibeEngineErrorCode::TlsConnectError => "TLS connection failed",
        VibeEngineErrorCode::OptionsParseError => "options parse failed",
        VibeEngineErrorCode::SerdeDeserializeError => "deserialization failed",
        VibeEngineErrorCode::SerdeSerializeError => "serialization failed",
        VibeEngineErrorCode::InvalidArgumentLogInfo => "invalid log info argument",
        VibeEngineErrorCode::MPSCSendError => "channel send failed",
        VibeEngineErrorCode::GenerateQRError => "QR generation failed",
        VibeEngineErrorCode::RuntimeError => "runtime error",
        VibeEngineErrorCode::BeaverError => "internal beaver error",
        VibeEngineErrorCode::SocketRecvTimeout => "socket receive timed out",
        VibeEngineErrorCode::SocketClosed => "socket closed",
        VibeEngineErrorCode::ProtocParseError => "protocol parse failed",
        VibeEngineErrorCode::SocketNotOpened => "socket is not opened",
        VibeEngineErrorCode::TaskInterruptionError => "task interrupted",
        VibeEngineErrorCode::ConnectionClosed => "connection closed",
        VibeEngineErrorCode::ConnectionExists => "connection already exists",
        VibeEngineErrorCode::ConnectionClosing => "connection is closing",
        VibeEngineErrorCode::InternalError => "internal error",
        VibeEngineErrorCode::NotLoggedInError => "not logged in",
        VibeEngineErrorCode::PageTokenError => "page token error",
        VibeEngineErrorCode::ClipboardInitializeError => "clipboard initialization failed",
        VibeEngineErrorCode::NotSupportedYet => "not supported yet",
    }
}

impl serde::Serialize for VibeEngineError {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut s = serializer.serialize_struct("VibeEngineError", 5)?;
        s.serialize_field("code", &self.code())?;
        s.serialize_field("kind", &self.kind)?;
        s.serialize_field("message", &self.message)?;
        s.serialize_field("source", &self.source)?;
        s.serialize_field("context", &self.context)?;
        s.end()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn assert_std_error<T: std::error::Error>() {}

    #[test]
    fn error_model_exposes_kind_message_source_and_context() {
        assert_std_error::<VibeEngineError>();

        let error = VibeEngineError::from_error_code_msg(
            VibeEngineErrorCode::TimeoutError,
            "destroy timed out".to_string(),
        )
        .with_source("shutdown queue did not finish")
        .with_context("engine.destroy_with_timeout");

        assert_eq!(error.code(), VibeEngineErrorCode::TimeoutError.code());
        assert_eq!(error.kind(), VibeErrorKind::Network);
        assert_eq!(error.message(), "destroy timed out");
        assert_eq!(
            error.source_message(),
            Some("shutdown queue did not finish")
        );
        assert_eq!(
            error.context(),
            &["engine.destroy_with_timeout".to_string()]
        );
        assert!(error.to_string().contains("destroy timed out"));
    }
}

#[cfg(test)]
mod strict_tests {
    use super::*;
    include!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/test/unit/api/engine_error_tests.rs"
    ));
}