Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add description field to error, more info in error message #137

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, error::Error, fmt::Display};

use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt::Display;

#[derive(Debug)]
pub enum KeycloakError {
Expand All @@ -12,31 +12,53 @@ pub enum KeycloakError {
},
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeycloakHttpError {
pub error: Option<String>,
#[serde(rename = "errorMessage")]
pub error_message: Option<String>,
}

impl From<reqwest::Error> for KeycloakError {
fn from(value: reqwest::Error) -> Self {
KeycloakError::ReqwestFailure(value)
}
}

impl Error for KeycloakError {
fn description(&self) -> &str {
"keycloak error"
}
impl Error for KeycloakError {}

fn cause(&self) -> Option<&dyn Error> {
None
impl Display for KeycloakError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
KeycloakError::ReqwestFailure(e) => write!(f, "keycloak error (network): {e}"),
KeycloakError::HttpFailure { status, body, text } => write!(
f,
"keycloak error (rest): {status} {}",
body.as_ref()
.and_then(|e| e.message())
.unwrap_or_else(|| Cow::from(text))
),
}
}
}

impl Display for KeycloakError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "keycloak error")
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeycloakHttpError {
pub error: Option<String>,
pub error_description: Option<String>,
#[serde(rename = "errorMessage")]
pub error_message: Option<String>,
}

impl KeycloakHttpError {
pub fn message(&self) -> Option<Cow<'_, str>> {
self.error_message
.as_deref()
.map(Cow::from)
.or_else(|| {
self.error
.as_deref()
.map(|error| {
format!(
"{} [{error}]",
self.error_description.as_deref().unwrap_or("null")
)
})
.map(Cow::from)
})
.or_else(|| self.error_description.as_deref().map(Cow::from))
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Add dependency to Cargo.toml:
keycloak = "25.0"
```

```rust#ignore
```rust, no_run
use keycloak::{
types::*,
{KeycloakAdmin, KeycloakAdminToken},
Expand Down Expand Up @@ -118,4 +118,4 @@ pub use error::KeycloakError;
pub use rest::{
KeycloakAdmin, KeycloakAdminToken, KeycloakServiceAccountAdminTokenRetriever,
KeycloakTokenSupplier,
};
};
2 changes: 1 addition & 1 deletion templates/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
{{ replace ( render ( read_to_str "templates/README.md" ) ) "```rust" "```rust#ignore" }}
{{ replace ( render ( read_to_str "templates/README.md" ) ) "```rust" "```rust, no_run" }}
*/

pub mod types;
Expand Down