graphql_starter/error/
graphql.rs

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::{any::Any, sync::Arc};

use async_graphql::{ErrorExtensions, Name};
use error_info::ErrorInfo;
use indexmap::IndexMap;
use tracing_error::SpanTrace;

use super::{Error, GenericErrorCode};

/// GraphQL Result that represents either success ([`Ok`]) or failure ([`Err`])
pub type GraphQLResult<T, E = Box<GraphQLError>> = std::result::Result<T, E>;

/// GraphQL error
#[derive(Clone)]
pub enum GraphQLError {
    Async(async_graphql::Error, SpanTrace),
    Custom(Error),
}

impl GraphQLError {
    /// Creates a new [GraphQLError]
    pub fn new(info: impl ErrorInfo + Send + Sync + 'static, unexpected: bool) -> Self {
        Self::Custom(Error::new(info, unexpected))
    }

    /// Creates a new internal server error
    pub fn internal(reason: impl Into<String>) -> Self {
        Self::Custom(Error::internal(reason))
    }

    /// Appends a property to the error
    pub fn with_property(self, key: &str, value: serde_json::Value) -> Self {
        match self {
            Self::Async(err, ctx) => {
                let err = err.extend_with(|_, e| match async_graphql::Value::try_from(value) {
                    Ok(value) => e.set(key, value),
                    Err(err) => tracing::error!("Couldn't deserialize error value: {err}"),
                });
                Self::Async(err, ctx)
            }
            Self::Custom(err) => {
                let err = err.with_property(key, value);
                Self::Custom(err)
            }
        }
    }

    /// Boxes this error
    pub fn boxed(self) -> Box<Self> {
        Box::new(self)
    }

    /// Checks wether this error is unexpected or not
    fn is_unexpected(&self) -> bool {
        match self {
            // errors from the graphql lib are unexpected
            GraphQLError::Async(_, _) => true,
            GraphQLError::Custom(err) => err.unexpected,
        }
    }

    /// Returns the string representation of the error
    pub fn to_string(&self, include_context: bool) -> String {
        match self {
            GraphQLError::Async(err, context) => {
                let code = GenericErrorCode::InternalServerError;
                let status = code.status();
                if include_context {
                    format!(
                        "[{} {}] {}: {}\n{}",
                        status.as_str(),
                        status.canonical_reason().unwrap_or("Unknown"),
                        code.code(),
                        &err.message,
                        &context
                    )
                } else {
                    format!(
                        "[{} {}] {}: {}",
                        status.as_str(),
                        status.canonical_reason().unwrap_or("Unknown"),
                        code.code(),
                        &err.message
                    )
                }
            }
            GraphQLError::Custom(err) => {
                if include_context {
                    format!("{err:#}")
                } else {
                    format!("{err}")
                }
            }
        }
    }
}

impl From<async_graphql::Error> for GraphQLError {
    fn from(err: async_graphql::Error) -> Self {
        GraphQLError::Async(err, SpanTrace::capture())
    }
}
impl From<async_graphql::Error> for Box<GraphQLError> {
    fn from(err: async_graphql::Error) -> Self {
        GraphQLError::from(err).boxed()
    }
}

impl From<Box<Error>> for GraphQLError {
    fn from(err: Box<Error>) -> Self {
        (*err).into()
    }
}
impl From<Box<Error>> for Box<GraphQLError> {
    fn from(err: Box<Error>) -> Self {
        (*err).into()
    }
}

impl<T> From<T> for GraphQLError
where
    T: Into<Error>,
{
    fn from(err: T) -> Self {
        GraphQLError::Custom(err.into())
    }
}
impl<T> From<T> for Box<GraphQLError>
where
    T: Into<Error>,
{
    fn from(err: T) -> Self {
        GraphQLError::from(err).boxed()
    }
}

impl From<Box<GraphQLError>> for async_graphql::Error {
    fn from(value: Box<GraphQLError>) -> Self {
        (*value).into()
    }
}
impl From<GraphQLError> for async_graphql::Error {
    fn from(e: GraphQLError) -> Self {
        // Trace the error when converting to async_graphql error, which is done just before responding to requests
        let new_error = match &e {
            GraphQLError::Async(err, _) => err
                .extensions
                .as_ref()
                .map(|e| e.get("statusCode").is_none())
                .unwrap_or(true),
            GraphQLError::Custom(_) => true,
        };
        if new_error {
            if e.is_unexpected() {
                tracing::error!("{}", e.to_string(true))
            } else if tracing::event_enabled!(tracing::Level::DEBUG) {
                tracing::warn!("{}", e.to_string(true))
            } else {
                tracing::warn!("{}", e.to_string(false))
            }
        }

        // Convert type
        let (gql_err, err_info): (async_graphql::Error, Option<Arc<dyn ErrorInfo + Send + Sync + 'static>>) = match e {
            GraphQLError::Async(mut err, _) => {
                if new_error {
                    // Hide the message and provide generic internal error info
                    err.source = Some(Arc::new(err.message));
                    err.message = "Internal server error".into();
                    (err, Some(Arc::new(GenericErrorCode::InternalServerError)))
                } else {
                    // Already converted
                    (err, None)
                }
            }
            GraphQLError::Custom(err) => {
                let source = err.source.map(|s| {
                    let source: Arc<dyn Any + Send + Sync> = Arc::new(s);
                    source
                });
                let async_err = async_graphql::Error {
                    message: err.info.message(),
                    source,
                    extensions: None,
                }
                .extend_with(|_, e| {
                    if let Some(prop) = err.properties {
                        for (k, v) in prop.into_iter() {
                            if k == "statusCode"
                                || k == "statusKind"
                                || k == "errorCode"
                                || k == "rawMessage"
                                || k == "messageFields"
                            {
                                tracing::error!("Error '{}' contains a reserved property: {}", err.info.code(), k);
                                continue;
                            }
                            match async_graphql::Value::try_from(v) {
                                Ok(v) => e.set(k, v),
                                Err(err) => tracing::error!("Couldn't deserialize error value: {err}"),
                            }
                        }
                    }
                });
                (async_err, Some(err.info))
            }
        };
        if let Some(err_info) = err_info {
            // Append error info properties
            gql_err.extend_with(|_, e| {
                let status = err_info.status();
                e.set("statusCode", status.as_u16());
                if let Some(reason) = status.canonical_reason() {
                    e.set("statusKind", reason);
                }
                e.set("errorCode", err_info.code());
                e.set("rawMessage", err_info.raw_message());
                let fields = err_info.fields();
                if !fields.is_empty() {
                    let fields_map = IndexMap::from_iter(fields.into_iter().map(|(k, v)| (Name::new(k), v.into())));
                    e.set("messageFields", fields_map);
                }
            })
        } else {
            gql_err
        }
    }
}