hyperdb-api-core 1.0.0-rc.4

Internal implementation details for hyperdb-api. Not a stable API; use hyperdb-api instead.
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
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! gRPC-specific error types.
//!
//! This module handles conversion from gRPC status codes and Hyper's structured
//! error details to the common [`crate::Error`] type.

use std::fmt;

use tonic::Status;

use crate::client::error::Error;

/// Which [`Error`] variant a gRPC status code or SQLSTATE maps to.
///
/// gRPC is the one place in the crate where the variant is chosen at
/// runtime from a wire code rather than being known at the call site, so
/// the decision needs a name it can be carried around under. It stays
/// private to this module — [`Error`] itself is flat and has no `kind`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Variant {
    Authentication,
    Cancelled,
    Connection,
    FeatureNotSupported,
    Other,
    Query,
    Timeout,
}

impl Variant {
    /// Builds the corresponding [`Error`], attaching the server's
    /// diagnostics to the variants that carry them.
    ///
    /// `Query`, `Connection`, and `Cancelled` have fields for the pieces
    /// they can use. The rest have only a message, so any `detail` is
    /// folded into it rather than dropped — that keeps the rendered text
    /// identical to what the previous struct-shaped error produced.
    fn build(
        self,
        message: String,
        detail: Option<String>,
        hint: Option<String>,
        sqlstate: Option<String>,
    ) -> Error {
        match self {
            Variant::Query => Error::Query {
                message,
                sqlstate,
                detail,
                hint,
            },
            Variant::Connection => Error::Connection {
                message: fold_detail(message, detail.as_deref()),
                sqlstate,
            },
            Variant::Cancelled => Error::Cancelled {
                message: fold_detail(message, detail.as_deref()),
                sqlstate,
            },
            Variant::Authentication => {
                Error::authentication(fold_detail(message, detail.as_deref()))
            }
            Variant::FeatureNotSupported => {
                Error::feature_not_supported(fold_detail(message, detail.as_deref()))
            }
            Variant::Timeout => Error::timeout(fold_detail(message, detail.as_deref())),
            Variant::Other => Error::other(fold_detail(message, detail.as_deref())),
        }
    }
}

/// Appends `": {detail}"` unless `message` already contains it, matching
/// what `Error`'s `Display` does for the variants that keep a `detail`
/// field.
fn fold_detail(message: String, detail: Option<&str>) -> String {
    match detail {
        Some(detail) if !message.contains(detail) => format!("{message}: {detail}"),
        _ => message,
    }
}

/// gRPC-specific error information.
///
/// This wraps additional error details from Hyper's gRPC error responses,
/// including SQLSTATE codes, hints, and detailed error messages.
#[derive(Debug, Clone)]
pub struct GrpcError {
    /// The SQLSTATE error code (e.g., "42703" for undefined column)
    pub sqlstate: Option<String>,
    /// The primary error message
    pub message: String,
    /// Additional detail about the error
    pub detail: Option<String>,
    /// A hint for how to resolve the error
    pub hint: Option<String>,
    /// The error source ("User" or "System")
    pub error_source: Option<String>,
}

impl fmt::Display for GrpcError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)?;
        if let Some(ref detail) = self.detail {
            write!(f, ": {detail}")?;
        }
        Ok(())
    }
}

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

#[expect(
    clippy::needless_pass_by_value,
    reason = "call-site ergonomics: function consumes logically-owned parameters, refactoring signatures is not worth per-site churn"
)]
/// Converts a tonic gRPC Status to our Error type.
///
/// This function attempts to parse Hyper's structured error details from the
/// gRPC status. If that fails, it falls back to parsing XML error format,
/// and finally to using the raw gRPC error message.
pub(super) fn from_grpc_status(status: Status) -> Error {
    // First, try to parse structured error details (ErrorInfo proto)
    if let Some(error_info) = parse_error_info(&status) {
        return grpc_code_to_variant(status.code()).build(
            error_info.message,
            error_info.detail,
            error_info.hint,
            error_info.sqlstate,
        );
    }

    // Fall back to parsing XML error format from the message
    if let Some(error) = parse_xml_error(status.message()) {
        return error;
    }

    // Last resort: use the raw gRPC error message
    grpc_code_to_variant(status.code()).build(status.message().to_string(), None, None, None)
}

/// Attempts to parse `ErrorInfo` from the gRPC status details.
fn parse_error_info(status: &Status) -> Option<GrpcError> {
    // The error details are in the status metadata as a serialized google.rpc.Status
    // containing salesforce.hyperdb.grpc.v1.ErrorInfo
    //
    // For now, we'll implement a simplified version that extracts from the status
    // details bytes. A full implementation would use prost to decode the Any types.

    // Try to decode the details
    let details = status.details();
    if details.is_empty() {
        return None;
    }

    // Try to parse as google.rpc.Status containing ErrorInfo
    // This is a simplified implementation - we look for known field patterns
    parse_error_info_from_bytes(details)
}

/// Parses `ErrorInfo` from raw bytes.
///
/// This is a simplified parser that looks for the `ErrorInfo` fields in the
/// serialized protobuf data.
fn parse_error_info_from_bytes(data: &[u8]) -> Option<GrpcError> {
    // Try to decode using prost
    use prost::Message;

    // The details are wrapped in google.rpc.Status
    // which contains a repeated Any field with ErrorInfo
    #[derive(Clone, PartialEq, Message)]
    struct GoogleRpcStatus {
        #[prost(int32, tag = "1")]
        code: i32,
        #[prost(string, tag = "2")]
        message: String,
        #[prost(message, repeated, tag = "3")]
        details: Vec<prost_types::Any>,
    }

    if let Ok(rpc_status) = GoogleRpcStatus::decode(data) {
        for detail in rpc_status.details {
            // Check if this is an ErrorInfo
            if detail
                .type_url
                .ends_with("salesforce.hyperdb.grpc.v1.ErrorInfo")
            {
                // Try to decode the ErrorInfo
                if let Some(error_info) = decode_error_info(&detail.value) {
                    return Some(error_info);
                }
            }
        }
    }

    None
}

/// Decodes `ErrorInfo` from its serialized form.
fn decode_error_info(data: &[u8]) -> Option<GrpcError> {
    use prost::Message;

    // ErrorInfo proto fields:
    // 1: primary_message (string)
    // 2: sqlstate (string)
    // 3: customer_hint (string)
    // 4: customer_detail (string)
    // 5: system_detail (string)
    // 6: position (message)
    // 7: error_source (string)
    #[derive(Clone, PartialEq, Message)]
    struct ErrorInfo {
        #[prost(string, tag = "1")]
        primary_message: String,
        #[prost(string, tag = "2")]
        sqlstate: String,
        #[prost(string, tag = "3")]
        customer_hint: String,
        #[prost(string, tag = "4")]
        customer_detail: String,
        #[prost(string, tag = "5")]
        system_detail: String,
        // Skipping position (tag 6) for now
        #[prost(string, tag = "7")]
        error_source: String,
    }

    if let Ok(info) = ErrorInfo::decode(data) {
        // Build error message combining primary_message and customer_detail
        let message = if info.customer_detail.is_empty() {
            info.primary_message.clone()
        } else {
            format!("{}: {}", info.primary_message, info.customer_detail)
        };

        return Some(GrpcError {
            sqlstate: if info.sqlstate.is_empty() {
                None
            } else {
                Some(info.sqlstate)
            },
            message,
            detail: if info.customer_detail.is_empty() {
                None
            } else {
                Some(info.customer_detail)
            },
            hint: if info.customer_hint.is_empty() {
                None
            } else {
                Some(info.customer_hint)
            },
            error_source: if info.error_source.is_empty() {
                None
            } else {
                Some(info.error_source)
            },
        });
    }

    None
}

/// Parses XML-format error message (legacy Hyper error format).
///
/// Example: `<sqlstate>42703</sqlstate><primary>column not found</primary><detail>...</detail>`
fn parse_xml_error(message: &str) -> Option<Error> {
    // Quick check if this looks like XML
    if !message.contains("<sqlstate>") && !message.contains("<primary>") {
        return None;
    }

    let sqlstate = extract_xml_tag(message, "sqlstate");
    let primary = extract_xml_tag(message, "primary");
    let detail = extract_xml_tag(message, "detail");
    let hint = extract_xml_tag(message, "hint");

    // Build the error message
    let error_message = match (&primary, &detail) {
        (Some(p), Some(d)) => format!("{p}: {d}"),
        (Some(p), None) => p.clone(),
        (None, Some(d)) => d.clone(),
        (None, None) => message.to_string(),
    };

    // Determine the variant from SQLSTATE
    let variant = sqlstate
        .as_ref()
        .map_or(Variant::Query, |s| sqlstate_to_variant(s));

    Some(variant.build(error_message, detail, hint, sqlstate))
}

/// Extracts content from an XML tag like `<tag>content</tag>`.
fn extract_xml_tag(text: &str, tag: &str) -> Option<String> {
    let start_tag = format!("<{tag}>");
    let end_tag = format!("</{tag}>");

    let start = text.find(&start_tag)? + start_tag.len();
    let end = text[start..].find(&end_tag)? + start;

    Some(text[start..end].to_string())
}

/// Converts a gRPC status code to the [`Error`] variant it maps to.
fn grpc_code_to_variant(code: tonic::Code) -> Variant {
    match code {
        tonic::Code::Ok => Variant::Other, // Shouldn't happen for errors
        tonic::Code::Cancelled => Variant::Cancelled,
        tonic::Code::Unknown => Variant::Query,
        tonic::Code::InvalidArgument => Variant::Query,
        tonic::Code::DeadlineExceeded => Variant::Timeout,
        tonic::Code::NotFound => Variant::Query,
        tonic::Code::AlreadyExists => Variant::Query,
        tonic::Code::PermissionDenied => Variant::Authentication,
        tonic::Code::ResourceExhausted => Variant::Query,
        tonic::Code::FailedPrecondition => Variant::Query,
        tonic::Code::Aborted => Variant::Query,
        tonic::Code::OutOfRange => Variant::Query,
        tonic::Code::Unimplemented => Variant::FeatureNotSupported,
        tonic::Code::Internal => Variant::Query,
        tonic::Code::Unavailable => Variant::Connection,
        tonic::Code::DataLoss => Variant::Query,
        tonic::Code::Unauthenticated => Variant::Authentication,
    }
}

/// Converts a SQLSTATE code to the [`Error`] variant it maps to.
fn sqlstate_to_variant(sqlstate: &str) -> Variant {
    match sqlstate {
        // Query canceled
        "57014" => Variant::Cancelled,
        // Authentication errors (28xxx)
        s if s.starts_with("28") => Variant::Authentication,
        // Connection errors (08xxx)
        s if s.starts_with("08") => Variant::Connection,
        // Feature not supported (0A000)
        "0A000" => Variant::FeatureNotSupported,
        // Everything else is a query error
        _ => Variant::Query,
    }
}

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

    #[test]
    fn test_parse_xml_error() {
        let msg = "<sqlstate>42703</sqlstate><primary>column not found</primary><detail>column \"foo\" does not exist</detail>";
        let error = parse_xml_error(msg).unwrap();
        assert!(error.to_string().contains("column not found"));
    }

    #[test]
    fn test_extract_xml_tag() {
        assert_eq!(
            extract_xml_tag("<foo>bar</foo>", "foo"),
            Some("bar".to_string())
        );
        assert_eq!(
            extract_xml_tag("<a>1</a><b>2</b>", "b"),
            Some("2".to_string())
        );
        assert_eq!(extract_xml_tag("<a>1</a>", "c"), None);
    }

    #[test]
    fn test_grpc_code_mapping() {
        assert_eq!(
            grpc_code_to_variant(tonic::Code::Cancelled),
            Variant::Cancelled
        );
        assert_eq!(
            grpc_code_to_variant(tonic::Code::Unauthenticated),
            Variant::Authentication
        );
        assert_eq!(
            grpc_code_to_variant(tonic::Code::Unavailable),
            Variant::Connection
        );
    }

    /// A SQLSTATE-selected variant must keep the code on the variants that
    /// have a field for it — the public `hyperdb_api::Error` mapping reads
    /// `sqlstate()` for `Cancelled` and `Connection`, not just `Query`.
    #[test]
    fn test_sqlstate_survives_variant_selection() {
        let err = sqlstate_to_variant("57014").build(
            "canceled".to_string(),
            None,
            None,
            Some("57014".to_string()),
        );
        assert!(matches!(err, Error::Cancelled { .. }));
        assert_eq!(err.sqlstate(), Some("57014"));

        let err = sqlstate_to_variant("08006").build(
            "connection failure".to_string(),
            None,
            None,
            Some("08006".to_string()),
        );
        assert!(matches!(err, Error::Connection { .. }));
        assert_eq!(err.sqlstate(), Some("08006"));
    }

    /// The variants with no `detail` field must fold it into the message
    /// rather than dropping it, so the rendered text is unchanged.
    #[test]
    fn test_detail_folded_into_message_when_no_field() {
        let err = Variant::Timeout.build(
            "deadline exceeded".to_string(),
            Some("waited 30s".to_string()),
            None,
            None,
        );
        assert_eq!(err.to_string(), "deadline exceeded: waited 30s");

        // Already contained → not repeated.
        let err = Variant::Timeout.build(
            "deadline exceeded: waited 30s".to_string(),
            Some("waited 30s".to_string()),
            None,
            None,
        );
        assert_eq!(err.to_string(), "deadline exceeded: waited 30s");
    }
}