swarm repositories / source
aboutsummaryrefslogtreecommitdiff
path: root/crates/ra-mcp-engine/src/error.rs
blob: f40e1aeff645f5dac33b12936d55ba6ad4973b00 (plain)
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
use crate::lsp_transport::RpcErrorPayload;
use ra_mcp_domain::{fault::Fault, types::InvariantViolation};
use serde_json::Value;
use thiserror::Error;

/// Engine result type.
pub type EngineResult<T> = Result<T, EngineError>;

/// Structured rust-analyzer response error.
#[derive(Debug, Clone, Error)]
#[error("lsp response error: code={code}, message={message}")]
pub struct LspResponseError {
    /// LSP JSON-RPC error code.
    pub code: i64,
    /// LSP JSON-RPC error message.
    pub message: String,
    /// Optional JSON-RPC error data payload.
    pub data: Option<Value>,
}

/// Engine failure type.
#[derive(Debug, Error)]
pub enum EngineError {
    /// I/O failure while syncing source documents.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    /// Domain invariant was violated.
    #[error(transparent)]
    Invariant(#[from] InvariantViolation),
    /// Transport/process/protocol fault.
    #[error("engine fault: {0:?}")]
    Fault(Fault),
    /// rust-analyzer returned a JSON-RPC error object.
    #[error(transparent)]
    LspResponse(#[from] LspResponseError),
    /// Response payload could not be deserialized into expected type.
    #[error("invalid lsp payload for method {method}: {message}")]
    InvalidPayload {
        /// Request method.
        method: &'static str,
        /// Decoder error detail.
        message: String,
    },
    /// Request params could not be serialized into JSON.
    #[error("invalid lsp request payload for method {method}: {message}")]
    InvalidRequest {
        /// Request method.
        method: &'static str,
        /// Encoder error detail.
        message: String,
    },
    /// Path to URL conversion failed.
    #[error("path cannot be represented as file URL")]
    InvalidFileUrl,
}

impl From<Fault> for EngineError {
    fn from(value: Fault) -> Self {
        Self::Fault(value)
    }
}

impl From<RpcErrorPayload> for LspResponseError {
    fn from(value: RpcErrorPayload) -> Self {
        Self {
            code: value.code,
            message: value.message,
            data: value.data,
        }
    }
}

impl From<RpcErrorPayload> for EngineError {
    fn from(value: RpcErrorPayload) -> Self {
        Self::LspResponse(value.into())
    }
}