use crate::std_shim::{String, ToString, format};
use serde::{Deserialize, Serialize};
pub const PROBLEM_TYPE_BASE: &str = "https://hupe1980.github.io/openadr/problems/";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProblemType {
pub slug: &'static str,
pub status: Option<u16>,
pub meaning: &'static str,
}
pub const PROBLEMS: &[ProblemType] = &[
ProblemType {
slug: "bad-request",
status: Some(400),
meaning: "The request is malformed in a way the more specific types below do not cover. Not retryable unchanged.",
},
ProblemType {
slug: "invalid-payload",
status: Some(400),
meaning: "The body parsed as JSON but failed validation — a field out of range, a missing required property, a pattern that does not match. `detail` names the field.",
},
ProblemType {
slug: "dangling-reference",
status: Some(400),
meaning: "The body refers to an object that does not exist, typically a `programID` on an event. The URL was fine; the payload was not.",
},
ProblemType {
slug: "unsupported-media-type",
status: Some(415),
meaning: "The body was labelled something other than `application/json` or an RFC 6839 `+json` type.",
},
ProblemType {
slug: "payload-too-large",
status: Some(413),
meaning: "The body exceeds the VTN's configured limit.",
},
ProblemType {
slug: "unauthorized",
status: Some(401),
meaning: "No credential, or one that did not verify. The response carries `WWW-Authenticate`. Get a new token and retry once.",
},
ProblemType {
slug: "missing-scope",
status: Some(403),
meaning: "The token verified but does not carry the scope this endpoint requires. Retrying will not help; the client's registration needs changing.",
},
ProblemType {
slug: "forbidden",
status: Some(403),
meaning: "The credential is valid and scoped but may not act on this object — a VEN writing another VEN's report, for instance.",
},
ProblemType {
slug: "not-found",
status: Some(404),
meaning: "No such object. Also returned for an object that exists but this client may not see, so that a `403` never confirms existence.",
},
ProblemType {
slug: "no-such-route",
status: Some(404),
meaning: "No endpoint at that path. Usually a base-path or version mistake rather than a missing object.",
},
ProblemType {
slug: "conflict",
status: Some(409),
meaning: "The write lost a race, or would break uniqueness. Re-read and retry.",
},
ProblemType {
slug: "not-implemented",
status: Some(501),
meaning: "A specified feature this deployment does not provide — the MQTT topic endpoints on a VTN with no broker, for example.",
},
ProblemType {
slug: "internal-server-error",
status: Some(500),
meaning: "A defect. `instance` is the request id; quote it.",
},
ProblemType {
slug: "storage-unavailable",
status: Some(500),
meaning: "The database could not be reached. Transient; retry with backoff.",
},
ProblemType {
slug: "unavailable",
status: Some(503),
meaning: "The VTN is up but declining work, typically while shutting down. Retry with backoff.",
},
ProblemType {
slug: "method-not-allowed",
status: Some(405),
meaning: "The path exists; the method is not defined for it.",
},
ProblemType {
slug: "timeout",
status: Some(504),
meaning: "The VTN gave up on the request. Retry with backoff.",
},
ProblemType {
slug: "error",
status: None,
meaning: "A status produced by a middleware with no more specific mapping.",
},
ProblemType {
slug: "unexpected",
status: None,
meaning: "Minted by **this crate's client**, not by a VTN: the peer returned an error with no problem body, so one was synthesised from the status. It says nothing about the peer beyond its status code.",
},
];
pub fn problem_type(slug: &str) -> Option<&'static ProblemType> {
PROBLEMS.iter().find(|p| p.slug == slug)
}
pub fn slug_of(type_uri: &str) -> Option<&'static ProblemType> {
type_uri
.strip_prefix(PROBLEM_TYPE_BASE)
.and_then(problem_type)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Problem {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub instance: Option<String>,
}
impl Problem {
pub fn new(status: u16, slug: &str, title: impl Into<String>) -> Self {
Self {
r#type: Some(format!("{PROBLEM_TYPE_BASE}{slug}")),
title: Some(title.into()),
status: Some(status),
detail: None,
instance: None,
}
}
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
self.detail = Some(detail.into());
self
}
pub fn with_instance(mut self, instance: impl Into<String>) -> Self {
self.instance = Some(instance.into());
self
}
pub fn status_or_500(&self) -> u16 {
self.status.unwrap_or(500)
}
}
impl core::fmt::Display for Problem {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let title = self.title.clone().unwrap_or_else(|| "Problem".to_string());
match &self.detail {
Some(d) => write!(f, "{} ({}): {d}", title, self.status_or_500()),
None => write!(f, "{} ({})", title, self.status_or_500()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn problem_serialises_only_what_is_set() {
let p = Problem::new(404, "not-found", "Not Found").with_detail("no such event");
let json = serde_json::to_string(&p).unwrap();
assert!(json.contains(r#""status":404"#));
assert!(json.contains(r#""title":"Not Found""#));
assert!(!json.contains("instance"));
}
#[test]
fn problem_type_is_dereferenceable() {
let p = Problem::new(400, "bad-request", "Bad Request");
assert_eq!(
p.r#type.as_deref(),
Some("https://hupe1980.github.io/openadr/problems/bad-request")
);
}
}