Skip to main content

find_error_element_slice

Function find_error_element_slice 

Source
pub fn find_error_element_slice(body: &[u8]) -> &[u8] 
Expand description

Locate the <Error> element within an AWS REST XML error response body and return a byte slice covering it (<Error>...</Error>).

Handles both wrapped and unwrapped error envelopes:

  • Wrapped (<ErrorResponse>...<Error>...</Error>...</ErrorResponse>): returns the inner <Error> element’s slice.
  • Unwrapped (<Error>...</Error> as the document root): returns the full body unchanged (the root is the <Error> element).

Falls back to the full body if it isn’t valid UTF-8, isn’t parseable as XML, or contains no <Error> element. Returning the body unchanged on failure lets downstream error deserialization surface a generic / “unhandled” error variant rather than panicking on malformed responses.

Robust to start-tag attributes (e.g. <Error xmlns="..."), nested same-name elements, comments, and CDATA sections — all of which a naive body_str.find("<Error>") substring match would mishandle.