forked from IABTechLab/trusted-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
109 lines (90 loc) · 3.91 KB
/
error.rs
File metadata and controls
109 lines (90 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Error types for the trusted server.
//!
//! This module provides the main error type [`TrustedServerError`] used throughout
//! the application. All errors are designed to work with the `error-stack` crate
//! for rich error context and reporting.
use core::error::Error;
use derive_more::Display;
use http::StatusCode;
/// The main error type for trusted server operations.
///
/// This enum encompasses all possible errors that can occur during
/// request processing, configuration, and data handling.
#[allow(dead_code)]
#[derive(Debug, Display)]
pub enum TrustedServerError {
/// Client-side input/validation error resulting in a 400 Bad Request.
#[display("Bad request: {message}")]
BadRequest { message: String },
/// Configuration errors that prevent the server from starting.
#[display("Configuration error: {message}")]
Configuration { message: String },
/// GAM (Google Ad Manager) integration error.
#[display("GAM error: {message}")]
Gam { message: String },
/// GDPR consent handling error.
#[display("GDPR consent error: {message}")]
GdprConsent { message: String },
/// The synthetic secret key is using the insecure default value.
#[display("Synthetic secret key is set to the default value - this is insecure")]
InsecureSecretKey,
/// Invalid UTF-8 data encountered.
#[display("Invalid UTF-8 data: {message}")]
InvalidUtf8 { message: String },
/// HTTP header value creation failed.
#[display("Invalid HTTP header value: {message}")]
InvalidHeaderValue { message: String },
/// Key-value store operation failed.
#[display("KV store error: {store_name} - {message}")]
KvStore { store_name: String, message: String },
/// Prebid integration error.
#[display("Prebid error: {message}")]
Prebid { message: String },
/// Proxy error.
#[display("Proxy error: {message}")]
Proxy { message: String },
/// Settings parsing or validation failed.
#[display("Settings error: {message}")]
Settings { message: String },
/// Synthetic ID generation or validation failed.
#[display("Synthetic ID error: {message}")]
SyntheticId { message: String },
/// Template rendering error.
#[display("Template error: {message}")]
Template { message: String },
/// Fastly platform error.
#[display("Fastly error: {message}")]
FastlyError { message: String },
}
impl Error for TrustedServerError {}
/// Extension trait for converting [`TrustedServerError`] to HTTP responses.
#[allow(dead_code)]
pub trait IntoHttpResponse {
/// Convert the error into an HTTP status code.
fn status_code(&self) -> StatusCode;
/// Get the error message to show to users (uses the Display implementation).
fn user_message(&self) -> String;
}
impl IntoHttpResponse for TrustedServerError {
fn status_code(&self) -> StatusCode {
match self {
Self::BadRequest { .. } => StatusCode::BAD_REQUEST,
Self::Configuration { .. } | Self::Settings { .. } => StatusCode::INTERNAL_SERVER_ERROR,
Self::Gam { .. } => StatusCode::BAD_GATEWAY,
Self::GdprConsent { .. } => StatusCode::BAD_REQUEST,
Self::InsecureSecretKey => StatusCode::INTERNAL_SERVER_ERROR,
Self::InvalidHeaderValue { .. } => StatusCode::BAD_REQUEST,
Self::InvalidUtf8 { .. } => StatusCode::BAD_REQUEST,
Self::KvStore { .. } => StatusCode::SERVICE_UNAVAILABLE,
Self::Prebid { .. } => StatusCode::BAD_GATEWAY,
Self::Proxy { .. } => StatusCode::BAD_GATEWAY,
Self::SyntheticId { .. } => StatusCode::INTERNAL_SERVER_ERROR,
Self::Template { .. } => StatusCode::INTERNAL_SERVER_ERROR,
Self::FastlyError { .. } => StatusCode::BAD_GATEWAY,
}
}
fn user_message(&self) -> String {
// Use the Display implementation which already has the specific error message
self.to_string()
}
}