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
|
//! The error-to-status table.
//!
//! SCOUT section implemented: SCOUT-NATIVE §6. The mapping itself lives in
//! GN4's route (`src/routes/search.rs::engine_error`), so it is mirrored
//! here rather than exported by the crate: this file is a guard, not an
//! API. Both functions below are exhaustive matches, which is the point —
//! adding a variant to [`gn_search_engine::SearchError`] stops compiling until
//! somebody decides what status it means and what code it carries.
use gn_search_engine::SearchError;
/// The status GN4 answers with for this error (SCOUT-NATIVE §6, the table
/// in `src/search/mod.rs:62-68`).
fn gn4_status(error: &SearchError) -> u16 {
match error {
SearchError::InvalidQuery { .. } => 422,
SearchError::Unreachable(_) => 502,
SearchError::Misbehaving(_) => 502,
SearchError::Backend(_) => 502,
SearchError::Internal(_) => 500,
}
}
/// The machine-readable code GN4 puts in the body (SCOUT-NATIVE §6).
fn gn4_code(error: &SearchError) -> &'static str {
match error {
SearchError::InvalidQuery { .. } => "invalid_query",
SearchError::Unreachable(_) => "backend_unreachable",
SearchError::Misbehaving(_) => "backend_misbehaving",
SearchError::Backend(_) => "backend_error",
SearchError::Internal(_) => "internal_error",
}
}
/// The whole table: 422 / 502 / 502 / 502 / 500, one row per variant.
#[test]
fn every_variant_maps_to_the_status_gn4_expects() {
let cases = [
(
SearchError::InvalidQuery {
message: "Syntax: <expression> AND <expression>".to_string(),
xapian_error_type: Some("QueryParserError".to_string()),
},
422,
"invalid_query",
),
(
SearchError::Unreachable("xapian index at /x is not a directory".to_string()),
502,
"backend_unreachable",
),
(
SearchError::Misbehaving("xapian doc data is not a search hit: x".to_string()),
502,
"backend_misbehaving",
),
(
SearchError::Backend("gn3 search returned HTTP 500".to_string()),
502,
"backend_error",
),
(
SearchError::Internal("native search task failed to join: x".to_string()),
500,
"internal_error",
),
];
for (error, status, code) in cases {
assert_eq!(gn4_status(&error), status, "status for {error:?}");
assert_eq!(gn4_code(&error), code, "code for {error:?}");
}
}
/// The diagnostic text is reachable without matching the variant, because
/// GN4 puts exactly this string in the response body's `detail`
/// (SCOUT-NATIVE §6).
#[test]
fn message_is_the_response_detail() {
let error = SearchError::InvalidQuery {
message: "InvalidOperationError: Query is invalid (invalid Hs/Mm location field)"
.to_string(),
xapian_error_type: Some("InvalidOperationError".to_string()),
};
assert_eq!(
error.message(),
"InvalidOperationError: Query is invalid (invalid Hs/Mm location field)"
);
assert_eq!(error.xapian_error_type(), Some("InvalidOperationError"));
assert_eq!(
error.to_string(),
"InvalidOperationError: Query is invalid (invalid Hs/Mm location field) \
(InvalidOperationError)"
);
let unreachable = SearchError::Unreachable("index is missing".to_string());
assert_eq!(unreachable.message(), "index is missing");
assert_eq!(unreachable.xapian_error_type(), None);
assert_eq!(unreachable.to_string(), "index is missing");
}
/// A variant without an exception class still displays without one.
#[test]
fn invalid_query_without_a_class_displays_the_message_alone() {
let error = SearchError::InvalidQuery {
message: "Unknown range operation".to_string(),
xapian_error_type: None,
};
assert_eq!(error.to_string(), "Unknown range operation");
assert_eq!(gn4_status(&error), 422);
}
/// Every variant is a `std::error::Error`, so GN4 can keep using it where
/// its own error trait is expected.
#[test]
fn variants_are_std_errors() {
fn assert_error<E: std::error::Error>(_: &E) {}
assert_error(&SearchError::Internal("x".to_string()));
assert_error(&SearchError::Backend("x".to_string()));
}
|