Skip to main content

evalhub_schema/
error.rs

1//! The error envelope and the closed set of error codes.
2//!
3//! Every rejection the hub makes has the same shape:
4//!
5//! ```text
6//! 422 { "errors": [ { "path": "results[0].metric", "code": "metric_id_invalid", "hint": "..." }, ... ] }
7//! ```
8//!
9//! Validation collects *all* errors before returning, so a producer fixes a
10//! record in one round trip rather than one error at a time. `path` is a
11//! JSON-pointer-like location in the submitted record; `code` is one of the
12//! variants below and is listed as an enum in the OpenAPI document; `hint`
13//! is prose for a human and is not part of the contract.
14//!
15//! | Code                     | Status | Meaning                                                              |
16//! | ------------------------ | ------ | -------------------------------------------------------------------- |
17//! | `schema`                 | 422    | JSON Schema violation (unknown key, wrong type, missing required)    |
18//! | `number_too_large`       | 422    | integer outside ±2^53                                                |
19//! | `counts_missing`         | 422    | `results` non-empty but no `counts`                                  |
20//! | `counts_inconsistent`    | 422    | `attempted < completed + failed + skipped + errored`                 |
21//! | `attachment_ref_unknown` | 422    | `samples_ref` / `calls` / `artifacts[]` / `error.log` not in `attachments[].path` |
22//! | `attachment_path_invalid`| 422    | duplicate, `..`, or absolute `attachments[].path`                    |
23//! | `metric_id_invalid`      | 422    | `results[].metric`, `run_results[].metric` or a run's `metrics` key not of the form `{ns}/{name}` |
24//! | `type_mismatch`          | 422    | query: operator applied to a path of another type                    |
25//! | `not_indexed`            | 422    | query: operator needs an index the path does not have                |
26//! | `unknown_path`           | 422    | query: path not in the schema                                        |
27//! | `runs_moved`             | 422    | an `evalhub.eval/2.0` header carries `runs`; runs are written apart  |
28//! | `run_status_detail`      | 422    | `error` present iff `status` is `error`, `skip_reason` iff `skipped` |
29//! | `run_id_invalid`         | 422    | `run_id` empty, containing `/`, or over 200 bytes                    |
30//! | `run_id_mismatch`        | 422    | the `run_id` in the body differs from the one in the path            |
31//! | `run_result_value_or_label` | 422 | a `run_results[]` element has neither `value` nor `label`            |
32//! | `run_results_eval_unknown`  | 422 | `run_results[].eval` is not the record of any `core/uses_eval`       |
33//! | `run_unknown`            | 422    | a `run_id` in the used set has no row in that Eval                   |
34//! | `run_not_in_used_set`    | 422    | `run_results[].run_id` outside the Card's used set for its `eval`    |
35//! | `too_many_run_results`   | 422    | more `run_results[]` than the configured limit                       |
36//! | `batch_duplicate_run_id` | 422    | the same `run_id` twice in one batch                                 |
37//! | `attachment_missing`     | 409    | `attachments[].sha256` not uploaded and confirmed                    |
38//! | `label_in_use`           | 409    | `@{label}` already names another version                             |
39//! | `namespace_in_use`       | 409    | the organisation slug is already a user or an organisation           |
40//! | `registry_entry_exists`  | 409    | that registry address is taken; entries are immutable                |
41//! | `run_deleted`            | 409    | writing a `run_id` that was deleted; a deleted id is never reused    |
42//! | `batch_too_large`        | 413    | more runs in one batch than the configured count                     |
43//! | `body_too_large`         | 413    | request body over the configured size in bytes                       |
44//!
45//! A `413` carries this body only with the two `413` codes above. Other
46//! statuses carry no body of this shape: `404` for anything the caller
47//! may not know exists (including private records), `403` for a token
48//! without the scope, `401` for no or an invalid token, `400` for a body that
49//! is not JSON.
50//!
51//! Codes are added, never renamed or reused. A client switching on `code`
52//! must treat an unknown code as a generic 422.
53
54use schemars::JsonSchema;
55use serde::{Deserialize, Serialize};
56
57/// The closed set of error codes.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
59#[serde(rename_all = "snake_case")]
60pub enum ErrorCode {
61    /// JSON Schema violation: unknown key, wrong type, missing required key.
62    Schema,
63    /// An integer outside ±2^53.
64    NumberTooLarge,
65    /// `results` is non-empty but `counts` is absent.
66    CountsMissing,
67    /// `counts.attempted < completed + failed + skipped + errored`.
68    CountsInconsistent,
69    /// A `samples_ref`, `calls`, `artifacts[]` or `error.log` entry names no `attachments[].path`.
70    AttachmentRefUnknown,
71    /// An `attachments[].path` is duplicated, absolute, or contains `..`.
72    AttachmentPathInvalid,
73    /// A `results[].metric`, a `run_results[].metric` or a key of a run's
74    /// `metrics` is not of the form `{ns}/{name}`.
75    MetricIdInvalid,
76    /// Query: operator applied to a path of another type.
77    TypeMismatch,
78    /// Query: operator needs an index the path does not have.
79    NotIndexed,
80    /// Query: path not in the schema.
81    UnknownPath,
82    /// An `attachments[].sha256` has not been uploaded and confirmed.
83    AttachmentMissing,
84    /// The requested `@{label}` already names another version.
85    LabelInUse,
86    /// The requested organisation slug is already a user or an organisation.
87    NamespaceInUse,
88    /// A registry entry already exists at that address. Entries are
89    /// immutable, so a correction is a new version.
90    RegistryEntryExists,
91    /// An `evalhub.eval/2.0` Eval header carries `runs`. Runs are not part
92    /// of the header; they are written one by one or in a batch.
93    RunsMoved,
94    /// A run's `status` and its detail disagree: `error` must be present
95    /// exactly when `status` is `error`, `skip_reason` exactly when it is
96    /// `skipped`.
97    RunStatusDetail,
98    /// A `run_id` is empty, contains `/`, or is longer than 200 bytes.
99    RunIdInvalid,
100    /// The `run_id` in the body differs from the one in the request path.
101    RunIdMismatch,
102    /// A `run_results[]` element has neither `value` nor `label`.
103    RunResultValueOrLabel,
104    /// A `run_results[].eval` is not the record of any of the Card's
105    /// `core/uses_eval` relations.
106    RunResultsEvalUnknown,
107    /// A `run_id` in the Card's used set has no row in that Eval (or the
108    /// Eval is one the writer may not see).
109    RunUnknown,
110    /// A `run_results[].run_id` is outside the Card's used set for its `eval`.
111    RunNotInUsedSet,
112    /// A Card version carries more `run_results[]` than the hub's configured limit.
113    TooManyRunResults,
114    /// The `run_id` names a run that was deleted. A deleted run's id is not
115    /// reused, because Cards that judged it would then point at different content.
116    RunDeleted,
117    /// The same `run_id` appears more than once in one batch.
118    BatchDuplicateRunId,
119    /// A batch carries more runs than the hub's configured count.
120    BatchTooLarge,
121    /// The request body is larger than the hub's configured size in bytes.
122    BodyTooLarge,
123}
124
125impl ErrorCode {
126    /// The HTTP status this code is returned with.
127    pub const fn status(self) -> u16 {
128        match self {
129            ErrorCode::AttachmentMissing
130            | ErrorCode::LabelInUse
131            | ErrorCode::NamespaceInUse
132            | ErrorCode::RegistryEntryExists
133            | ErrorCode::RunDeleted => 409,
134            ErrorCode::BatchTooLarge | ErrorCode::BodyTooLarge => 413,
135            _ => 422,
136        }
137    }
138}
139
140/// One rejection.
141#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
142#[serde(deny_unknown_fields)]
143pub struct ErrorEntry {
144    /// Location in the submitted document, JSON-pointer-like (`results[0].metric`).
145    pub path: String,
146    /// The code.
147    pub code: ErrorCode,
148    /// Prose for a human. Not part of the contract.
149    #[serde(default, skip_serializing_if = "Option::is_none")]
150    pub hint: Option<String>,
151}
152
153/// The body of every `422` and `409` response.
154#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
155#[serde(deny_unknown_fields)]
156pub struct ErrorEnvelope {
157    /// Every rejection found, in document order.
158    pub errors: Vec<ErrorEntry>,
159}