Skip to content

Commit

Permalink
add description field to error, more info in error message (#137)
Browse files Browse the repository at this point in the history
* add description field to error, more info in error message

* more generic error message

* doc: use no_run instead ignore
  • Loading branch information
kilork authored Oct 7, 2024
1 parent 7a72f05 commit e86f369
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
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

0 comments on commit e86f369

Please sign in to comment.