Function sanitize_error_message

Source
pub fn sanitize_error_message(error: &(dyn Error + 'static)) -> String
Expand description

Sanitize error messages to remove internal implementation details like stack traces. This ensures that sensitive internal information is not exposed to API clients.

The function traverses the error chain to find the deepest (root) error and returns its message. This is useful for API responses where we want to provide meaningful error information without exposing internal stack traces or implementation details.

§Arguments

  • error - The error to sanitize

§Returns

The message from the root cause error in the error chain

§Examples

use mistralrs_server_core::util::sanitize_error_message;

// For a simple error without chain
let error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
assert_eq!(sanitize_error_message(&error), "File not found");

// For chained errors, returns the root cause
let root = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "Access denied");
let wrapped = anyhow::Error::new(root).context("Failed to read file");
// This would return "Access denied" instead of "Failed to read file"