pub enum MiddlewareError {
Unauthenticated(String),
Unauthorized(String),
RateLimitExceeded {
message: String,
retry_after: Option<u64>,
},
InvalidRequest(String),
Internal(String),
Custom {
code: String,
message: String,
},
}
Expand description
Errors that can occur during middleware execution
These errors are converted to McpError
by the framework and then to
JSON-RPC error responses. Middleware should use semantic error types
rather than creating JSON-RPC errors directly.
§Conversion Chain
MiddlewareError → McpError → JsonRpcError → HTTP/Lambda response
§JSON-RPC Error Codes
Each error variant maps to a specific JSON-RPC error code (see error_codes
):
Unauthenticated
→-32001
“Authentication required”Unauthorized
→-32002
“Permission denied”RateLimitExceeded
→-32003
“Rate limit exceeded”InvalidRequest
→-32600
(standard Invalid Request)Internal
→-32603
(standard Internal error)Custom{code, msg}
→ custom code from variant
§Examples
use turul_http_mcp_server::middleware::{MiddlewareError, McpMiddleware, RequestContext, SessionInjection};
use turul_mcp_session_storage::SessionView;
use async_trait::async_trait;
struct ApiKeyAuth {
valid_key: String,
}
#[async_trait]
impl McpMiddleware for ApiKeyAuth {
async fn before_dispatch(
&self,
ctx: &mut RequestContext<'_>,
_session: Option<&dyn SessionView>,
_injection: &mut SessionInjection,
) -> Result<(), MiddlewareError> {
let key = ctx.metadata()
.get("api-key")
.and_then(|v| v.as_str())
.ok_or_else(|| MiddlewareError::Unauthorized("Missing API key".into()))?;
if key != self.valid_key {
return Err(MiddlewareError::Unauthorized("Invalid API key".into()));
}
Ok(())
}
}
Variants§
Unauthenticated(String)
Authentication required but not provided
Authentication provided but insufficient permissions
RateLimitExceeded
Rate limit exceeded
InvalidRequest(String)
Request validation failed
Internal(String)
Internal middleware error (should not expose to client)
Custom
Custom error with code and message
Implementations§
Source§impl MiddlewareError
impl MiddlewareError
Sourcepub fn unauthenticated(msg: impl Into<String>) -> Self
pub fn unauthenticated(msg: impl Into<String>) -> Self
Create an unauthenticated error
Create an unauthorized error
Sourcepub fn rate_limit(msg: impl Into<String>, retry_after: Option<u64>) -> Self
pub fn rate_limit(msg: impl Into<String>, retry_after: Option<u64>) -> Self
Create a rate limit error
Sourcepub fn invalid_request(msg: impl Into<String>) -> Self
pub fn invalid_request(msg: impl Into<String>) -> Self
Create an invalid request error
Trait Implementations§
Source§impl Clone for MiddlewareError
impl Clone for MiddlewareError
Source§fn clone(&self) -> MiddlewareError
fn clone(&self) -> MiddlewareError
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for MiddlewareError
impl Debug for MiddlewareError
Source§impl Display for MiddlewareError
impl Display for MiddlewareError
Source§impl Error for MiddlewareError
impl Error for MiddlewareError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl PartialEq for MiddlewareError
impl PartialEq for MiddlewareError
impl StructuralPartialEq for MiddlewareError
Auto Trait Implementations§
impl Freeze for MiddlewareError
impl RefUnwindSafe for MiddlewareError
impl Send for MiddlewareError
impl Sync for MiddlewareError
impl Unpin for MiddlewareError
impl UnwindSafe for MiddlewareError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more