reasonkit-web 0.1.7

High-performance MCP server for browser automation, web capture, and content extraction. Rust-powered CDP client for AI agents.
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! Error types for ReasonKit Web
//!
//! This module provides a comprehensive error type hierarchy using `thiserror`
//! for proper error handling across all components.
//!
//! # Error Categories
//!
//! - [`WebError`] - HTTP-level errors with status codes and JSON responses
//! - [`enum@Error`] - Internal operation errors (browser, MCP, extraction)
//! - Domain-specific errors: [`BrowserError`], [`McpError`], [`ExtractionError`], etc.
//!
//! # Example
//!
//! ```rust,no_run
//! use reasonkit_web::error::{WebError, Result};
//!
//! fn process_request(data: &str) -> Result<String> {
//!     if data.is_empty() {
//!         return Err(WebError::invalid_request("Data cannot be empty").into());
//!     }
//!     Ok(data.to_uppercase())
//! }
//! ```

use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fmt;
use thiserror::Error;
use tracing::{error, warn};

// ============================================================================
// WebError - HTTP-level errors with JSON responses
// ============================================================================

/// HTTP-level error with status codes and structured JSON responses.
///
/// This error type is designed for web API responses, providing:
/// - Appropriate HTTP status codes
/// - Structured JSON error bodies
/// - Request ID correlation for tracing
/// - Logging integration
///
/// # JSON Response Format
///
/// ```json
/// {
///     "error": "Human-readable error message",
///     "code": "ERROR_CODE",
///     "request_id": "optional-request-id"
/// }
/// ```
#[derive(Error, Debug, Clone)]
pub enum WebError {
    /// Invalid request - malformed input, missing fields, validation failures
    /// HTTP Status: 400 Bad Request
    #[error("Invalid request: {message}")]
    InvalidRequest {
        /// Detailed error message
        message: String,
    },

    /// Unauthorized - authentication required or invalid credentials
    /// HTTP Status: 401 Unauthorized
    #[error("Unauthorized: {reason}")]
    Unauthorized {
        /// Reason for authorization failure
        reason: String,
    },

    /// Forbidden - authenticated but not permitted
    /// HTTP Status: 403 Forbidden
    #[error("Forbidden: {reason}")]
    Forbidden {
        /// Reason access was denied
        reason: String,
    },

    /// Resource not found
    /// HTTP Status: 404 Not Found
    #[error("Not found: {resource}")]
    NotFound {
        /// Description of the missing resource
        resource: String,
    },

    /// Request content too large
    /// HTTP Status: 413 Payload Too Large
    #[error("Content too large: {size} bytes exceeds maximum of {max} bytes")]
    ContentTooLarge {
        /// Actual size of the content
        size: usize,
        /// Maximum allowed size
        max: usize,
    },

    /// Rate limit exceeded
    /// HTTP Status: 429 Too Many Requests
    #[error("Rate limited: retry after {retry_after_secs} seconds")]
    RateLimited {
        /// Number of seconds to wait before retrying
        retry_after_secs: u64,
    },

    /// Processing error - operation failed during execution
    /// HTTP Status: 500 Internal Server Error
    #[error("Processing error: {0}")]
    ProcessingError(String),

    /// Internal server error - unexpected failures
    /// HTTP Status: 500 Internal Server Error
    #[error("Internal error: {message}")]
    InternalError {
        /// Error message (sanitized for external display)
        message: String,
    },

    /// Service unavailable - temporarily unable to handle request
    /// HTTP Status: 503 Service Unavailable
    #[error("Service unavailable: {reason}")]
    ServiceUnavailable {
        /// Reason the service is unavailable
        reason: String,
    },

    /// Gateway timeout - upstream service timed out
    /// HTTP Status: 504 Gateway Timeout
    #[error("Gateway timeout after {timeout_ms}ms")]
    GatewayTimeout {
        /// Timeout duration in milliseconds
        timeout_ms: u64,
    },
}

impl WebError {
    // ========================================================================
    // Factory Methods
    // ========================================================================

    /// Create an invalid request error with a message
    pub fn invalid_request(message: impl Into<String>) -> Self {
        Self::InvalidRequest {
            message: message.into(),
        }
    }

    /// Create an invalid request error for a missing field
    pub fn missing_field(field: &str) -> Self {
        Self::InvalidRequest {
            message: format!("Missing required field: {}", field),
        }
    }

    /// Create an invalid request error for an invalid field value
    pub fn invalid_field(field: &str, reason: &str) -> Self {
        Self::InvalidRequest {
            message: format!("Invalid value for field '{}': {}", field, reason),
        }
    }

    /// Create an unauthorized error
    pub fn unauthorized(reason: impl Into<String>) -> Self {
        Self::Unauthorized {
            reason: reason.into(),
        }
    }

    /// Create a forbidden error
    pub fn forbidden(reason: impl Into<String>) -> Self {
        Self::Forbidden {
            reason: reason.into(),
        }
    }

    /// Create a not found error
    pub fn not_found(resource: impl Into<String>) -> Self {
        Self::NotFound {
            resource: resource.into(),
        }
    }

    /// Create a content too large error
    pub fn content_too_large(size: usize, max: usize) -> Self {
        Self::ContentTooLarge { size, max }
    }

    /// Create a rate limited error
    pub fn rate_limited(retry_after_secs: u64) -> Self {
        Self::RateLimited { retry_after_secs }
    }

    /// Create a processing error from any error type
    pub fn processing<E: std::fmt::Display>(source: E) -> Self {
        Self::ProcessingError(source.to_string())
    }

    /// Create an internal error with a message
    pub fn internal(message: impl Into<String>) -> Self {
        Self::InternalError {
            message: message.into(),
        }
    }

    /// Create an internal error from any error, sanitizing the message
    pub fn internal_from<E: std::error::Error>(err: E) -> Self {
        // Log the full error for debugging
        error!(error = %err, "Internal error occurred");
        Self::InternalError {
            message: "An unexpected error occurred".to_string(),
        }
    }

    /// Create a service unavailable error
    pub fn service_unavailable(reason: impl Into<String>) -> Self {
        Self::ServiceUnavailable {
            reason: reason.into(),
        }
    }

    /// Create a gateway timeout error
    pub fn gateway_timeout(timeout_ms: u64) -> Self {
        Self::GatewayTimeout { timeout_ms }
    }

    // ========================================================================
    // HTTP Status Code Mapping
    // ========================================================================

    /// Get the HTTP status code for this error
    pub fn status_code(&self) -> u16 {
        match self {
            Self::InvalidRequest { .. } => 400,
            Self::Unauthorized { .. } => 401,
            Self::Forbidden { .. } => 403,
            Self::NotFound { .. } => 404,
            Self::ContentTooLarge { .. } => 413,
            Self::RateLimited { .. } => 429,
            Self::ProcessingError(_) => 500,
            Self::InternalError { .. } => 500,
            Self::ServiceUnavailable { .. } => 503,
            Self::GatewayTimeout { .. } => 504,
        }
    }

    /// Get the error code string for this error
    pub fn error_code(&self) -> &'static str {
        match self {
            Self::InvalidRequest { .. } => "INVALID_REQUEST",
            Self::Unauthorized { .. } => "UNAUTHORIZED",
            Self::Forbidden { .. } => "FORBIDDEN",
            Self::NotFound { .. } => "NOT_FOUND",
            Self::ContentTooLarge { .. } => "CONTENT_TOO_LARGE",
            Self::RateLimited { .. } => "RATE_LIMITED",
            Self::ProcessingError(_) => "PROCESSING_ERROR",
            Self::InternalError { .. } => "INTERNAL_ERROR",
            Self::ServiceUnavailable { .. } => "SERVICE_UNAVAILABLE",
            Self::GatewayTimeout { .. } => "GATEWAY_TIMEOUT",
        }
    }

    /// Convert error to JSON response body
    pub fn to_json(&self) -> serde_json::Value {
        json!({
            "error": self.to_string(),
            "code": self.error_code()
        })
    }

    /// Convert error to JSON response body with request ID
    pub fn to_json_with_request_id(&self, request_id: &str) -> serde_json::Value {
        json!({
            "error": self.to_string(),
            "code": self.error_code(),
            "request_id": request_id
        })
    }

    /// Log the error with appropriate level and optional request ID
    pub fn log(&self, request_id: Option<&str>) {
        let request_id = request_id.unwrap_or("unknown");

        match self {
            Self::InvalidRequest { message } => {
                warn!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    message = %message,
                    "Invalid request"
                );
            }
            Self::Unauthorized { reason } => {
                warn!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    reason = %reason,
                    "Unauthorized access attempt"
                );
            }
            Self::Forbidden { reason } => {
                warn!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    reason = %reason,
                    "Forbidden access"
                );
            }
            Self::NotFound { resource } => {
                warn!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    resource = %resource,
                    "Resource not found"
                );
            }
            Self::ContentTooLarge { size, max } => {
                warn!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    size = %size,
                    max = %max,
                    "Content too large"
                );
            }
            Self::RateLimited { retry_after_secs } => {
                warn!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    retry_after_secs = %retry_after_secs,
                    "Rate limited"
                );
            }
            Self::ProcessingError(err) => {
                error!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    error = %err,
                    "Processing error"
                );
            }
            Self::InternalError { message } => {
                error!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    message = %message,
                    "Internal error"
                );
            }
            Self::ServiceUnavailable { reason } => {
                error!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    reason = %reason,
                    "Service unavailable"
                );
            }
            Self::GatewayTimeout { timeout_ms } => {
                error!(
                    request_id = %request_id,
                    error_code = %self.error_code(),
                    timeout_ms = %timeout_ms,
                    "Gateway timeout"
                );
            }
        }
    }

    /// Check if this error should be retried
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            Self::RateLimited { .. }
                | Self::ServiceUnavailable { .. }
                | Self::GatewayTimeout { .. }
        )
    }

    /// Get retry-after header value if applicable
    pub fn retry_after(&self) -> Option<u64> {
        match self {
            Self::RateLimited { retry_after_secs } => Some(*retry_after_secs),
            _ => None,
        }
    }
}

/// Structured JSON error response for serialization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorResponse {
    /// Human-readable error message
    pub error: String,
    /// Machine-readable error code
    pub code: String,
    /// Optional request ID for correlation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    /// Optional additional details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
}

impl ErrorResponse {
    /// Create a new error response
    pub fn new(error: impl Into<String>, code: impl Into<String>) -> Self {
        Self {
            error: error.into(),
            code: code.into(),
            request_id: None,
            details: None,
        }
    }

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

    /// Set additional details
    pub fn with_details(mut self, details: serde_json::Value) -> Self {
        self.details = Some(details);
        self
    }
}

impl From<&WebError> for ErrorResponse {
    fn from(err: &WebError) -> Self {
        Self {
            error: err.to_string(),
            code: err.error_code().to_string(),
            request_id: None,
            details: None,
        }
    }
}

// ============================================================================
// Internal Error - Comprehensive domain errors
// ============================================================================

/// The main error type for ReasonKit Web internal operations
#[derive(Error, Debug)]
pub enum Error {
    /// HTTP/Web-level errors
    #[error("{0}")]
    Web(#[from] WebError),

    /// Browser-related errors
    #[error("Browser error: {0}")]
    Browser(#[from] BrowserError),

    /// MCP protocol errors
    #[error("MCP error: {0}")]
    Mcp(#[from] McpError),

    /// Content extraction errors
    #[error("Extraction error: {0}")]
    Extraction(#[from] ExtractionError),

    /// Navigation errors
    #[error("Navigation error: {0}")]
    Navigation(#[from] NavigationError),

    /// Capture errors (screenshot, PDF, etc.)
    #[error("Capture error: {0}")]
    Capture(#[from] CaptureError),

    /// I/O errors
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// JSON serialization/deserialization errors
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// ChromiumOxide errors
    #[error("CDP error: {0}")]
    Cdp(String),

    /// Generic error with message
    #[error("{0}")]
    Generic(String),
}

/// Browser lifecycle and control errors
#[derive(Error, Debug)]
pub enum BrowserError {
    /// Failed to launch browser
    #[error("Failed to launch browser: {0}")]
    LaunchFailed(String),

    /// Browser configuration error
    #[error("Invalid browser configuration: {0}")]
    ConfigError(String),

    /// Browser connection lost
    #[error("Browser connection lost")]
    ConnectionLost,

    /// Failed to create new page/tab
    #[error("Failed to create page: {0}")]
    PageCreationFailed(String),

    /// Browser already closed
    #[error("Browser already closed")]
    AlreadyClosed,

    /// Timeout waiting for browser
    #[error("Browser operation timed out after {0}ms")]
    Timeout(u64),
}

/// MCP protocol errors
#[derive(Error, Debug)]
pub enum McpError {
    /// Invalid JSON-RPC request
    #[error("Invalid JSON-RPC request: {0}")]
    InvalidRequest(String),

    /// Unknown method
    #[error("Unknown method: {0}")]
    UnknownMethod(String),

    /// Invalid parameters
    #[error("Invalid parameters: {0}")]
    InvalidParams(String),

    /// Tool not found
    #[error("Tool not found: {0}")]
    ToolNotFound(String),

    /// Tool execution failed
    #[error("Tool execution failed: {0}")]
    ToolExecutionFailed(String),

    /// Protocol version mismatch
    #[error("Protocol version mismatch: expected {expected}, got {actual}")]
    VersionMismatch {
        /// Expected version
        expected: String,
        /// Actual version received
        actual: String,
    },

    /// Parse error
    #[error("Parse error: {0}")]
    ParseError(String),
}

/// Content extraction errors
#[derive(Error, Debug)]
pub enum ExtractionError {
    /// Element not found
    #[error("Element not found: {0}")]
    ElementNotFound(String),

    /// Invalid selector
    #[error("Invalid selector: {0}")]
    InvalidSelector(String),

    /// Extraction failed
    #[error("Extraction failed: {0}")]
    ExtractionFailed(String),

    /// Content parsing failed
    #[error("Content parsing failed: {0}")]
    ParsingFailed(String),

    /// JavaScript execution failed
    #[error("JavaScript execution failed: {0}")]
    JsExecutionFailed(String),
}

/// Navigation errors
#[derive(Error, Debug)]
pub enum NavigationError {
    /// Invalid URL
    #[error("Invalid URL: {0}")]
    InvalidUrl(String),

    /// Navigation timeout
    #[error("Navigation timed out after {0}ms")]
    Timeout(u64),

    /// Page load failed
    #[error("Page load failed: {0}")]
    LoadFailed(String),

    /// SSL/TLS error
    #[error("SSL/TLS error: {0}")]
    SslError(String),

    /// Network error
    #[error("Network error: {0}")]
    NetworkError(String),

    /// HTTP error
    #[error("HTTP error {status}: {message}")]
    HttpError {
        /// HTTP status code
        status: u16,
        /// Error message
        message: String,
    },
}

/// Capture errors (screenshots, PDFs, etc.)
#[derive(Error, Debug)]
pub enum CaptureError {
    /// Screenshot failed
    #[error("Screenshot capture failed: {0}")]
    ScreenshotFailed(String),

    /// PDF generation failed
    #[error("PDF generation failed: {0}")]
    PdfFailed(String),

    /// MHTML capture failed
    #[error("MHTML capture failed: {0}")]
    MhtmlFailed(String),

    /// HTML capture failed
    #[error("HTML capture failed: {0}")]
    HtmlFailed(String),

    /// Invalid capture format
    #[error("Invalid capture format: {0}")]
    InvalidFormat(String),

    /// Capture timeout
    #[error("Capture timed out after {0}ms")]
    Timeout(u64),
}

/// Result type alias for ReasonKit Web operations
pub type Result<T> = std::result::Result<T, Error>;

/// Result type alias for WebError operations
pub type WebResult<T> = std::result::Result<T, WebError>;

impl Error {
    /// Create a generic error from a string
    pub fn generic<S: Into<String>>(msg: S) -> Self {
        Error::Generic(msg.into())
    }

    /// Create a CDP error from a string
    pub fn cdp<S: Into<String>>(msg: S) -> Self {
        Error::Cdp(msg.into())
    }

    /// Convert internal error to WebError for HTTP responses
    pub fn into_web_error(self) -> WebError {
        match self {
            Error::Web(e) => e,
            Error::Browser(e) => match e {
                BrowserError::Timeout(ms) => WebError::GatewayTimeout { timeout_ms: ms },
                BrowserError::ConnectionLost => {
                    WebError::service_unavailable("Browser connection lost")
                }
                BrowserError::AlreadyClosed => {
                    WebError::service_unavailable("Browser session closed")
                }
                _ => WebError::internal(e.to_string()),
            },
            Error::Mcp(e) => match e {
                McpError::InvalidRequest(msg) => WebError::invalid_request(msg),
                McpError::InvalidParams(msg) => WebError::invalid_request(msg),
                McpError::ToolNotFound(tool) => {
                    WebError::not_found(format!("Tool not found: {}", tool))
                }
                _ => WebError::internal(e.to_string()),
            },
            Error::Navigation(e) => match e {
                NavigationError::InvalidUrl(url) => {
                    WebError::invalid_request(format!("Invalid URL: {}", url))
                }
                NavigationError::Timeout(ms) => WebError::GatewayTimeout { timeout_ms: ms },
                NavigationError::HttpError { status, message } => {
                    if status == 404 {
                        WebError::not_found(message)
                    } else if status == 401 {
                        WebError::unauthorized(message)
                    } else if status == 403 {
                        WebError::forbidden(message)
                    } else if status == 429 {
                        WebError::rate_limited(60) // Default retry
                    } else {
                        WebError::internal(format!("HTTP {}: {}", status, message))
                    }
                }
                _ => WebError::internal(e.to_string()),
            },
            Error::Extraction(e) => match e {
                ExtractionError::ElementNotFound(selector) => {
                    WebError::not_found(format!("Element not found: {}", selector))
                }
                ExtractionError::InvalidSelector(sel) => {
                    WebError::invalid_request(format!("Invalid selector: {}", sel))
                }
                _ => WebError::internal(e.to_string()),
            },
            Error::Capture(e) => match e {
                CaptureError::Timeout(ms) => WebError::GatewayTimeout { timeout_ms: ms },
                CaptureError::InvalidFormat(fmt) => {
                    WebError::invalid_request(format!("Invalid format: {}", fmt))
                }
                _ => WebError::internal(e.to_string()),
            },
            Error::Io(e) => WebError::internal(format!("I/O error: {}", e)),
            Error::Json(e) => WebError::invalid_request(format!("JSON error: {}", e)),
            Error::Cdp(msg) => WebError::internal(format!("CDP error: {}", msg)),
            Error::Generic(msg) => WebError::internal(msg),
        }
    }
}

/// Convert chromiumoxide errors
impl From<chromiumoxide::error::CdpError> for Error {
    fn from(err: chromiumoxide::error::CdpError) -> Self {
        Error::Cdp(err.to_string())
    }
}

// ============================================================================
// Conversion implementations for WebError
// ============================================================================

impl From<std::io::Error> for WebError {
    fn from(err: std::io::Error) -> Self {
        WebError::internal(format!("I/O error: {}", err))
    }
}

impl From<serde_json::Error> for WebError {
    fn from(err: serde_json::Error) -> Self {
        WebError::invalid_request(format!("JSON error: {}", err))
    }
}

impl From<anyhow::Error> for WebError {
    fn from(err: anyhow::Error) -> Self {
        WebError::ProcessingError(err.to_string())
    }
}

// ============================================================================
// Request ID generation
// ============================================================================

/// Generate a unique request ID for tracing
pub fn generate_request_id() -> String {
    use rand::Rng;
    let mut rng = rand::rng();
    let id: u64 = rng.random();
    format!("req_{:016x}", id)
}

/// Request context for error handling
#[derive(Debug, Clone)]
pub struct RequestContext {
    /// Unique request ID
    pub request_id: String,
    /// Request start time
    pub start_time: std::time::Instant,
}

impl RequestContext {
    /// Create a new request context with a generated ID
    pub fn new() -> Self {
        Self {
            request_id: generate_request_id(),
            start_time: std::time::Instant::now(),
        }
    }

    /// Create a new request context with a specific ID
    pub fn with_id(request_id: impl Into<String>) -> Self {
        Self {
            request_id: request_id.into(),
            start_time: std::time::Instant::now(),
        }
    }

    /// Get elapsed time in milliseconds
    pub fn elapsed_ms(&self) -> u64 {
        self.start_time.elapsed().as_millis() as u64
    }

    /// Log an error with this request context
    pub fn log_error(&self, error: &WebError) {
        error.log(Some(&self.request_id));
    }
}

impl Default for RequestContext {
    fn default() -> Self {
        Self::new()
    }
}

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

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_web_error_status_codes() {
        assert_eq!(WebError::invalid_request("test").status_code(), 400);
        assert_eq!(WebError::unauthorized("test").status_code(), 401);
        assert_eq!(WebError::forbidden("test").status_code(), 403);
        assert_eq!(WebError::not_found("test").status_code(), 404);
        assert_eq!(WebError::content_too_large(100, 50).status_code(), 413);
        assert_eq!(WebError::rate_limited(60).status_code(), 429);
        assert_eq!(WebError::internal("test").status_code(), 500);
        assert_eq!(WebError::service_unavailable("test").status_code(), 503);
        assert_eq!(WebError::gateway_timeout(5000).status_code(), 504);
    }

    #[test]
    fn test_web_error_codes() {
        assert_eq!(
            WebError::invalid_request("test").error_code(),
            "INVALID_REQUEST"
        );
        assert_eq!(WebError::unauthorized("test").error_code(), "UNAUTHORIZED");
        assert_eq!(WebError::forbidden("test").error_code(), "FORBIDDEN");
        assert_eq!(WebError::not_found("test").error_code(), "NOT_FOUND");
        assert_eq!(
            WebError::content_too_large(100, 50).error_code(),
            "CONTENT_TOO_LARGE"
        );
        assert_eq!(WebError::rate_limited(60).error_code(), "RATE_LIMITED");
        assert_eq!(WebError::internal("test").error_code(), "INTERNAL_ERROR");
    }

    #[test]
    fn test_web_error_json() {
        let err = WebError::invalid_request("Missing name field");
        let json = err.to_json();

        assert_eq!(json["code"], "INVALID_REQUEST");
        assert!(json["error"]
            .as_str()
            .unwrap()
            .contains("Missing name field"));
    }

    #[test]
    fn test_web_error_json_with_request_id() {
        let err = WebError::rate_limited(120);
        let json = err.to_json_with_request_id("req_abc123");

        assert_eq!(json["code"], "RATE_LIMITED");
        assert_eq!(json["request_id"], "req_abc123");
    }

    #[test]
    fn test_web_error_factory_methods() {
        let err = WebError::missing_field("email");
        assert!(err.to_string().contains("Missing required field: email"));

        let err = WebError::invalid_field("age", "must be positive");
        assert!(err.to_string().contains("Invalid value for field 'age'"));
    }

    #[test]
    fn test_web_error_retryable() {
        assert!(!WebError::invalid_request("test").is_retryable());
        assert!(!WebError::unauthorized("test").is_retryable());
        assert!(WebError::rate_limited(60).is_retryable());
        assert!(WebError::service_unavailable("test").is_retryable());
        assert!(WebError::gateway_timeout(5000).is_retryable());
    }

    #[test]
    fn test_web_error_retry_after() {
        assert_eq!(WebError::rate_limited(120).retry_after(), Some(120));
        assert_eq!(WebError::invalid_request("test").retry_after(), None);
    }

    #[test]
    fn test_error_display() {
        let err = Error::Browser(BrowserError::LaunchFailed("no chrome".to_string()));
        assert!(err.to_string().contains("Failed to launch browser"));
        assert!(err.to_string().contains("no chrome"));
    }

    #[test]
    fn test_mcp_error() {
        let err = McpError::ToolNotFound("unknown_tool".to_string());
        assert_eq!(err.to_string(), "Tool not found: unknown_tool");
    }

    #[test]
    fn test_extraction_error() {
        let err = ExtractionError::ElementNotFound("#missing".to_string());
        assert!(err.to_string().contains("Element not found"));
    }

    #[test]
    fn test_navigation_error() {
        let err = NavigationError::HttpError {
            status: 404,
            message: "Not Found".to_string(),
        };
        assert!(err.to_string().contains("404"));
        assert!(err.to_string().contains("Not Found"));
    }

    #[test]
    fn test_generic_error() {
        let err = Error::generic("something went wrong");
        assert_eq!(err.to_string(), "something went wrong");
    }

    #[test]
    fn test_error_into_web_error() {
        let err = Error::Navigation(NavigationError::InvalidUrl("bad-url".to_string()));
        let web_err = err.into_web_error();
        assert_eq!(web_err.status_code(), 400);
        assert!(web_err.to_string().contains("Invalid URL"));

        let err = Error::Navigation(NavigationError::Timeout(5000));
        let web_err = err.into_web_error();
        assert_eq!(web_err.status_code(), 504);
    }

    #[test]
    fn test_error_response_serialization() {
        let response = ErrorResponse::new("Test error", "TEST_ERROR")
            .with_request_id("req_123")
            .with_details(json!({"field": "name"}));

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("TEST_ERROR"));
        assert!(json.contains("req_123"));
        assert!(json.contains("name"));
    }

    #[test]
    fn test_request_context() {
        let ctx = RequestContext::new();
        assert!(ctx.request_id.starts_with("req_"));
        assert_eq!(ctx.request_id.len(), 20); // "req_" + 16 hex chars

        let ctx = RequestContext::with_id("custom-id-123");
        assert_eq!(ctx.request_id, "custom-id-123");
    }

    #[test]
    fn test_generate_request_id() {
        let id1 = generate_request_id();
        let id2 = generate_request_id();
        assert_ne!(id1, id2);
        assert!(id1.starts_with("req_"));
    }

    #[test]
    fn test_web_error_from_io_error() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let web_err: WebError = io_err.into();
        assert_eq!(web_err.status_code(), 500);
    }

    #[test]
    fn test_content_too_large_display() {
        let err = WebError::content_too_large(1024 * 1024 * 10, 1024 * 1024);
        assert!(err.to_string().contains("10485760"));
        assert!(err.to_string().contains("1048576"));
    }
}