Skip to main content

hyperlane_core/context/
impl.rs

1use super::*;
2
3/// Implementation of `Default` trait for `Context`.
4impl Default for Context {
5    /// Creates a default `Context` instance.
6    ///
7    /// # Returns
8    ///
9    /// - `Context` - A new context with default values and a static default server.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            request: Request::default(),
14            response: Response::default(),
15            route_params: HashMapXxHash3_64::default(),
16            original_target: None,
17            original_head: None,
18            attributes: ThreadSafeAttributeStore::default(),
19        }
20    }
21}
22
23/// Implementation of `PartialEq` trait for `Context`.
24impl PartialEq for Context {
25    /// Compares two `Context` instances for equality.
26    ///
27    /// # Arguments
28    ///
29    /// - `&Self` - The first `Context` instance.
30    /// - `&Self` - The second `Context` instance.
31    ///
32    /// # Returns
33    ///
34    /// - `bool` - True if the instances are equal, otherwise false.
35    #[inline(always)]
36    fn eq(&self, other: &Self) -> bool {
37        self.get_request() == other.get_request()
38            && self.get_response() == other.get_response()
39            && self.get_route_params() == other.get_route_params()
40            && self.get_attributes().len() == other.get_attributes().len()
41    }
42}
43
44/// Implementation of `Eq` trait for `Context`.
45impl Eq for Context {}
46
47/// Structured response serialization for [`Context`].
48impl Context {
49    /// Builds the response for the incoming request without sending it.
50    ///
51    /// A wire `HEAD` request receives only the status line and headers, even if
52    /// middleware rewrites the request method. A standalone context uses its
53    /// current request method. Explicit raw stream writes are not affected.
54    ///
55    /// # Returns
56    ///
57    /// - `Vec<u8>` - The serialized response bytes.
58    #[inline(always)]
59    #[must_use]
60    pub fn build_response(&mut self) -> Vec<u8> {
61        let is_head: bool = self
62            .try_get_original_head()
63            .unwrap_or_else(|| self.get_request().get_method().is_head());
64        if is_head {
65            self.get_mut_response().build_head()
66        } else {
67            self.get_mut_response().build()
68        }
69    }
70
71    /// Appends the serialized response for the incoming request into `out`.
72    ///
73    /// Preserves the same `HEAD` handling as [`Context::build_response`] while
74    /// reusing `out` without per-request heap allocation.
75    ///
76    /// # Arguments
77    ///
78    /// - `&mut Vec<u8>` - Destination wire buffer.
79    #[inline(always)]
80    pub fn build_response_into(&mut self, out: &mut Vec<u8>) {
81        let is_head: bool = self
82            .try_get_original_head()
83            .unwrap_or_else(|| self.get_request().get_method().is_head());
84        if is_head {
85            self.get_mut_response().build_head_into(out);
86        } else {
87            self.get_mut_response().build_into(out);
88        }
89    }
90
91    /// Serializes and sends the response over `stream` without per-response allocation.
92    ///
93    /// Preserves the same `HEAD` handling as [`Context::build_response`].
94    ///
95    /// # Arguments
96    ///
97    /// - `&mut Stream` - The active response stream.
98    ///
99    /// # Returns
100    ///
101    /// - `Result<(), ResponseError>` - Ok on success or transport error.
102    #[inline(always)]
103    pub async fn try_send_response(&mut self, stream: &mut Stream) -> Result<(), ResponseError> {
104        let is_head: bool = self
105            .try_get_original_head()
106            .unwrap_or_else(|| self.get_request().get_method().is_head());
107        if is_head {
108            stream.try_send_response_head(self.get_mut_response()).await
109        } else {
110            stream.try_send_response(self.get_mut_response()).await
111        }
112    }
113}
114
115/// Implementation of `From` trait for converting `usize` address into `&Context`.
116impl From<usize> for &'static Context {
117    /// Converts a memory address into a reference to `Context`.
118    ///
119    /// # Arguments
120    ///
121    /// - `usize` - The memory address of the `Context` instance.
122    ///
123    /// # Returns
124    ///
125    /// - `&'static Context` - A reference to the `Context` at the given address.
126    ///
127    /// # Safety
128    ///
129    /// - The address is guaranteed to be a valid `Context` instance
130    ///   that was previously converted from a reference and is managed by the runtime.
131    #[inline(always)]
132    fn from(address: usize) -> &'static Context {
133        unsafe { &*(address as *const Context) }
134    }
135}
136
137/// Implementation of `From` trait for converting `usize` address into `&mut Context`.
138impl<'a> From<usize> for &'a mut Context {
139    /// Converts a memory address into a mutable reference to `Context`.
140    ///
141    /// # Arguments
142    ///
143    /// - `usize` - The memory address of the `Context` instance.
144    ///
145    /// # Returns
146    ///
147    /// - `&'a mut Context` - A mutable reference to the `Context` at the given address.
148    ///
149    /// # Safety
150    ///
151    /// - The address is guaranteed to be a valid `Context` instance
152    ///   that was previously converted from a reference and is managed by the runtime.
153    #[inline(always)]
154    fn from(address: usize) -> &'a mut Context {
155        unsafe { &mut *(address as *mut Context) }
156    }
157}
158
159/// Implementation of `From` trait for converting `&Context` into `usize` address.
160impl From<&Context> for usize {
161    /// Converts a reference to `Context` into its memory address.
162    ///
163    /// # Arguments
164    ///
165    /// - `&Context` - The reference to the `Context` instance.
166    ///
167    /// # Returns
168    ///
169    /// - `usize` - The memory address of the `Context` instance.
170    #[inline(always)]
171    fn from(ctx: &Context) -> Self {
172        ctx as *const Context as usize
173    }
174}
175
176/// Implementation of `From` trait for converting `&mut Context` into `usize` address.
177impl From<&mut Context> for usize {
178    /// Converts a mutable reference to `Context` into its memory address.
179    ///
180    /// # Arguments
181    ///
182    /// - `&mut Context` - The mutable reference to the `Context` instance.
183    ///
184    /// # Returns
185    ///
186    /// - `usize` - The memory address of the `Context` instance.
187    #[inline(always)]
188    fn from(ctx: &mut Context) -> Self {
189        ctx as *mut Context as usize
190    }
191}
192
193/// Implementation of `AsRef` trait for `Context`.
194impl AsRef<Context> for Context {
195    /// Converts `&Context` to `&Context` via memory address conversion.
196    ///
197    /// # Returns
198    ///
199    /// - `&Self` - A reference to the `Context` instance.
200    #[inline(always)]
201    fn as_ref(&self) -> &Self {
202        let address: usize = self.into();
203        address.into()
204    }
205}
206
207/// Implementation of `AsMut` trait for `Context`.
208impl AsMut<Context> for Context {
209    /// Converts `&mut Context` to `&mut Context` via memory address conversion.
210    ///
211    /// # Returns
212    ///
213    /// - `&mut Self` - A mutable reference to the `Context` instance.
214    #[inline(always)]
215    fn as_mut(&mut self) -> &mut Self {
216        let address: usize = self.into();
217        address.into()
218    }
219}
220
221/// Implementation of `Lifetime` trait for `Context`.
222impl Lifetime for Context {
223    /// Converts a reference to the context into a `'static` reference.
224    ///
225    /// # Returns
226    ///
227    /// - `&'static Self` - A reference to the context with a `'static` lifetime.
228    ///
229    /// # Safety
230    ///
231    /// - The address is guaranteed to be a valid `Self` instance
232    ///   that was previously converted from a reference and is managed by the runtime.
233    #[inline(always)]
234    unsafe fn leak(&self) -> &'static Self {
235        let address: usize = self.into();
236        address.into()
237    }
238
239    /// Converts a reference to the context into a `'static` mutable reference.
240    ///
241    /// # Returns
242    ///
243    /// - `&'static mut Self` - A mutable reference to the context with a `'static` lifetime.
244    ///
245    /// # Safety
246    ///
247    /// - The address is guaranteed to be a valid `Self` instance
248    ///   that was previously converted from a reference and is managed by the runtime.
249    #[inline(always)]
250    unsafe fn leak_mut(&self) -> &'static mut Self {
251        let address: usize = self.into();
252        address.into()
253    }
254}
255
256/// Implementation of methods for `Context` structure.
257impl Context {
258    /// Clears the attributes the application set for the previous request.
259    ///
260    /// The original request target lives in its own field, so every attribute
261    /// in the map is request-local and the map is emptied outright. The map
262    /// keeps its capacity, so a connection that sets the same attributes over
263    /// and over stops allocating for them.
264    ///
265    /// # Returns
266    ///
267    /// - `&mut Self` - The context ready for the next request.
268    #[inline(always)]
269    pub(crate) fn reset_request_attributes(&mut self) -> &mut Self {
270        let attrs: &mut ThreadSafeAttributeStore = self.get_mut_attributes();
271        if !attrs.is_empty() {
272            return_attr_entries(attrs.drain());
273        }
274        self
275    }
276
277    /// Clears all route parameters while retaining the map's allocated capacity.
278    ///
279    /// Used between keep-alive requests on the same connection to avoid
280    /// reallocating the parameter map for every request.
281    ///
282    /// # Returns
283    ///
284    /// - `&mut Self` - A mutable reference to self for chaining.
285    #[inline(always)]
286    pub(crate) fn clear_route_params(&mut self) -> &mut Self {
287        let params: &mut HashMapXxHash3_64<String, String> = self.get_mut_route_params();
288        if !params.is_empty() {
289            return_route_param_entries(params.drain());
290        }
291        self
292    }
293
294    /// Attempts to retrieve a specific route parameter by its name.
295    ///
296    /// # Arguments
297    ///
298    /// - `T` - The name of the route parameter to retrieve.
299    ///
300    /// # Returns
301    ///
302    /// - `Option<String>` - The value of the route parameter if it exists.
303    #[inline(always)]
304    pub fn try_get_route_param<T>(&self, name: T) -> Option<String>
305    where
306        T: AsRef<str>,
307    {
308        self.get_route_params().get(name.as_ref()).cloned()
309    }
310
311    /// Retrieves a specific route parameter by its name, panicking if not found.
312    ///
313    /// # Arguments
314    ///
315    /// - `T` - The name of the route parameter to retrieve.
316    ///
317    /// # Returns
318    ///
319    /// - `String` - The value of the route parameter if it exists.
320    ///
321    /// # Panics
322    ///
323    /// - If the route parameter is not found.
324    #[inline(always)]
325    pub fn get_route_param<T>(&self, name: T) -> String
326    where
327        T: AsRef<str>,
328    {
329        self.try_get_route_param(name).unwrap()
330    }
331
332    /// Returns the original wire request target before routing or query-map conversion.
333    ///
334    /// # Returns
335    ///
336    /// - `Option<String>` - The unchanged path and query, or none for a manually constructed context.
337    pub fn get_request_target(&self) -> Option<String> {
338        self.get_request_target_ref().cloned()
339    }
340
341    /// Returns the original wire request target without copying it.
342    ///
343    /// # Returns
344    ///
345    /// - `Option<&String>` - The unchanged path and query, or none for a manually constructed context.
346    pub(crate) fn get_request_target_ref(&self) -> Option<&String> {
347        self.original_target.as_ref()
348    }
349
350    /// Takes the original target string out of the context for pooling.
351    ///
352    /// # Returns
353    ///
354    /// - `Option<String>` - The previously stored target string, if any.
355    #[inline(always)]
356    pub(crate) fn take_original_target(&mut self) -> Option<String> {
357        self.get_mut_original_target().take()
358    }
359
360    /// Stores the original target for the current request before user middleware runs.
361    ///
362    /// # Arguments
363    ///
364    /// - `T` - The unmodified target supplied by the protocol transport.
365    ///
366    /// # Returns
367    ///
368    /// - `&mut Self` - The context retaining its request-local target snapshot.
369    pub(crate) fn set_request_target<T>(&mut self, target: T) -> &mut Self
370    where
371        T: AsRef<str> + Into<String>,
372    {
373        let stored: &mut Option<String> = &mut self.original_target;
374        match stored {
375            Some(existing) => {
376                existing.clear();
377                existing.push_str(target.as_ref());
378            }
379            None => {
380                *stored = Some(target.into());
381            }
382        }
383        self
384    }
385
386    /// Validates and formats a plain external attribute key into a caller-provided stack buffer in a single pass.
387    ///
388    /// # Arguments
389    ///
390    /// - `&str` - Candidate external attribute key.
391    /// - `&'a mut [MaybeUninit<u8>; 128]` - Uninitialized stack buffer receiving `External("<key>")`.
392    ///
393    /// # Returns
394    ///
395    /// - `Option<&'a str>` - Borrowed formatted attribute map key when all bytes are unescaped printable ASCII.
396    #[inline(always)]
397    fn try_format_plain_external_key<'a>(
398        key: &str,
399        buf: &'a mut [MaybeUninit<u8>; 128],
400    ) -> Option<&'a str> {
401        let key_bytes: &[u8] = key.as_bytes();
402        let key_len: usize = key_bytes.len();
403        if key_len > EXTERNAL_ATTR_STACK_LIMIT {
404            return None;
405        }
406        let prefix_len: usize = EXTERNAL_ATTR_PREFIX.len();
407        let suffix_len: usize = EXTERNAL_ATTR_SUFFIX.len();
408        let total: usize = prefix_len + key_len + suffix_len;
409        // SAFETY: `key_len <= EXTERNAL_ATTR_STACK_LIMIT` guarantees `total <= 128`. Every copied
410        // byte is verified to be printable ASCII (`0x20..0x7f`) without `"` or `\`, so `0..total`
411        // is fully initialized and valid UTF-8.
412        unsafe {
413            let dst: *mut u8 = buf.as_mut_ptr().cast::<u8>();
414            let key_dst: *mut u8 = dst.add(prefix_len);
415            let mut idx: usize = 0;
416            while idx < key_len {
417                let byte: u8 = *key_bytes.get_unchecked(idx);
418                if !(0x20..0x7f).contains(&byte)
419                    || byte == EXTERNAL_ATTR_QUOTE
420                    || byte == EXTERNAL_ATTR_BACKSLASH
421                {
422                    return None;
423                }
424                *key_dst.add(idx) = byte;
425                idx += 1;
426            }
427            ptr::copy_nonoverlapping(EXTERNAL_ATTR_PREFIX.as_ptr(), dst, prefix_len);
428            ptr::copy_nonoverlapping(
429                EXTERNAL_ATTR_SUFFIX.as_ptr(),
430                key_dst.add(key_len),
431                suffix_len,
432            );
433            Some(from_utf8_unchecked(slice::from_raw_parts(dst, total)))
434        }
435    }
436
437    /// Returns the static map key for an internal framework attribute.
438    ///
439    /// # Arguments
440    ///
441    /// - `InternalAttribute` - The internal attribute variant.
442    ///
443    /// # Returns
444    ///
445    /// - `&'static str` - Formatted map key matching `Attribute::Internal(key).to_string()`.
446    #[inline(always)]
447    fn internal_attribute_key(key: InternalAttribute) -> &'static str {
448        match key {
449            InternalAttribute::TaskPanicData => INTERNAL_TASK_PANIC_KEY,
450            InternalAttribute::RequestErrorData => INTERNAL_REQUEST_ERROR_KEY,
451        }
452    }
453
454    /// Attempts to borrow a specific attribute by its key without cloning the stored value.
455    ///
456    /// # Arguments
457    ///
458    /// - `K` - The key of the attribute to retrieve.
459    ///
460    /// # Returns
461    ///
462    /// - `Option<&V>` - Borrowed reference to the attribute value if present and of type `V`.
463    #[inline(always)]
464    pub fn try_get_attribute_ref<V, K>(&self, key: K) -> Option<&V>
465    where
466        V: AnySendSyncClone,
467        K: AsRef<str>,
468    {
469        let key: &str = key.as_ref();
470        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
471        if let Some(formatted) = Self::try_format_plain_external_key(key, &mut stack_buf) {
472            return self
473                .get_attributes()
474                .get(formatted)
475                .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>());
476        }
477        self.get_attributes()
478            .get(&Attribute::External(key.to_owned()).to_string())
479            .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
480    }
481
482    /// Attempts to retrieve a specific attribute by its key, casting it to the specified type.
483    ///
484    /// The original request target is readable under its own key, because the
485    /// framework stores it in its own field rather than in the attribute map.
486    ///
487    /// # Arguments
488    ///
489    /// - `K` - The key of the attribute to retrieve.
490    ///
491    /// # Returns
492    ///
493    /// - `Option<V>` - The attribute value if it exists and can be cast to the specified type.
494    #[inline(always)]
495    pub fn try_get_attribute<V, K>(&self, key: K) -> Option<V>
496    where
497        V: AnySendSyncClone,
498        K: AsRef<str>,
499    {
500        let key: &str = key.as_ref();
501        if key == REQUEST_TARGET_ATTRIBUTE {
502            let target: Option<&String> = self.get_request_target_ref();
503            return match target {
504                Some(target) => {
505                    // The target is held as a plain string, so it is boxed
506                    // here to take the same downcast the map takes. This path
507                    // is not on the request path: the framework stores and
508                    // reads the target through its own field.
509                    let value: ArcAnySendSync = Arc::new(target.clone());
510                    value.downcast_ref::<V>().cloned()
511                }
512                None => None,
513            };
514        }
515        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
516        if let Some(formatted) = Self::try_format_plain_external_key(key, &mut stack_buf) {
517            return self
518                .get_attributes()
519                .get(formatted)
520                .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
521                .cloned();
522        }
523        self.get_attributes()
524            .get(&Attribute::External(key.to_owned()).to_string())
525            .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
526            .cloned()
527    }
528
529    /// Retrieves a specific attribute by its key, casting it to the specified type, panicking if not found.
530    ///
531    /// # Arguments
532    ///
533    /// - `K` - The key of the attribute to retrieve.
534    ///
535    /// # Returns
536    ///
537    /// - `V` - The attribute value if it exists and can be cast to the specified type.
538    ///
539    /// # Panics
540    ///
541    /// - If the attribute is not found.
542    #[inline(always)]
543    pub fn get_attribute<V, K>(&self, key: K) -> V
544    where
545        V: AnySendSyncClone,
546        K: AsRef<str>,
547    {
548        self.try_get_attribute(key).unwrap()
549    }
550
551    /// Sets an attribute in the context.
552    ///
553    /// # Arguments
554    ///
555    /// - `K` - The key of the attribute to set.
556    /// - `V` - The value of the attribute.
557    ///
558    /// # Returns
559    ///
560    /// - `&mut Self` - A reference to the modified context.
561    #[inline(always)]
562    pub fn set_attribute<K, V>(&mut self, key: K, value: V) -> &mut Self
563    where
564        K: AsRef<str>,
565        V: AnySendSyncClone,
566    {
567        let key_str: &str = key.as_ref();
568        let boxed: ArcAnySendSync = Arc::new(value);
569        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
570        if let Some(formatted) = Self::try_format_plain_external_key(key_str, &mut stack_buf) {
571            let attrs: &mut ThreadSafeAttributeStore = self.get_mut_attributes();
572            if let Some(existing) = attrs.get_mut(formatted) {
573                *existing = boxed;
574            } else {
575                let mut key_buf: String = take_attr_key_buffer();
576                key_buf.push_str(formatted);
577                attrs.insert(key_buf, boxed);
578            }
579            return self;
580        }
581        let map_key: String = Attribute::External(key_str.to_owned()).to_string();
582        self.get_mut_attributes().insert(map_key, boxed);
583        self
584    }
585
586    /// Removes an attribute from the context.
587    ///
588    /// # Arguments
589    ///
590    /// - `K` - The key of the attribute to remove.
591    ///
592    /// # Returns
593    ///
594    /// - `&mut Self` - A reference to the modified context.
595    #[inline(always)]
596    pub fn remove_attribute<K>(&mut self, key: K) -> &mut Self
597    where
598        K: AsRef<str>,
599    {
600        let key_str: &str = key.as_ref();
601        let mut stack_buf: [MaybeUninit<u8>; 128] = [const { MaybeUninit::uninit() }; 128];
602        if let Some(formatted) = Self::try_format_plain_external_key(key_str, &mut stack_buf) {
603            self.get_mut_attributes().remove(formatted);
604            return self;
605        }
606        self.get_mut_attributes()
607            .remove(&Attribute::External(key_str.to_owned()).to_string());
608        self
609    }
610
611    /// Clears all attributes from the context.
612    ///
613    /// # Returns
614    ///
615    /// - `&mut Self` - A reference to the modified context.
616    #[inline(always)]
617    pub fn clear_attribute(&mut self) -> &mut Self {
618        self.get_mut_attributes().clear();
619        self
620    }
621
622    /// Retrieves an internal framework attribute.
623    ///
624    /// # Arguments
625    ///
626    /// - `InternalAttribute` - The internal attribute key to retrieve.
627    ///
628    /// # Returns
629    ///
630    /// - `Option<V>` - The attribute value if it exists and can be cast to the specified type.
631    #[inline(always)]
632    fn try_get_internal_attribute<V>(&self, key: InternalAttribute) -> Option<V>
633    where
634        V: AnySendSyncClone,
635    {
636        self.get_attributes()
637            .get(Self::internal_attribute_key(key))
638            .and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<V>())
639            .cloned()
640    }
641
642    /// Retrieves an internal framework attribute.
643    ///
644    /// # Arguments
645    ///
646    /// - `InternalAttribute` - The internal attribute key to retrieve.
647    ///
648    /// # Returns
649    ///
650    /// - `V` - The attribute value if it exists and can be cast to the specified type.
651    ///
652    /// # Panics
653    ///
654    /// - If the attribute is not found.
655    #[inline(always)]
656    fn get_internal_attribute<V>(&self, key: InternalAttribute) -> V
657    where
658        V: AnySendSyncClone,
659    {
660        self.try_get_internal_attribute(key).unwrap()
661    }
662
663    /// Sets an internal framework attribute.
664    ///
665    /// # Arguments
666    ///
667    /// - `InternalAttribute` - The internal attribute key to set.
668    /// - `V` - The value of the attribute.
669    ///
670    /// # Returns
671    ///
672    /// - `&mut Self` - A reference to the modified context.
673    #[inline(always)]
674    fn set_internal_attribute<V>(&mut self, key: InternalAttribute, value: V) -> &mut Self
675    where
676        V: AnySendSyncClone,
677    {
678        self.get_mut_attributes().insert(
679            Self::internal_attribute_key(key).to_owned(),
680            Arc::new(value),
681        );
682        self
683    }
684
685    /// Stores panic data for the current task context.
686    ///
687    /// # Arguments
688    ///
689    /// - `PanicData` - The panic data specific to the current task.
690    ///
691    /// # Returns
692    ///
693    /// - `&mut Self` - Reference to the modified context for method chaining.
694    #[inline(always)]
695    pub fn set_task_panic(&mut self, panic_data: PanicData) -> &mut Self {
696        self.set_internal_attribute(InternalAttribute::TaskPanicData, panic_data)
697    }
698
699    /// Retrieves panic data associated with the current task.
700    ///
701    /// # Returns
702    ///
703    /// - `Option<PanicData>` - Task panic data if a panic was caught during execution.
704    #[inline(always)]
705    pub fn try_get_task_panic_data(&self) -> Option<PanicData> {
706        self.try_get_internal_attribute(InternalAttribute::TaskPanicData)
707    }
708
709    /// Retrieves panic data associated with the current task.
710    ///
711    /// # Returns
712    ///
713    /// - `PanicData` - Task panic data if available.
714    ///
715    /// # Panics
716    ///
717    /// - If no task panic data is found.
718    #[inline(always)]
719    pub fn get_task_panic_data(&self) -> PanicData {
720        self.get_internal_attribute(InternalAttribute::TaskPanicData)
721    }
722
723    /// Sets the request error information for the context.
724    ///
725    /// # Arguments
726    ///
727    /// - `RequestError` - The request error information to store.
728    ///
729    /// # Returns
730    ///
731    /// - `&mut Self` - A reference to the modified context.
732    #[inline(always)]
733    pub(crate) fn set_request_error_data(&mut self, request_error: RequestError) -> &mut Self {
734        self.set_internal_attribute(InternalAttribute::RequestErrorData, request_error)
735    }
736
737    /// Retrieves request error information if an error occurred during handling.
738    ///
739    /// # Returns
740    ///
741    /// - `Option<RequestError>` - The request error information if an error was caught.
742    #[inline(always)]
743    pub fn try_get_request_error_data(&self) -> Option<RequestError> {
744        self.try_get_internal_attribute(InternalAttribute::RequestErrorData)
745    }
746
747    /// Retrieves request error information if an error occurred during handling.
748    ///
749    /// # Returns
750    ///
751    /// - `RequestError` - The request error information if an error was caught.
752    ///
753    /// # Panics
754    ///
755    /// - If the request error information is not found.
756    #[inline(always)]
757    pub fn get_request_error_data(&self) -> RequestError {
758        self.get_internal_attribute(InternalAttribute::RequestErrorData)
759    }
760}