dropshot 0.17.1

expose REST APIs from a Rust program
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
// Copyright 2024 Oxide Computer Company

//! Test cases for user-defined error types.

use dropshot::ApiDescription;
use dropshot::ErrorStatusCode;
use dropshot::HttpError;
use dropshot::HttpResponseError;
use dropshot::HttpResponseOk;
use dropshot::Path;
use dropshot::RequestContext;
use dropshot::endpoint;
use dropshot::test_util::TestContext;
use http::Method;
use http::StatusCode;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use crate::common;

// Define an enum error type.
#[derive(Debug, thiserror::Error, Serialize, JsonSchema)]
pub(crate) enum EnumError {
    // A user-defined custom error variant. This one is one of the classic
    // confusing TeX error messages.
    #[error("overfull \\hbox (badness {badness}) at line {line}")]
    OverfullHbox { badness: i32, line: u32 },
    // Variant constructed from Dropshot `HttpError`s.
    #[error("{internal_message}")]
    HttpError {
        message: String,
        error_code: Option<String>,
        #[serde(skip)]
        internal_message: String,
        #[serde(skip)]
        status: ErrorStatusCode,
    },
}

impl From<HttpError> for EnumError {
    fn from(e: HttpError) -> Self {
        EnumError::HttpError {
            message: e.external_message,
            error_code: e.error_code,
            internal_message: e.internal_message,
            status: e.status_code,
        }
    }
}

impl HttpResponseError for EnumError {
    fn status_code(&self) -> ErrorStatusCode {
        match self {
            EnumError::OverfullHbox { .. } => {
                ErrorStatusCode::INTERNAL_SERVER_ERROR
            }
            EnumError::HttpError { status, .. } => *status,
        }
    }
}

/// This is the same as `EnumError`, but with the non-serialized fields removed.
/// This should match what the client receives.
#[derive(Debug, Deserialize, Eq, PartialEq)]
enum DeserializedEnumError {
    OverfullHbox { badness: i32, line: u32 },
    HttpError { message: String, error_code: Option<String> },
}

// Also, define a struct error type wrapping an enum.
#[derive(Debug, thiserror::Error, Serialize, JsonSchema)]
#[error("{message}")]
pub(crate) struct StructError {
    message: String,
    kind: ErrorKind,
    #[serde(skip)]
    status: ErrorStatusCode,
}

#[derive(Debug, Deserialize, Serialize, JsonSchema, Eq, PartialEq)]
enum ErrorKind {
    /// Can't get ye flask.
    CantGetYeFlask,
    /// Flagrant error,
    Other,
}

/// This is the same as `StructError`, but with the non-serialized fields removed.
/// This should match what the client receives.
#[derive(Debug, Deserialize, Eq, PartialEq)]
struct DeserializedStructError {
    message: String,
    kind: ErrorKind,
}

impl From<HttpError> for StructError {
    fn from(e: HttpError) -> Self {
        Self {
            message: e.external_message,
            kind: ErrorKind::Other,
            status: e.status_code,
        }
    }
}

impl HttpResponseError for StructError {
    fn status_code(&self) -> ErrorStatusCode {
        self.status
    }
}

// The test endpoints take a boolean parameter that determines whether to return
// an error or a success. This is used primarily so that we can send a request
// with a path param that *doesn't* parse as a boolean, in order to trigger an
// extractor error and exercise the conversion of dropshot `HttpError`s into the
// user error type.
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub(crate) struct HandlerPathParam {
    should_error: bool,
}

#[endpoint {
    method = GET,
    path = "/test/enum-error/{should_error}",
}]
async fn enum_error_handler(
    _rqctx: RequestContext<()>,
    path: Path<HandlerPathParam>,
) -> Result<HttpResponseOk<()>, EnumError> {
    enum_error_inner(path)
}

#[endpoint {
    method = GET,
    path = "/test/struct-error/{should_error}",
}]
async fn struct_error_handler(
    _rqctx: RequestContext<()>,
    path: Path<HandlerPathParam>,
) -> Result<HttpResponseOk<()>, StructError> {
    struct_error_inner(path)
}

// A handler that returns a `dropshot::HttpError`. This is used by the OpenAPI
// tests for custom errors in order to ensure that handlers returning
// `HttpError` can coexist with those that return user-defined types.
#[endpoint {
    method = GET,
    path = "/test/dropshot-error/",
}]
async fn dropshot_error_handler(
    _rqctx: RequestContext<()>,
) -> Result<HttpResponseOk<()>, HttpError> {
    Err(HttpError::for_internal_error("something bad happened".to_string()))
}

fn enum_error_inner(
    path: Path<HandlerPathParam>,
) -> Result<HttpResponseOk<()>, EnumError> {
    let HandlerPathParam { should_error } = path.into_inner();
    if should_error {
        Err(EnumError::OverfullHbox { badness: 10000, line: 42 })
    } else {
        Ok(HttpResponseOk(()))
    }
}

fn struct_error_inner(
    path: Path<HandlerPathParam>,
) -> Result<HttpResponseOk<()>, StructError> {
    let HandlerPathParam { should_error } = path.into_inner();
    if should_error {
        Err(StructError {
            kind: ErrorKind::CantGetYeFlask,
            message: "can't get ye flask".to_string(),
            status: ErrorStatusCode::NOT_FOUND,
        })
    } else {
        Ok(HttpResponseOk(()))
    }
}

pub(crate) fn api() -> ApiDescription<()> {
    let mut api = ApiDescription::new();
    api.register(enum_error_handler).unwrap();
    api.register(struct_error_handler).unwrap();
    api.register(dropshot_error_handler).unwrap();
    api
}

#[dropshot::api_description]
pub(crate) trait CustomErrorApi {
    type Context;

    #[endpoint {
        method = GET,
        path = "/test/enum-error/{should_error}",
    }]
    async fn enum_error(
        rqctx: RequestContext<Self::Context>,
        path: Path<HandlerPathParam>,
    ) -> Result<HttpResponseOk<()>, EnumError>;

    #[endpoint {
        method = GET,
        path = "/test/struct-error/{should_error}",
    }]
    async fn struct_error(
        rqctx: RequestContext<Self::Context>,
        path: Path<HandlerPathParam>,
    ) -> Result<HttpResponseOk<()>, StructError>;

    #[endpoint {
        method = GET,
        path = "/test/dropshot-error/",
    }]
    async fn dropshot_error(
        rqctx: RequestContext<Self::Context>,
    ) -> Result<HttpResponseOk<()>, HttpError>;
}

enum ApiImpl {}

impl CustomErrorApi for ApiImpl {
    type Context = ();

    async fn enum_error(
        _rqctx: RequestContext<()>,
        path: Path<HandlerPathParam>,
    ) -> Result<HttpResponseOk<()>, EnumError> {
        enum_error_inner(path)
    }

    async fn struct_error(
        _rqctx: RequestContext<()>,
        path: Path<HandlerPathParam>,
    ) -> Result<HttpResponseOk<()>, StructError> {
        struct_error_inner(path)
    }

    async fn dropshot_error(
        _rqctx: RequestContext<()>,
    ) -> Result<HttpResponseOk<()>, HttpError> {
        Err(HttpError::for_internal_error("something bad happened".to_string()))
    }
}

// Test case: the enum error handler returns the user-defiend error variant.
#[tokio::test]
async fn test_enum_user_error() {
    let api = api();
    let testctx = common::test_setup_with_context(
        "test_enum_user_error",
        api,
        (),
        dropshot::HandlerTaskMode::Detached,
    );
    do_enum_user_error_test(testctx).await;
}

async fn do_enum_user_error_test(testctx: TestContext<()>) {
    let json = testctx
        .client_testctx
        .with_error_type::<DeserializedEnumError>()
        .make_request_error(
            Method::GET,
            "/test/enum-error/true",
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
    assert_eq!(
        dbg!(json),
        DeserializedEnumError::OverfullHbox { badness: 10000, line: 42 }
    );

    testctx.teardown().await;
}

// Test case: the enum error handler converts a Dropshot `HttpError` from an
// extractor into its user-defined error type.
#[tokio::test]
async fn test_enum_extractor_error() {
    let api = api();
    let testctx = common::test_setup_with_context(
        "test_enum_extractor_error",
        api,
        (),
        dropshot::HandlerTaskMode::Detached,
    );
    // This time, instead of /test/enum-error/true, let's send something that
    // doesn't parse as a bool, so that the path param extractor returns an error.
    let json = testctx
        .client_testctx
        .with_error_type::<DeserializedEnumError>()
        .make_request_error(
            Method::GET,
            "/test/enum-error/asdf",
            StatusCode::BAD_REQUEST,
        )
        .await;
    assert_eq!(
        dbg!(json),
        DeserializedEnumError::HttpError {
            message:
                "bad parameter in URL path: unable to parse 'asdf' as bool"
                    .to_string(),
            error_code: None
        },
    );

    testctx.teardown().await;
}

// Test case: the struct error handler returns the user-defiend error variant.
#[tokio::test]
async fn test_struct_user_error() {
    let api = api();
    let testctx = common::test_setup_with_context(
        "test_struct_user_error",
        api,
        (),
        dropshot::HandlerTaskMode::Detached,
    );
    do_struct_user_error_test(testctx).await;
}

async fn do_struct_user_error_test(testctx: TestContext<()>) {
    let json = testctx
        .client_testctx
        .with_error_type::<DeserializedStructError>()
        .make_request_error(
            Method::GET,
            "/test/struct-error/true",
            StatusCode::NOT_FOUND,
        )
        .await;
    assert_eq!(
        dbg!(json),
        DeserializedStructError {
            message: "can't get ye flask".to_string(),
            kind: ErrorKind::CantGetYeFlask,
        }
    );

    testctx.teardown().await;
}

// Test case: the struct error handler converts a Dropshot `HttpError` from an
// extractor into its user-defined error type.
#[tokio::test]
async fn test_struct_extractor_error() {
    let api = api();
    let testctx = common::test_setup_with_context(
        "test_struct_extractor_error",
        api,
        (),
        dropshot::HandlerTaskMode::Detached,
    );
    // This time, instead of /test/struct-error/true, let's send something that
    // doesn't parse as a bool, so that the path param extractor returns an error.
    let json = testctx
        .client_testctx
        .with_error_type::<DeserializedStructError>()
        .make_request_error(
            Method::GET,
            "/test/struct-error/this-wont-work",
            StatusCode::BAD_REQUEST,
        )
        .await;
    assert_eq!(
        dbg!(json),
        DeserializedStructError {
            message:
                "bad parameter in URL path: unable to parse 'this-wont-work' as bool"
                    .to_string(),
            kind: ErrorKind::Other,
        },
    );

    testctx.teardown().await;
}

// Test cases for trait-based APIs.

#[test]
fn test_trait_based_api() {
    custom_error_api_mod::stub_api_description().unwrap();
    custom_error_api_mod::api_description::<ApiImpl>().unwrap();
}

#[tokio::test]
async fn test_trait_enum_user_error() {
    let api = custom_error_api_mod::api_description::<ApiImpl>().unwrap();
    let testctx = common::test_setup_with_context(
        "test_trait_enum_user_error",
        api,
        (),
        dropshot::HandlerTaskMode::Detached,
    );
    do_struct_user_error_test(testctx).await;
}

#[tokio::test]
async fn test_trait_struct_user_error() {
    let api = custom_error_api_mod::api_description::<ApiImpl>().unwrap();
    let testctx = common::test_setup_with_context(
        "test_trait_struct_user_error",
        api,
        (),
        dropshot::HandlerTaskMode::Detached,
    );
    do_struct_user_error_test(testctx).await;
}