mz_frontegg_client/error.rs
1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! This module defines custom error types and structs related to Frontegg API.
11//!
12//! [`ApiError`] is an error struct that represents an error returned by the
13//! Frontegg API. It contains information about the HTTP status code and a
14//! vector of error messages.
15//!
16//! [`Error`](`enum@Error`) is a custom error type that extends the
17//! [`mz_frontegg_auth::Error`] enum.
18//!
19//! It contains three variants:
20//! * [`Error::Auth`]: represents an authentication error from the
21//! [`mz-frontegg-auth`] crate.
22//! * [`Error::Transport`]: represents a transport error from the `reqwest`
23//! crate during a network request.
24//! * [`Error::Api`]: represents an Frontegg API error from a request.
25
26use std::fmt;
27
28use reqwest::StatusCode;
29use thiserror::Error;
30
31/// An error returned by the Frontegg API.
32#[derive(Debug, Clone)]
33pub struct ApiError {
34 /// The HTTP status code.
35 pub status_code: StatusCode,
36 /// A detailed message about the error conditions.
37 pub messages: Vec<String>,
38}
39
40impl fmt::Display for ApiError {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 write!(
43 f,
44 "{} (status {})",
45 self.messages.join(","),
46 self.status_code
47 )
48 }
49}
50
51impl std::error::Error for ApiError {}
52
53/// A custom error type that extends the `Error` enum in the `mz-frontegg-auth`
54/// crate.
55#[derive(Error, Debug)]
56pub enum Error {
57 /// An authentication error from the [`mz_frontegg_auth`] crate.
58 #[error(transparent)]
59 Auth(#[from] mz_frontegg_auth::Error),
60 /// A transport error from the `reqwest` crate during a network request.
61 #[error("frontegg error: transport: {0}")]
62 Transport(#[from] reqwest::Error),
63 /// An Frontegg API error from a request.
64 #[error("frontegg error: api: {0}")]
65 Api(#[from] ApiError),
66 /// An error indicating that the JWK set contains no keys.
67 #[error("JWK set contained no keys.")]
68 EmptyJwks,
69 /// An error thrown after trying to fetch the JWKS from well-known endpoint.
70 #[error("Error fetching JWKS.")]
71 FetchingJwks,
72 /// An error thrown after trying to build a [jsonwebtoken::DecodingKey] using JWKS
73 #[error("Converting JWK into decoding key.")]
74 ConvertingJwks,
75 /// An error indicating that the claims are invalid, acoording to the structure
76 /// or keys provided. If the error persists,
77 /// check if the token claims matches the struct.
78 #[error("Error decoding JWT claims: {0}")]
79 DecodingClaims(#[from] jsonwebtoken::errors::Error),
80}