Skip to content
Open
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
12 changes: 6 additions & 6 deletions examples/magic8ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ impl IntoErrorPayload for MysticError {

fn error_code(&self) -> i64 {
match self {
Self::CrystalBallCloudy => -32001,
Self::StarsMisaligned { .. } => -32002,
Self::MercuryRetrograde => -32003,
Self::UnreadableAura(_) => -32004,
Self::CrystalBallCloudy => 2001,
Self::StarsMisaligned { .. } => 2002,
Self::MercuryRetrograde => 2003,
Self::UnreadableAura(_) => 2004,
}
}

Expand All @@ -100,15 +100,15 @@ impl IntoErrorPayload for MysticError {
fn into_error_payload(self) -> ErrorPayload<Box<serde_json::value::RawValue>> {
let code = self.error_code();
let message = self.error_message();
let data = match &self {
let data = match self {
Self::CrystalBallCloudy => serde_json::value::to_raw_value(&CloudyDetail {
visibility: "opaque",
suggestion: "try polishing the ball",
})
.ok(),
Self::StarsMisaligned { sign } => {
serde_json::value::to_raw_value(&MisalignmentDetail {
sign: sign.clone(),
sign,
interfering_planet: "Saturn",
})
.ok()
Expand Down
28 changes: 18 additions & 10 deletions src/types/resp/into_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::borrow::Cow;
///
/// # Provided methods
///
/// Only [`error_code`] is required. The remaining methods have sensible
/// defaults:
/// Only [`error_code`] and the associated type `ErrData` must be specified.
/// The remaining methods have sensible defaults:
///
/// | Method | Default |
/// |----------------------|----------------------------------------|
Expand Down Expand Up @@ -41,8 +41,8 @@ use std::borrow::Cow;
///
/// fn error_code(&self) -> i64 {
/// match self {
/// Self::NotFound(_) => 404,
/// Self::RateLimited { .. } => 429,
/// Self::NotFound(_) => 1001,
/// Self::RateLimited { .. } => 1002,
/// }
/// }
///
Expand All @@ -65,7 +65,7 @@ use std::borrow::Cow;
///
/// let err = AppError::NotFound("user/42".into());
/// let payload = err.into_error_payload();
/// assert_eq!(payload.code, 404);
/// assert_eq!(payload.code, 1001);
/// assert_eq!(payload.message, "Not found");
/// assert_eq!(payload.data.as_deref(), Some("user/42"));
/// ```
Expand Down Expand Up @@ -215,6 +215,14 @@ impl IntoErrorPayload for () {
}
}

impl IntoErrorPayload for core::convert::Infallible {
type ErrData = ();

fn error_code(&self) -> i64 {
match *self {}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -234,15 +242,15 @@ mod tests {
#[test]
fn error_payload_identity() {
let original = ErrorPayload {
code: 404,
code: 1001,
message: Cow::Borrowed("Not found"),
data: Some("missing".to_string()),
};
assert_eq!(original.error_code(), 404);
assert_eq!(original.error_code(), 1001);
assert_eq!(original.error_message(), "Not found");

let payload = original.into_error_payload();
assert_eq!(payload.code, 404);
assert_eq!(payload.code, 1001);
assert_eq!(payload.message, "Not found");
assert_eq!(payload.data.as_deref(), Some("missing"));
}
Expand Down Expand Up @@ -309,7 +317,7 @@ mod tests {

fn error_code(&self) -> i64 {
match self {
Self::NotFound(_) => 404,
Self::NotFound(_) => 1001,
Self::Internal => -32603,
}
}
Expand All @@ -331,7 +339,7 @@ mod tests {

let err = AppError::NotFound("user/42".into());
let payload = err.into_error_payload();
assert_eq!(payload.code, 404);
assert_eq!(payload.code, 1001);
assert_eq!(payload.message, "Not found");
assert_eq!(payload.data.as_deref(), Some("user/42"));

Expand Down