Skip to main content

systemprompt_api/routes/agent/contexts/webhook/
error.rs

1//! Webhook event-load error to HTTP mapping.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use systemprompt_agent::AgentError;
7use systemprompt_models::api::ApiError;
8use systemprompt_traits::RepositoryError;
9
10use super::validation::PayloadValidationError;
11use crate::error::ApiHttpError;
12
13#[derive(Debug, thiserror::Error)]
14pub enum LoadEventError {
15    #[error("Unknown event type: {0}")]
16    UnknownEventType(String),
17    #[error(transparent)]
18    Agent(#[from] AgentError),
19    #[error(transparent)]
20    Repository(#[from] RepositoryError),
21    #[error("{entity} not found: {id}")]
22    NotFound { entity: &'static str, id: String },
23    #[error("Invalid {field}: {source}")]
24    Deserialize {
25        field: &'static str,
26        #[source]
27        source: serde_json::Error,
28    },
29    #[error("{0} is required for this event type")]
30    MissingField(&'static str),
31    #[error("Invalid payload: {0}")]
32    InvalidPayload(String),
33    #[error(transparent)]
34    Payload(#[from] PayloadValidationError),
35}
36
37impl From<LoadEventError> for ApiHttpError {
38    fn from(err: LoadEventError) -> Self {
39        match err {
40            LoadEventError::Agent(e) => Self::from(e),
41            LoadEventError::Repository(e) => Self::from(e),
42            e @ LoadEventError::NotFound { .. } => Self::not_found(e.to_string()),
43            e @ (LoadEventError::UnknownEventType(_)
44            | LoadEventError::Deserialize { .. }
45            | LoadEventError::MissingField(_)
46            | LoadEventError::InvalidPayload(_)
47            | LoadEventError::Payload(PayloadValidationError::TooLarge { .. })) => {
48                Self::bad_request(e.to_string())
49            },
50            LoadEventError::Payload(e) => {
51                ApiError::internal("Webhook payload is invalid", e).into()
52            },
53        }
54    }
55}