crates/edge/api/src/code.rs 5.2 KiB 161 lines Raw
1 //! The refusal codes that a client sees, and their HTTP status.2 //!3 //! A refusal comes before admission (spec section 12). It always carries a code4 //! from this table, so a client can branch on the code and not on the message.5 6 use std::fmt;7 8 use inf3r_json::{Object, Value};9 10 /// The client's reason for refusing a request.11 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]12 pub enum Code {13     /// The name is not in the catalog.14     UnknownModel,15     /// The model does not declare the capability.16     UnknownCapability,17     /// The model declares the capability. The host does not serve it.18     CapabilityMissing,19     /// The kind is not permitted. The mime or the role is incorrect.20     InvalidPart,21     /// An inline part is more than 1 MiB.22     PayloadTooLarge,23     /// The queue is full.24     QueueFull,25     /// The ledger cannot reserve the bytes.26     NoRoom,27     /// The plugin failed to start or to load.28     LoadFailed,29     /// The plugin stopped during the job.30     PluginCrashed,31     /// The manifest does not agree with the hardware probe.32     Mismatch,33     /// The plugin did not obey the protocol.34     Protocol,35     /// The job exceeded the deadline.36     Timeout,37     /// Two plugins serve one name.38     AmbiguousModel,39 }40 41 impl Code {42     /// The word on the wire.43     pub fn as_str(self) -> &'static str {44         match self {45             Code::UnknownModel => "unknown_model",46             Code::UnknownCapability => "unknown_capability",47             Code::CapabilityMissing => "capability_missing",48             Code::InvalidPart => "invalid_part",49             Code::PayloadTooLarge => "payload_too_large",50             Code::QueueFull => "queue_full",51             Code::NoRoom => "no_room",52             Code::LoadFailed => "load_failed",53             Code::PluginCrashed => "plugin_crashed",54             Code::Mismatch => "mismatch",55             Code::Protocol => "protocol",56             Code::Timeout => "timeout",57             Code::AmbiguousModel => "ambiguous_model",58         }59     }60 61     /// The HTTP status of this refusal.62     pub fn status(self) -> u16 {63         match self {64             Code::UnknownModel => 404,65             Code::UnknownCapability | Code::CapabilityMissing | Code::InvalidPart => 400,66             Code::PayloadTooLarge => 413,67             Code::QueueFull => 429,68             Code::NoRoom | Code::LoadFailed => 503,69             Code::PluginCrashed => 502,70             Code::Timeout => 504,71             Code::Mismatch | Code::Protocol | Code::AmbiguousModel => 500,72         }73     }74 75     /// Whether the same request may work later.76     ///77     /// A refusal that the operator must fix is not retryable: a retry of an78     /// unknown model is another unknown model.79     pub fn retryable(self) -> bool {80         matches!(81             self,82             Code::QueueFull | Code::NoRoom | Code::LoadFailed | Code::PluginCrashed | Code::Timeout83         )84     }85 86     /// Whether the reply carries `Retry-After`.87     pub fn has_retry_after(self) -> bool {88         matches!(self, Code::QueueFull | Code::NoRoom)89     }90 91     /// The client code for a wire failure.92     ///93     /// A socket fault is not a client's fault, so it maps to `protocol` and94     /// keeps its detail. The wire codes that do have a client meaning keep it.95     pub fn from_wire(code: inf3r_proto::body::ErrorCode) -> Code {96         use inf3r_proto::body::ErrorCode as Wire;97         match code {98             Wire::UnknownModel => Code::UnknownModel,99             Wire::Capability => Code::UnknownCapability,100             Wire::Part => Code::InvalidPart,101             Wire::Size => Code::PayloadTooLarge,102             Wire::Busy => Code::QueueFull,103             Wire::Timeout => Code::Timeout,104             Wire::Dependency => Code::LoadFailed,105             Wire::Version | Wire::Frame | Wire::Stream | Wire::Credit | Wire::Internal => {106                 Code::Protocol107             }108             Wire::Cancelled => Code::Timeout,109         }110     }111 }112 113 impl fmt::Display for Code {114     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {115         f.write_str(self.as_str())116     }117 }118 119 /// A refusal, with the request that caused it.120 #[derive(Clone, PartialEq, Eq, Debug)]121 pub struct Refusal {122     /// The code.123     pub code: Code,124     /// One sentence for a human.125     pub message: String,126     /// The identifier that the client sent.127     pub request_id: String,128 }129 130 impl Refusal {131     /// A refusal with an empty message.132     pub fn new(code: Code, request_id: impl Into<String>) -> Refusal {133         Refusal {134             code,135             message: code.as_str().to_string(),136             request_id: request_id.into(),137         }138     }139 140     /// The same refusal with a message.141     pub fn with(self, message: impl Into<String>) -> Refusal {142         Refusal {143             message: message.into(),144             ..self145         }146     }147 148     /// The JSON body of rule 2 of section 12.149     pub fn to_json(&self) -> Value {150         Object::new().put("error", self.body()).build()151     }152 153     fn body(&self) -> Value {154         Object::new()155             .put("code", Value::str(self.code.as_str()))156             .put("message", Value::str(self.message.clone()))157             .put("retryable", Value::Bool(self.code.retryable()))158             .put("request_id", Value::str(self.request_id.clone()))159             .build()160     }161 }