Skip to main content

harmony_jolt/
error.rs

1use thiserror::Error as ThisError;
2use std::{fmt, result::Result as StdResult, num::ParseIntError};
3
4#[derive(Debug, ThisError)]
5pub enum Error {
6    #[error("Path index out of range when using wildcard. Index={idx};Length={len};")]
7    PathIndexOutOfRange { idx: usize, len: usize },
8    #[error("Match index out of range when using wildcard. Index={idx};Length={len};")]
9    MatchIndexOutOfRange { idx: usize, len: usize },
10    #[error("Unexpected end of right hand side expression.")]
11    UnexpectedEndOfRhs,
12    #[error("Unexpected right hand side expression.")]
13    UnexpectedRhsEntry,
14    #[error("Unexpected object in right hand side.")]
15    UnexpectedObjectInRhs,
16    #[error("Not implemented yet.")]
17    Todo,
18    /// Holds only the length of the index string. The string is a matched key or a value from
19    /// the request body, and callers log these errors.
20    #[error("Invalid index in expression. Length={len};\n{source}")]
21    InvalidIndex {
22        len: usize,
23        #[source]
24        source: ParseIntError,
25    },
26    #[error("Array index out of range. Index={idx};Length={len};")]
27    ArrIndexOutOfRange { idx: usize, len: usize },
28    #[error("Array index would pad the output with too many null slots. Index={idx};Length={len};Used={used};Limit={max} padded slots per shift;")]
29    ArrayPaddingLimit {
30        idx: usize,
31        len: usize,
32        used: usize,
33        max: usize,
34    },
35    #[error(
36        "Modifier run copied or wrote too much data. Used={used};Limit={max} estimated bytes per run;"
37    )]
38    ModifierBudgetLimit { used: usize, max: usize },
39    /// `used` is the total the run would have reached with the failed charge, as in
40    /// `ModifierBudgetLimit`. It is not what was spent before it.
41    #[error(
42        "Shift run copied too much data from the input. Used={used};Limit={max} estimated bytes per run;"
43    )]
44    ShiftBudgetLimit { used: usize, max: usize },
45    #[error("Array index string is too long. Length={len};Max={max};")]
46    IndexTooLong { len: usize, max: usize },
47    /// Holds only the kind of the value, such as "object" or "number (negative)". The value
48    /// comes from the request body, and callers log these errors.
49    #[error("Json value can't be used as an index: {kind}")]
50    InvalidIndexVal { kind: &'static str },
51    #[error("Key not found in object:{0}")]
52    KeyNotFound(MissingKey),
53    #[error("Expression didn't evaluate to a string.")]
54    EvalString,
55    #[error("Empty path while executing shift. THIS SHOULD NEVER HAPPEN.")]
56    ShiftEmptyPath,
57    #[error("Path is not empty after executing shift. THIS SHOULD NEVER HAPPEN.")]
58    ShiftPathNotEmpty,
59    #[error("Parse error: {0}")]
60    ParseError(String),
61    #[error("Invalid function: {0}")]
62    InvalidFunction(String),
63    #[error("Function argument error: {0}")]
64    FunctionArgumentError(String),
65    #[error("Path evaluation error: {0}")]
66    PathEvaluationError(String),
67}
68
69pub type Result<T> = StdResult<T, Error>;
70
71/// The key named in an [`Error::KeyNotFound`].
72///
73/// A key written in the spec is kept so spec mistakes are easy to find. A key built from the
74/// request body, through `&` or `@`, is reduced to its length. Callers log these errors, and the
75/// body may hold personal data of any size.
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub enum MissingKey {
78    Spec(String),
79    FromInput { len: usize },
80}
81
82impl fmt::Display for MissingKey {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match self {
85            MissingKey::Spec(key) => f.write_str(key),
86            MissingKey::FromInput { len } => write!(f, "<key from input, {len} bytes>"),
87        }
88    }
89}
90
91/// Name the JSON type of a value, for error messages that must not contain the value.
92pub(crate) fn json_kind(v: &serde_json::Value) -> &'static str {
93    use serde_json::Value;
94
95    match v {
96        Value::Null => "null",
97        Value::Bool(_) => "boolean",
98        Value::Number(_) => "number",
99        Value::String(_) => "string",
100        Value::Array(_) => "array",
101        Value::Object(_) => "object",
102    }
103}