(*--------------------------------------------------------------------------- Copyright (c) 2025 Anil Madhavapeddy. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) module Request_error = struct type urn = [ | `Unknown_capability | `Not_json | `Not_request | `Limit | `Other of string ] let urn_to_string = function | `Unknown_capability -> "urn:ietf:params:jmap:error:unknownCapability" | `Not_json -> "urn:ietf:params:jmap:error:notJSON" | `Not_request -> "urn:ietf:params:jmap:error:notRequest" | `Limit -> "urn:ietf:params:jmap:error:limit" | `Other s -> s let urn_of_string = function | "urn:ietf:params:jmap:error:unknownCapability" -> `Unknown_capability | "urn:ietf:params:jmap:error:notJSON" -> `Not_json | "urn:ietf:params:jmap:error:notRequest" -> `Not_request | "urn:ietf:params:jmap:error:limit" -> `Limit | s -> `Other s let urn_jsont = let kind = "Request error URN" in Jsont.map ~kind ~dec:(fun s -> urn_of_string s) ~enc:urn_to_string Jsont.string type t = { type_ : urn; status : int; title : string option; detail : string option; limit : string option; } let make type_ status title detail limit = { type_; status; title; detail; limit } let type_ t = t.type_ let status t = t.status let title t = t.title let detail t = t.detail let limit t = t.limit let jsont = let kind = "Request error" in Jsont.Object.map ~kind make |> Jsont.Object.mem "type" urn_jsont ~enc:type_ |> Jsont.Object.mem "status" Jsont.int ~enc:status |> Jsont.Object.opt_mem "title" Jsont.string ~enc:title |> Jsont.Object.opt_mem "detail" Jsont.string ~enc:detail |> Jsont.Object.opt_mem "limit" Jsont.string ~enc:limit |> Jsont.Object.finish end type method_error_type = [ | `Server_unavailable | `Server_fail | `Server_partial_fail | `Unknown_method | `Invalid_arguments | `Invalid_result_reference | `Forbidden | `Account_not_found | `Account_not_supported_by_method | `Account_read_only | `Other of string ] let method_error_type_to_string = function | `Server_unavailable -> "serverUnavailable" | `Server_fail -> "serverFail" | `Server_partial_fail -> "serverPartialFail" | `Unknown_method -> "unknownMethod" | `Invalid_arguments -> "invalidArguments" | `Invalid_result_reference -> "invalidResultReference" | `Forbidden -> "forbidden" | `Account_not_found -> "accountNotFound" | `Account_not_supported_by_method -> "accountNotSupportedByMethod" | `Account_read_only -> "accountReadOnly" | `Other s -> s let method_error_type_of_string = function | "serverUnavailable" -> `Server_unavailable | "serverFail" -> `Server_fail | "serverPartialFail" -> `Server_partial_fail | "unknownMethod" -> `Unknown_method | "invalidArguments" -> `Invalid_arguments | "invalidResultReference" -> `Invalid_result_reference | "forbidden" -> `Forbidden | "accountNotFound" -> `Account_not_found | "accountNotSupportedByMethod" -> `Account_not_supported_by_method | "accountReadOnly" -> `Account_read_only | s -> `Other s let method_error_type_jsont = let kind = "Method error type" in Jsont.map ~kind ~dec:(fun s -> method_error_type_of_string s) ~enc:method_error_type_to_string Jsont.string type method_error = { type_ : method_error_type; description : string option; } let method_error_make type_ description = { type_; description } let method_error_type_ t = t.type_ let method_error_description t = t.description let method_error_jsont = let kind = "Method error" in Jsont.Object.map ~kind method_error_make |> Jsont.Object.mem "type" method_error_type_jsont ~enc:method_error_type_ |> Jsont.Object.opt_mem "description" Jsont.string ~enc:method_error_description |> Jsont.Object.finish type set_error_type = [ | `Forbidden | `Over_quota | `Too_large | `Rate_limit | `Not_found | `Invalid_patch | `Will_destroy | `Invalid_properties | `Singleton | `Forbidden_mail_from | `Forbidden_from | `Forbidden_to_send | `Other of string ] let set_error_type_to_string = function | `Forbidden -> "forbidden" | `Over_quota -> "overQuota" | `Too_large -> "tooLarge" | `Rate_limit -> "rateLimit" | `Not_found -> "notFound" | `Invalid_patch -> "invalidPatch" | `Will_destroy -> "willDestroy" | `Invalid_properties -> "invalidProperties" | `Singleton -> "singleton" | `Forbidden_mail_from -> "forbiddenMailFrom" | `Forbidden_from -> "forbiddenFrom" | `Forbidden_to_send -> "forbiddenToSend" | `Other s -> s let set_error_type_of_string = function | "forbidden" -> `Forbidden | "overQuota" -> `Over_quota | "tooLarge" -> `Too_large | "rateLimit" -> `Rate_limit | "notFound" -> `Not_found | "invalidPatch" -> `Invalid_patch | "willDestroy" -> `Will_destroy | "invalidProperties" -> `Invalid_properties | "singleton" -> `Singleton | "forbiddenMailFrom" -> `Forbidden_mail_from | "forbiddenFrom" -> `Forbidden_from | "forbiddenToSend" -> `Forbidden_to_send | s -> `Other s let set_error_type_jsont = let kind = "SetError type" in Jsont.map ~kind ~dec:(fun s -> set_error_type_of_string s) ~enc:set_error_type_to_string Jsont.string type set_error = { type_ : set_error_type; description : string option; properties : string list option; } let set_error ?description ?properties type_ = { type_; description; properties } let set_error_make type_ description properties = { type_; description; properties } let set_error_type_ t = t.type_ let set_error_description t = t.description let set_error_properties t = t.properties let set_error_jsont = let kind = "SetError" in Jsont.Object.map ~kind set_error_make |> Jsont.Object.mem "type" set_error_type_jsont ~enc:set_error_type_ |> Jsont.Object.opt_mem "description" Jsont.string ~enc:set_error_description |> Jsont.Object.opt_mem "properties" (Jsont.list Jsont.string) ~enc:set_error_properties |> Jsont.Object.finish