1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Error definitions and utilities for engine execution.
use crate::{
    error::{DepthError, InternalRsonpathError},
    input::error::InputError,
};
use thiserror::Error;

/// Error enum for all types of errors that can be reported
/// during engine execution.
///
/// **NOTE**: Most errors are _not_ guaranteed to be raised for every
/// JSON document that is malformed in the respective manner.
/// The engine may ignore such errors and simply produce incorrect results
/// for invalid documents.
#[derive(Debug, Error)]
pub enum EngineError {
    /// Error while reading from the supplied [`Input`](crate::input::Input) implementation.
    #[error(transparent)]
    InputError(#[from] InputError),
    /// Error while writing to the supplied [`Sink`](crate::result::Sink) implementation.
    #[error("Error writing a result to sink: '{0}'")]
    SinkError(#[source] Box<dyn std::error::Error + Send + Sync>),
    /// Document depth fell below zero, which can only happen
    /// if there are more closing than opening braces.
    /// The inner [`usize`] value indicates the position of the mismatched closing character.
    #[error("Mismatched closing character in the input JSON at position {0}.")]
    DepthBelowZero(usize, #[source] DepthError),
    /// The depth limit was reached -- the document is too nested.
    /// The inner [`usize`] value indicates the position of the opening character
    /// which caused the overflow.
    #[error("Opening character at position {0} caused depth overflow.")]
    DepthAboveLimit(usize, #[source] DepthError),
    /// The engine reached end of the document while depth was positive.
    /// This means that some of the opening characters do not have matching
    /// closing characters.
    #[error("Malformed input JSON; end of input was reached, but unmatched opening characters remained.")]
    MissingClosingCharacter(),
    /// The engine reached a structural character that should occur only in an object or list,
    /// but there was no preceding opening character.
    #[error("Malformed input JSON; structural characters present, but no opening character read.")]
    MissingOpeningCharacter(),
    /// The engine found a query match, but no value associated with it.
    #[error("Malformed input JSON; a query match was found, but there was no associated value")]
    MissingItem(),
    /// An error occurred when trying to parse a member name terminated by a particular colon character.
    /// The inner [`usize`] value should be set to the byte index of the colon.
    #[error(
        "Malformed member name in the input JSON; \
        the colon at position {0} must be preceded by a string, but \
        there are no matching double quote characters."
    )]
    MalformedStringQuotes(usize),
    /// Engine error that occurred due to a known limitation.
    #[error(transparent)]
    NotSupported(#[from] crate::error::UnsupportedFeatureError),
    /// Irrecoverable error due to a broken invariant or assumption.
    /// The engine returns these instead of panicking.
    #[error("EngineError: {0}")]
    InternalError(
        #[source]
        #[from]
        InternalRsonpathError,
    ),
}