1use thiserror::Error;
2
3#[cfg(feature = "extension")]
4use axum::{
5 http::StatusCode,
6 response::{IntoResponse, Response},
7 Json,
8};
9#[cfg(feature = "extension")]
10use serde_json::json;
11
12#[derive(Error, Debug)]
13pub enum SentryTunnelError {
14 #[error("Invalid envelope encoding")]
15 InvalidEncoding,
16
17 #[error("Empty envelope")]
18 EmptyEnvelope,
19
20 #[error("Invalid envelope header: {0}")]
21 InvalidHeader(String),
22
23 #[error("Missing DSN in envelope")]
24 MissingDsn,
25
26 #[error("Invalid DSN URL: {0}")]
27 InvalidDsnUrl(String),
28
29 #[error("Invalid sentry hostname: {0}")]
30 InvalidHostname(String),
31
32 #[error("Invalid sentry project id: {0}")]
33 InvalidProjectId(String),
34
35 #[error("Error tunneling to sentry: {0}")]
36 TunnelError(String),
37}
38
39#[cfg(feature = "extension")]
40impl IntoResponse for SentryTunnelError {
41 fn into_response(self) -> Response {
42 let (status, message) = match &self {
43 Self::InvalidEncoding
44 | Self::EmptyEnvelope
45 | Self::InvalidHeader(_)
46 | Self::MissingDsn
47 | Self::InvalidDsnUrl(_)
48 | Self::InvalidHostname(_)
49 | Self::InvalidProjectId(_) => (StatusCode::BAD_REQUEST, self.to_string()),
50
51 Self::TunnelError(_) => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
52 };
53
54 (status, Json(json!({ "error": message }))).into_response()
55 }
56}