prax-query 0.6.5

Type-safe query builder for the Prax ORM
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
//! Comprehensive error types for query operations with actionable messages.
//!
//! This module provides detailed error types that include:
//! - Error codes for programmatic handling
//! - Actionable suggestions for fixing issues
//! - Context about what operation failed
//! - Help text and documentation links
//!
//! # Error Codes
//!
//! Error codes follow a pattern: P{category}{number}
//! - 1xxx: Query errors (not found, invalid filter, etc.)
//! - 2xxx: Constraint violations (unique, foreign key, etc.)
//! - 3xxx: Connection errors (timeout, pool, auth)
//! - 4xxx: Transaction errors (deadlock, serialization)
//! - 5xxx: Execution errors (timeout, syntax, params)
//! - 6xxx: Data errors (type, serialization)
//! - 7xxx: Configuration errors
//! - 8xxx: Migration errors
//! - 9xxx: Tenant errors
//!
//! ```rust
//! use prax_query::ErrorCode;
//!
//! // Error codes have string representations
//! let code = ErrorCode::RecordNotFound;
//! let code = ErrorCode::UniqueConstraint;
//! let code = ErrorCode::ConnectionFailed;
//! ```
//!
//! # Creating Errors
//!
//! ```rust
//! use prax_query::{QueryError, ErrorCode};
//!
//! // Not found error
//! let err = QueryError::not_found("User");
//! assert_eq!(err.code, ErrorCode::RecordNotFound);
//!
//! // Generic error with code
//! let err = QueryError::new(ErrorCode::UniqueConstraint, "Email already exists");
//! assert_eq!(err.code, ErrorCode::UniqueConstraint);
//! ```
//!
//! # Error Properties
//!
//! ```rust
//! use prax_query::{QueryError, ErrorCode};
//!
//! let err = QueryError::not_found("User");
//!
//! // Access error code (public field)
//! assert_eq!(err.code, ErrorCode::RecordNotFound);
//!
//! // Access error message
//! let message = err.to_string();
//! assert!(message.contains("User"));
//! ```

use std::fmt;
use thiserror::Error;

/// Result type for query operations.
pub type QueryResult<T> = Result<T, QueryError>;

/// Error codes for programmatic error handling.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorCode {
    // Query errors (1xxx)
    /// Record not found (P1001).
    RecordNotFound = 1001,
    /// Multiple records found when expecting one (P1002).
    NotUnique = 1002,
    /// Invalid filter or where clause (P1003).
    InvalidFilter = 1003,
    /// Invalid select or include (P1004).
    InvalidSelect = 1004,
    /// Required field missing (P1005).
    RequiredFieldMissing = 1005,

    // Constraint errors (2xxx)
    /// Unique constraint violation (P2001).
    UniqueConstraint = 2001,
    /// Foreign key constraint violation (P2002).
    ForeignKeyConstraint = 2002,
    /// Check constraint violation (P2003).
    CheckConstraint = 2003,
    /// Not null constraint violation (P2004).
    NotNullConstraint = 2004,

    // Connection errors (3xxx)
    /// Database connection failed (P3001).
    ConnectionFailed = 3001,
    /// Connection pool exhausted (P3002).
    PoolExhausted = 3002,
    /// Connection timeout (P3003).
    ConnectionTimeout = 3003,
    /// Authentication failed (P3004).
    AuthenticationFailed = 3004,
    /// SSL/TLS error (P3005).
    SslError = 3005,

    // Transaction errors (4xxx)
    /// Transaction failed (P4001).
    TransactionFailed = 4001,
    /// Deadlock detected (P4002).
    Deadlock = 4002,
    /// Serialization failure (P4003).
    SerializationFailure = 4003,
    /// Transaction already committed/rolled back (P4004).
    TransactionClosed = 4004,

    // Query execution errors (5xxx)
    /// Query timeout (P5001).
    QueryTimeout = 5001,
    /// SQL syntax error (P5002).
    SqlSyntax = 5002,
    /// Invalid parameter (P5003).
    InvalidParameter = 5003,
    /// Query too complex (P5004).
    QueryTooComplex = 5004,
    /// General database error (P5005).
    DatabaseError = 5005,

    // Data errors (6xxx)
    /// Invalid data type (P6001).
    InvalidDataType = 6001,
    /// Serialization error (P6002).
    SerializationError = 6002,
    /// Deserialization error (P6003).
    DeserializationError = 6003,
    /// Data truncation (P6004).
    DataTruncation = 6004,

    // Configuration errors (7xxx)
    /// Invalid configuration (P7001).
    InvalidConfiguration = 7001,
    /// Missing configuration (P7002).
    MissingConfiguration = 7002,
    /// Invalid connection string (P7003).
    InvalidConnectionString = 7003,

    // Internal errors (9xxx)
    /// Internal error (P9001).
    Internal = 9001,
    /// Unknown error (P9999).
    Unknown = 9999,
}

impl ErrorCode {
    /// Get the error code string (e.g., "P1001").
    pub fn code(&self) -> String {
        format!("P{}", *self as u16)
    }

    /// Get a short description of the error code.
    pub fn description(&self) -> &'static str {
        match self {
            Self::RecordNotFound => "Record not found",
            Self::NotUnique => "Multiple records found",
            Self::InvalidFilter => "Invalid filter condition",
            Self::InvalidSelect => "Invalid select or include",
            Self::RequiredFieldMissing => "Required field missing",
            Self::UniqueConstraint => "Unique constraint violation",
            Self::ForeignKeyConstraint => "Foreign key constraint violation",
            Self::CheckConstraint => "Check constraint violation",
            Self::NotNullConstraint => "Not null constraint violation",
            Self::ConnectionFailed => "Database connection failed",
            Self::PoolExhausted => "Connection pool exhausted",
            Self::ConnectionTimeout => "Connection timeout",
            Self::AuthenticationFailed => "Authentication failed",
            Self::SslError => "SSL/TLS error",
            Self::TransactionFailed => "Transaction failed",
            Self::Deadlock => "Deadlock detected",
            Self::SerializationFailure => "Serialization failure",
            Self::TransactionClosed => "Transaction already closed",
            Self::QueryTimeout => "Query timeout",
            Self::SqlSyntax => "SQL syntax error",
            Self::InvalidParameter => "Invalid parameter",
            Self::QueryTooComplex => "Query too complex",
            Self::DatabaseError => "Database error",
            Self::InvalidDataType => "Invalid data type",
            Self::SerializationError => "Serialization error",
            Self::DeserializationError => "Deserialization error",
            Self::DataTruncation => "Data truncation",
            Self::InvalidConfiguration => "Invalid configuration",
            Self::MissingConfiguration => "Missing configuration",
            Self::InvalidConnectionString => "Invalid connection string",
            Self::Internal => "Internal error",
            Self::Unknown => "Unknown error",
        }
    }

    /// Get the documentation URL for this error.
    pub fn docs_url(&self) -> String {
        format!("https://prax.rs/docs/errors/{}", self.code())
    }
}

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

/// Suggestion for fixing an error.
#[derive(Debug, Clone)]
pub struct Suggestion {
    /// The suggestion text.
    pub text: String,
    /// Optional code example.
    pub code: Option<String>,
}

impl Suggestion {
    /// Create a new suggestion.
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            code: None,
        }
    }

    /// Add a code example.
    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }
}

/// Additional context for an error.
#[derive(Debug, Clone, Default)]
pub struct ErrorContext {
    /// The operation that was being performed.
    pub operation: Option<String>,
    /// The model involved.
    pub model: Option<String>,
    /// The field involved.
    pub field: Option<String>,
    /// The SQL query (if available).
    pub sql: Option<String>,
    /// Suggestions for fixing the error.
    pub suggestions: Vec<Suggestion>,
    /// Help text.
    pub help: Option<String>,
    /// Related errors.
    pub related: Vec<String>,
}

impl ErrorContext {
    /// Create new empty context.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the operation.
    pub fn operation(mut self, op: impl Into<String>) -> Self {
        self.operation = Some(op.into());
        self
    }

    /// Set the model.
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Set the field.
    pub fn field(mut self, field: impl Into<String>) -> Self {
        self.field = Some(field.into());
        self
    }

    /// Set the SQL query.
    pub fn sql(mut self, sql: impl Into<String>) -> Self {
        self.sql = Some(sql.into());
        self
    }

    /// Add a suggestion.
    pub fn suggestion(mut self, suggestion: Suggestion) -> Self {
        self.suggestions.push(suggestion);
        self
    }

    /// Add a text suggestion.
    pub fn suggest(mut self, text: impl Into<String>) -> Self {
        self.suggestions.push(Suggestion::new(text));
        self
    }

    /// Set help text.
    pub fn help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }
}

/// Errors that can occur during query operations.
#[derive(Error, Debug)]
pub struct QueryError {
    /// The error code.
    pub code: ErrorCode,
    /// The error message.
    pub message: String,
    /// Additional context.
    pub context: ErrorContext,
    /// The source error (if any).
    #[source]
    pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl fmt::Display for QueryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] {}", self.code.code(), self.message)
    }
}

impl QueryError {
    /// Create a new error with the given code and message.
    pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            context: ErrorContext::default(),
            source: None,
        }
    }

    /// Add context about the operation.
    pub fn with_context(mut self, operation: impl Into<String>) -> Self {
        self.context.operation = Some(operation.into());
        self
    }

    /// Add a suggestion for fixing the error.
    pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
        self.context.suggestions.push(Suggestion::new(suggestion));
        self
    }

    /// Add a code suggestion.
    pub fn with_code_suggestion(
        mut self,
        text: impl Into<String>,
        code: impl Into<String>,
    ) -> Self {
        self.context
            .suggestions
            .push(Suggestion::new(text).with_code(code));
        self
    }

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

    /// Set the model.
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.context.model = Some(model.into());
        self
    }

    /// Set the field.
    pub fn with_field(mut self, field: impl Into<String>) -> Self {
        self.context.field = Some(field.into());
        self
    }

    /// Set the SQL query.
    pub fn with_sql(mut self, sql: impl Into<String>) -> Self {
        self.context.sql = Some(sql.into());
        self
    }

    /// Set the source error.
    pub fn with_source<E: std::error::Error + Send + Sync + 'static>(mut self, source: E) -> Self {
        self.source = Some(Box::new(source));
        self
    }

    // ============== Constructor Functions ==============

    /// Create a not found error.
    pub fn not_found(model: impl Into<String>) -> Self {
        let model = model.into();
        Self::new(
            ErrorCode::RecordNotFound,
            format!("No {} record found matching the query", model),
        )
        .with_model(&model)
        .with_suggestion(format!("Verify the {} exists before querying", model))
        .with_code_suggestion(
            "Use findFirst() instead to get None instead of an error",
            format!(
                "client.{}().find_first().r#where(...).exec().await",
                model.to_lowercase()
            ),
        )
    }

    /// Create a not unique error.
    pub fn not_unique(model: impl Into<String>) -> Self {
        let model = model.into();
        Self::new(
            ErrorCode::NotUnique,
            format!("Expected unique {} record but found multiple", model),
        )
        .with_model(&model)
        .with_suggestion("Add more specific filters to narrow down to a single record")
        .with_suggestion("Use find_many() if you expect multiple results")
    }

    /// Create a constraint violation error.
    pub fn constraint_violation(model: impl Into<String>, message: impl Into<String>) -> Self {
        let model = model.into();
        let message = message.into();
        Self::new(
            ErrorCode::UniqueConstraint,
            format!("Constraint violation on {}: {}", model, message),
        )
        .with_model(&model)
    }

    /// Create a unique constraint violation error.
    pub fn unique_violation(model: impl Into<String>, field: impl Into<String>) -> Self {
        let model = model.into();
        let field = field.into();
        Self::new(
            ErrorCode::UniqueConstraint,
            format!("Unique constraint violated on {}.{}", model, field),
        )
        .with_model(&model)
        .with_field(&field)
        .with_suggestion(format!("A record with this {} already exists", field))
        .with_code_suggestion(
            "Use upsert() to update if exists, create if not",
            format!(
                "client.{}().upsert()\n  .r#where({}::{}::equals(value))\n  .create(...)\n  .update(...)\n  .exec().await",
                model.to_lowercase(), model.to_lowercase(), field
            ),
        )
    }

    /// Create a foreign key violation error.
    pub fn foreign_key_violation(model: impl Into<String>, relation: impl Into<String>) -> Self {
        let model = model.into();
        let relation = relation.into();
        Self::new(
            ErrorCode::ForeignKeyConstraint,
            format!("Foreign key constraint violated: {} -> {}", model, relation),
        )
        .with_model(&model)
        .with_field(&relation)
        .with_suggestion(format!(
            "Ensure the related {} record exists before creating this {}",
            relation, model
        ))
        .with_suggestion("Check for typos in the relation ID")
    }

    /// Create a not null violation error.
    pub fn not_null_violation(model: impl Into<String>, field: impl Into<String>) -> Self {
        let model = model.into();
        let field = field.into();
        Self::new(
            ErrorCode::NotNullConstraint,
            format!("Cannot set {}.{} to null - field is required", model, field),
        )
        .with_model(&model)
        .with_field(&field)
        .with_suggestion(format!("Provide a value for the {} field", field))
        .with_help("Make the field optional in your schema if null should be allowed")
    }

    /// Create an invalid input error.
    pub fn invalid_input(field: impl Into<String>, message: impl Into<String>) -> Self {
        let field = field.into();
        let message = message.into();
        Self::new(
            ErrorCode::InvalidParameter,
            format!("Invalid input for {}: {}", field, message),
        )
        .with_field(&field)
    }

    /// Create a connection error.
    pub fn connection(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(
            ErrorCode::ConnectionFailed,
            format!("Connection error: {}", message),
        )
        .with_suggestion("Check that the database server is running")
        .with_suggestion("Verify the connection URL is correct")
        .with_suggestion("Check firewall settings allow the connection")
    }

    /// Create a connection timeout error.
    pub fn connection_timeout(duration_ms: u64) -> Self {
        Self::new(
            ErrorCode::ConnectionTimeout,
            format!("Connection timed out after {}ms", duration_ms),
        )
        .with_suggestion("Increase the connect_timeout in your connection string")
        .with_suggestion("Check network connectivity to the database server")
        .with_code_suggestion(
            "Add connect_timeout to your connection URL",
            "postgres://user:pass@host/db?connect_timeout=30",
        )
    }

    /// Create a pool exhausted error.
    pub fn pool_exhausted(max_connections: u32) -> Self {
        Self::new(
            ErrorCode::PoolExhausted,
            format!("Connection pool exhausted (max {} connections)", max_connections),
        )
        .with_suggestion("Increase max_connections in pool configuration")
        .with_suggestion("Ensure connections are being released properly")
        .with_suggestion("Check for connection leaks in your application")
        .with_help("Consider using connection pooling middleware like PgBouncer for high-traffic applications")
    }

    /// Create an authentication error.
    pub fn authentication_failed(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(
            ErrorCode::AuthenticationFailed,
            format!("Authentication failed: {}", message),
        )
        .with_suggestion("Check username and password in connection string")
        .with_suggestion("Verify the user has permission to access the database")
        .with_suggestion("Check pg_hba.conf (PostgreSQL) or user privileges (MySQL)")
    }

    /// Create a timeout error.
    pub fn timeout(duration_ms: u64) -> Self {
        Self::new(
            ErrorCode::QueryTimeout,
            format!("Query timed out after {}ms", duration_ms),
        )
        .with_suggestion("Optimize the query to run faster")
        .with_suggestion("Add indexes to improve query performance")
        .with_suggestion("Increase the query timeout if the query is expected to be slow")
        .with_help("Consider paginating large result sets")
    }

    /// Create a transaction error.
    pub fn transaction(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(
            ErrorCode::TransactionFailed,
            format!("Transaction error: {}", message),
        )
    }

    /// Create a deadlock error.
    pub fn deadlock() -> Self {
        Self::new(
            ErrorCode::Deadlock,
            "Deadlock detected - transaction was rolled back".to_string(),
        )
        .with_suggestion("Retry the transaction")
        .with_suggestion("Access tables in a consistent order across transactions")
        .with_suggestion("Keep transactions short to reduce lock contention")
        .with_help("Deadlocks occur when two transactions wait for each other's locks")
    }

    /// Create an SQL syntax error.
    pub fn sql_syntax(message: impl Into<String>, sql: impl Into<String>) -> Self {
        let message = message.into();
        let sql = sql.into();
        Self::new(
            ErrorCode::SqlSyntax,
            format!("SQL syntax error: {}", message),
        )
        .with_sql(&sql)
        .with_suggestion("Check the generated SQL for errors")
        .with_help("This is likely a bug in Prax - please report it")
    }

    /// Create a serialization error.
    pub fn serialization(message: impl Into<String>) -> Self {
        Self::new(ErrorCode::SerializationError, message.into())
    }

    /// Create a deserialization error.
    pub fn deserialization(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(
            ErrorCode::DeserializationError,
            format!("Failed to deserialize result: {}", message),
        )
        .with_suggestion("Check that the model matches the database schema")
        .with_suggestion("Ensure data types are compatible")
    }

    /// Create a general database error.
    pub fn database(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(ErrorCode::DatabaseError, message)
            .with_suggestion("Check the database logs for more details")
    }

    /// Create an internal error.
    pub fn internal(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(ErrorCode::Internal, format!("Internal error: {}", message))
            .with_help("This is likely a bug in Prax ORM - please report it at https://github.com/pegasusheavy/prax-orm/issues")
    }

    /// Create an unsupported operation error.
    pub fn unsupported(message: impl Into<String>) -> Self {
        let message = message.into();
        Self::new(
            ErrorCode::InvalidConfiguration,
            format!("Unsupported: {}", message),
        )
        .with_help("This operation is not supported by the current database driver")
    }

    // ============== Error Checks ==============

    /// Check if this is a not found error.
    pub fn is_not_found(&self) -> bool {
        self.code == ErrorCode::RecordNotFound
    }

    /// Check if this is a constraint violation.
    pub fn is_constraint_violation(&self) -> bool {
        matches!(
            self.code,
            ErrorCode::UniqueConstraint
                | ErrorCode::ForeignKeyConstraint
                | ErrorCode::CheckConstraint
                | ErrorCode::NotNullConstraint
        )
    }

    /// Check if this is a timeout error.
    pub fn is_timeout(&self) -> bool {
        matches!(
            self.code,
            ErrorCode::QueryTimeout | ErrorCode::ConnectionTimeout
        )
    }

    /// Check if this is a connection error.
    pub fn is_connection_error(&self) -> bool {
        matches!(
            self.code,
            ErrorCode::ConnectionFailed
                | ErrorCode::PoolExhausted
                | ErrorCode::ConnectionTimeout
                | ErrorCode::AuthenticationFailed
                | ErrorCode::SslError
        )
    }

    /// Check if this error is retryable.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self.code,
            ErrorCode::ConnectionTimeout
                | ErrorCode::PoolExhausted
                | ErrorCode::QueryTimeout
                | ErrorCode::Deadlock
                | ErrorCode::SerializationFailure
        )
    }

    // ============== Display Functions ==============

    /// Get the error code.
    pub fn error_code(&self) -> &ErrorCode {
        &self.code
    }

    /// Get the documentation URL for this error.
    pub fn docs_url(&self) -> String {
        self.code.docs_url()
    }

    /// Display the full error with all context and suggestions.
    pub fn display_full(&self) -> String {
        let mut output = String::new();

        // Error header
        output.push_str(&format!("Error [{}]: {}\n", self.code.code(), self.message));

        // Context
        if let Some(ref op) = self.context.operation {
            output.push_str(&format!("  → While: {}\n", op));
        }
        if let Some(ref model) = self.context.model {
            output.push_str(&format!("  → Model: {}\n", model));
        }
        if let Some(ref field) = self.context.field {
            output.push_str(&format!("  → Field: {}\n", field));
        }

        // SQL (truncated if too long)
        if let Some(ref sql) = self.context.sql {
            let sql_display = if sql.len() > 200 {
                format!("{}...", &sql[..200])
            } else {
                sql.clone()
            };
            output.push_str(&format!("  → SQL: {}\n", sql_display));
        }

        // Suggestions
        if !self.context.suggestions.is_empty() {
            output.push_str("\nSuggestions:\n");
            for (i, suggestion) in self.context.suggestions.iter().enumerate() {
                output.push_str(&format!("  {}. {}\n", i + 1, suggestion.text));
                if let Some(ref code) = suggestion.code {
                    output.push_str(&format!(
                        "     ```\n     {}\n     ```\n",
                        code.replace('\n', "\n     ")
                    ));
                }
            }
        }

        // Help
        if let Some(ref help) = self.context.help {
            output.push_str(&format!("\nHelp: {}\n", help));
        }

        // Documentation link
        output.push_str(&format!("\nMore info: {}\n", self.docs_url()));

        output
    }

    /// Display error with ANSI colors for terminal output.
    pub fn display_colored(&self) -> String {
        let mut output = String::new();

        // Error header (red)
        output.push_str(&format!(
            "\x1b[1;31mError [{}]\x1b[0m: \x1b[1m{}\x1b[0m\n",
            self.code.code(),
            self.message
        ));

        // Context (dim)
        if let Some(ref op) = self.context.operation {
            output.push_str(&format!("  \x1b[2m→ While:\x1b[0m {}\n", op));
        }
        if let Some(ref model) = self.context.model {
            output.push_str(&format!("  \x1b[2m→ Model:\x1b[0m {}\n", model));
        }
        if let Some(ref field) = self.context.field {
            output.push_str(&format!("  \x1b[2m→ Field:\x1b[0m {}\n", field));
        }

        // Suggestions (yellow)
        if !self.context.suggestions.is_empty() {
            output.push_str("\n\x1b[1;33mSuggestions:\x1b[0m\n");
            for (i, suggestion) in self.context.suggestions.iter().enumerate() {
                output.push_str(&format!(
                    "  \x1b[33m{}.\x1b[0m {}\n",
                    i + 1,
                    suggestion.text
                ));
                if let Some(ref code) = suggestion.code {
                    output.push_str(&format!(
                        "     \x1b[2m```\x1b[0m\n     \x1b[36m{}\x1b[0m\n     \x1b[2m```\x1b[0m\n",
                        code.replace('\n', "\n     ")
                    ));
                }
            }
        }

        // Help (cyan)
        if let Some(ref help) = self.context.help {
            output.push_str(&format!("\n\x1b[1;36mHelp:\x1b[0m {}\n", help));
        }

        // Documentation link (blue)
        output.push_str(&format!(
            "\n\x1b[2mMore info:\x1b[0m \x1b[4;34m{}\x1b[0m\n",
            self.docs_url()
        ));

        output
    }
}

/// Extension trait for converting errors to QueryError.
pub trait IntoQueryError {
    /// Convert to a QueryError.
    fn into_query_error(self) -> QueryError;
}

impl<E: std::error::Error + Send + Sync + 'static> IntoQueryError for E {
    fn into_query_error(self) -> QueryError {
        QueryError::internal(self.to_string()).with_source(self)
    }
}

/// Helper for creating errors with context.
#[macro_export]
macro_rules! query_error {
    ($code:expr, $msg:expr) => {
        $crate::error::QueryError::new($code, $msg)
    };
    ($code:expr, $msg:expr, $($key:ident = $value:expr),+ $(,)?) => {{
        let mut err = $crate::error::QueryError::new($code, $msg);
        $(
            err = err.$key($value);
        )+
        err
    }};
}

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

    #[test]
    fn test_error_code_format() {
        assert_eq!(ErrorCode::RecordNotFound.code(), "P1001");
        assert_eq!(ErrorCode::UniqueConstraint.code(), "P2001");
        assert_eq!(ErrorCode::ConnectionFailed.code(), "P3001");
    }

    #[test]
    fn test_not_found_error() {
        let err = QueryError::not_found("User");
        assert!(err.is_not_found());
        assert!(err.message.contains("User"));
        assert!(!err.context.suggestions.is_empty());
    }

    #[test]
    fn test_unique_violation_error() {
        let err = QueryError::unique_violation("User", "email");
        assert!(err.is_constraint_violation());
        assert_eq!(err.context.model, Some("User".to_string()));
        assert_eq!(err.context.field, Some("email".to_string()));
    }

    #[test]
    fn test_timeout_error() {
        let err = QueryError::timeout(5000);
        assert!(err.is_timeout());
        assert!(err.message.contains("5000"));
    }

    #[test]
    fn test_error_with_context() {
        let err = QueryError::not_found("User")
            .with_context("Finding user by email")
            .with_suggestion("Use a different query method");

        assert_eq!(
            err.context.operation,
            Some("Finding user by email".to_string())
        );
        assert!(err.context.suggestions.len() >= 2); // Original + new one
    }

    #[test]
    fn test_retryable_errors() {
        assert!(QueryError::timeout(1000).is_retryable());
        assert!(QueryError::deadlock().is_retryable());
        assert!(QueryError::pool_exhausted(10).is_retryable());
        assert!(!QueryError::not_found("User").is_retryable());
    }

    #[test]
    fn test_connection_errors() {
        assert!(QueryError::connection("failed").is_connection_error());
        assert!(QueryError::authentication_failed("bad password").is_connection_error());
        assert!(QueryError::pool_exhausted(10).is_connection_error());
    }

    #[test]
    fn test_display_full() {
        let err = QueryError::unique_violation("User", "email").with_context("Creating new user");

        let output = err.display_full();
        assert!(output.contains("P2001"));
        assert!(output.contains("User"));
        assert!(output.contains("email"));
        assert!(output.contains("Suggestions"));
    }

    #[test]
    fn test_docs_url() {
        let err = QueryError::not_found("User");
        assert!(err.docs_url().contains("P1001"));
    }

    #[test]
    fn test_error_macro() {
        let err = query_error!(
            ErrorCode::InvalidParameter,
            "Invalid email format",
            with_field = "email",
            with_suggestion = "Use a valid email address"
        );

        assert_eq!(err.code, ErrorCode::InvalidParameter);
        assert_eq!(err.context.field, Some("email".to_string()));
    }

    #[test]
    fn test_suggestion_with_code() {
        let err = QueryError::not_found("User")
            .with_code_suggestion("Try this instead", "client.user().find_first()");

        let suggestion = err.context.suggestions.last().unwrap();
        assert!(suggestion.code.is_some());
    }
}