hyper/ffi/error.rs
1use crate::ffi::size_t;
2
3/// A more detailed error object returned by some hyper functions.
4///
5/// Compare with `hyper_code`, which is a simpler error returned from
6/// some hyper functions.
7///
8/// Methods:
9///
10/// - hyper_error_code: Get an equivalent hyper_code from this error.
11/// - hyper_error_print: Print the details of this error to a buffer.
12/// - hyper_error_free: Frees a hyper_error.
13pub struct hyper_error(crate::Error);
14
15/// A return code for many of hyper's methods.
16#[repr(C)]
17pub enum hyper_code {
18 /// All is well.
19 HYPERE_OK,
20 /// General error, details in the `hyper_error *`.
21 HYPERE_ERROR,
22 /// A function argument was invalid.
23 HYPERE_INVALID_ARG,
24 /// The IO transport returned an EOF when one wasn't expected.
25 ///
26 /// This typically means an HTTP request or response was expected, but the
27 /// connection closed cleanly without sending (all of) it.
28 HYPERE_UNEXPECTED_EOF,
29 /// Aborted by a user supplied callback.
30 HYPERE_ABORTED_BY_CALLBACK,
31 /// An optional hyper feature was not enabled.
32 #[cfg_attr(feature = "http2", allow(unused))]
33 HYPERE_FEATURE_NOT_ENABLED,
34 /// The peer sent an HTTP message that could not be parsed.
35 HYPERE_INVALID_PEER_MESSAGE,
36}
37
38// ===== impl hyper_error =====
39
40impl hyper_error {
41 fn code(&self) -> hyper_code {
42 use crate::error::Kind as ErrorKind;
43 use crate::error::User;
44
45 match self.0.kind() {
46 ErrorKind::Parse(_) => hyper_code::HYPERE_INVALID_PEER_MESSAGE,
47 ErrorKind::IncompleteMessage => hyper_code::HYPERE_UNEXPECTED_EOF,
48 ErrorKind::User(User::AbortedByCallback) => hyper_code::HYPERE_ABORTED_BY_CALLBACK,
49 // TODO: add more variants
50 _ => hyper_code::HYPERE_ERROR,
51 }
52 }
53
54 fn print_to(&self, dst: &mut [u8]) -> usize {
55 use std::io::Write;
56
57 let mut dst = std::io::Cursor::new(dst);
58
59 // A write! error doesn't matter. As much as possible will have been
60 // written, and the Cursor position will know how far that is (even
61 // if that is zero).
62 let _ = write!(dst, "{}", &self.0);
63 dst.position() as usize
64 }
65}
66
67ffi_fn! {
68 /// Frees a `hyper_error`.
69 ///
70 /// This should be used for any error once it is no longer needed.
71 fn hyper_error_free(err: *mut hyper_error) {
72 drop(non_null!(Box::from_raw(err) ?= ()));
73 }
74}
75
76ffi_fn! {
77 /// Get an equivalent `hyper_code` from this error.
78 fn hyper_error_code(err: *const hyper_error) -> hyper_code {
79 non_null!(&*err ?= hyper_code::HYPERE_INVALID_ARG).code()
80 }
81}
82
83ffi_fn! {
84 /// Print the details of this error to a buffer.
85 ///
86 /// The `dst_len` value must be the maximum length that the buffer can
87 /// store.
88 ///
89 /// The return value is number of bytes that were written to `dst`.
90 fn hyper_error_print(err: *const hyper_error, dst: *mut u8, dst_len: size_t) -> size_t {
91 let dst = unsafe {
92 std::slice::from_raw_parts_mut(dst, dst_len)
93 };
94 non_null!(&*err ?= 0).print_to(dst)
95 }
96}