use http::{Response, StatusCode, header};
use crate::{Invocation, JmapError};
use jmap_types::MethodErrorType;
use jmap_types::problem::ProblemDetails;
pub fn problem_response(
status: StatusCode,
details: &ProblemDetails,
content_language: Option<&str>,
) -> Response<String> {
let body = serde_json::to_string(details).expect("ProblemDetails is infallible");
let mut builder = Response::builder()
.status(status)
.header(header::CONTENT_TYPE, jmap_types::problem::PROBLEM_JSON);
if let Some(lang) = content_language
&& let Ok(value) = header::HeaderValue::from_str(lang)
{
builder = builder.header(header::CONTENT_LANGUAGE, value);
}
builder.body(body).expect("valid status code and headers")
}
pub fn error_invocation(call_id: &str, err: JmapError) -> Invocation {
let err_value = serde_json::to_value(&err)
.expect("JmapError Serialize invariant — see jmap_error_all_constructors_serialize");
Invocation::new("error", err_value, call_id)
}
pub fn error_status(err: &JmapError) -> StatusCode {
match err.error_type {
MethodErrorType::NotJson
| MethodErrorType::NotRequest
| MethodErrorType::Limit
| MethodErrorType::UnknownCapability
| MethodErrorType::InvalidArguments
| MethodErrorType::RequestTooLarge => StatusCode::BAD_REQUEST,
MethodErrorType::Forbidden => StatusCode::FORBIDDEN,
MethodErrorType::ServerFail => StatusCode::INTERNAL_SERVER_ERROR,
MethodErrorType::ServerUnavailable => StatusCode::SERVICE_UNAVAILABLE,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct RequestError {
status: StatusCode,
err: JmapError,
}
impl std::fmt::Display for RequestError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.err.description.as_deref() {
Some(desc) => write!(
f,
"{}: {}: {}",
self.status.as_u16(),
self.err.error_type,
desc
),
None => write!(f, "{}: {}", self.status.as_u16(), self.err.error_type),
}
}
}
impl std::error::Error for RequestError {}
impl RequestError {
pub fn problem_details(&self) -> ProblemDetails {
ProblemDetails::for_jmap_error(&self.err, self.status.as_u16())
}
pub fn status(&self) -> StatusCode {
self.status
}
pub fn into_response(self) -> Response<String> {
problem_response(self.status, &self.problem_details(), None)
}
}
pub fn request_error(err: JmapError) -> RequestError {
let status = error_status(&err);
RequestError { status, err }
}
impl From<JmapError> for RequestError {
fn from(err: JmapError) -> Self {
request_error(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::StatusCode;
#[test]
fn error_invocation_structure() {
let inv = error_invocation("c0", JmapError::unknown_method());
assert_eq!(inv.name, "error");
assert_eq!(inv.call_id, "c0");
}
#[test]
fn error_invocation_args_contains_type() {
let inv = error_invocation("c0", JmapError::unknown_method());
assert_eq!(inv.arguments["type"], "unknownMethod");
}
#[test]
fn error_invocation_server_fail() {
let inv = error_invocation("y", JmapError::server_fail("boom"));
assert_eq!(inv.arguments["type"], "serverFail");
assert_eq!(inv.arguments["description"], "boom");
}
#[test]
fn error_status_unknown_capability_is_400() {
let e: JmapError =
serde_json::from_value(serde_json::json!({"type": "unknownCapability"})).unwrap();
assert_eq!(error_status(&e), StatusCode::BAD_REQUEST);
}
#[test]
fn error_status_invalid_arguments_is_400() {
assert_eq!(
error_status(&JmapError::invalid_arguments("x")),
StatusCode::BAD_REQUEST
);
}
#[test]
fn error_status_request_too_large_is_400() {
assert_eq!(
error_status(&JmapError::request_too_large()),
StatusCode::BAD_REQUEST
);
}
#[test]
fn error_status_forbidden_is_403() {
assert_eq!(error_status(&JmapError::forbidden()), StatusCode::FORBIDDEN);
}
#[test]
fn error_status_account_not_found_is_500() {
assert_eq!(
error_status(&JmapError::account_not_found()),
StatusCode::INTERNAL_SERVER_ERROR
);
}
#[test]
fn error_status_server_fail_is_500() {
assert_eq!(
error_status(&JmapError::server_fail("x")),
StatusCode::INTERNAL_SERVER_ERROR
);
}
#[test]
fn error_status_unknown_type_is_500() {
let e: JmapError =
serde_json::from_value(serde_json::json!({"type": "totallyMadeUp"})).unwrap();
assert_eq!(error_status(&e), StatusCode::INTERNAL_SERVER_ERROR);
}
#[test]
fn request_error_derives_status() {
let re = request_error(JmapError::invalid_arguments("bad"));
assert_eq!(re.into_response().status(), StatusCode::BAD_REQUEST);
}
#[test]
fn request_error_into_response_status_code() {
let re = request_error(JmapError::invalid_arguments("bad"));
let resp = re.into_response();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn request_error_content_type_is_problem_json() {
let re = request_error(JmapError::not_request());
let resp = re.into_response();
assert_eq!(
resp.headers()
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok()),
Some("application/problem+json"),
"Content-Type must be application/problem+json per RFC 9457"
);
}
#[test]
fn request_error_type_is_full_urn() {
let body: serde_json::Value = serde_json::from_str(
&request_error(JmapError::not_request())
.into_response()
.into_body(),
)
.unwrap();
assert_eq!(
body["type"], "urn:ietf:params:jmap:error:notRequest",
"type must be full URN"
);
}
#[test]
fn request_error_status_field_matches_http_status() {
let body: serde_json::Value = serde_json::from_str(
&request_error(JmapError::not_request())
.into_response()
.into_body(),
)
.unwrap();
assert_eq!(body["status"], 400, "status field must match HTTP code");
}
#[test]
fn request_error_limit_includes_limit_property() {
let body: serde_json::Value = serde_json::from_str(
&request_error(JmapError::limit("maxCallsInRequest"))
.into_response()
.into_body(),
)
.unwrap();
assert_eq!(
body["limit"], "maxCallsInRequest",
"limit property must name the exceeded limit"
);
assert_eq!(body["type"], "urn:ietf:params:jmap:error:limit");
}
#[test]
fn request_error_limit_with_description_includes_limit_and_detail() {
let mut err = JmapError::limit("maxCallsInRequest");
err.description = Some("17 calls exceed 16".to_owned());
let body: serde_json::Value =
serde_json::from_str(&request_error(err).into_response().into_body()).unwrap();
assert_eq!(
body,
serde_json::json!({
"type": "urn:ietf:params:jmap:error:limit",
"status": 400,
"limit": "maxCallsInRequest",
"detail": "17 calls exceed 16",
})
);
}
#[test]
fn jmap_error_all_constructors_serialize() {
let errors = vec![
JmapError::not_json(),
JmapError::not_request(),
JmapError::limit("maxCallsInRequest"),
JmapError::unknown_capability_with_detail("urn:example:unknown"),
JmapError::forbidden(),
JmapError::server_fail("test"),
JmapError::server_unavailable(),
JmapError::server_partial_fail(),
JmapError::unknown_method(),
JmapError::invalid_arguments("x"),
JmapError::invalid_result_reference(),
JmapError::account_not_found(),
JmapError::account_not_supported_by_method(),
JmapError::account_read_only(),
JmapError::request_too_large(),
JmapError::too_large(),
JmapError::rate_limit(),
JmapError::state_mismatch(),
JmapError::cannot_calculate_changes(),
JmapError::anchor_not_found(),
JmapError::unsupported_sort(),
JmapError::unsupported_filter(),
JmapError::too_many_changes_with_limit(100),
JmapError::from_account_not_found(),
JmapError::from_account_not_supported_by_method(),
JmapError::custom("customErrorType"),
];
for err in &errors {
let v = serde_json::to_value(err);
assert!(v.is_ok(), "JmapError variant failed to serialize: {err:?}");
}
}
#[test]
fn from_jmap_error_matches_request_error() {
let via_from: RequestError = JmapError::invalid_arguments("x").into();
let via_fn = request_error(JmapError::invalid_arguments("x"));
assert_eq!(
via_from.into_response().status(),
via_fn.into_response().status()
);
}
}