Skip to main content

Error

Struct Error 

Source
pub struct Error<W: Warning> { /* private fields */ }
Expand description

The warning that caused an operation to fail.

The warning is stored apart from the source set so it is clear which warning has been escalated to an error.

Implementations§

Source§

impl<W: Warning> Error<W>

Source

pub fn warning(&self) -> &W

Return reference to the Warning.

Examples found in repository?
examples/price_cdr_with_known_version.rs (line 79)
75fn print_pricing_error(error: &warning::Error<price::Warning>) {
76    eprintln!(
77        "ERR: Unable to price CDR due to error at path `{}`: {}",
78        error.element().path,
79        error.warning()
80    );
81}
More examples
Hide additional examples
examples/price_cdr_with_unknown_version.rs (line 88)
84fn print_timezone_error(error: &warning::Error<timezone::Warning>) {
85    eprintln!(
86        "ERR: Unable to find timezone due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
91
92/// Print the `timezone::Warning`s to `stderr`.
93fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from the timezone search:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
104
105/// Print the `price::Warning` that halted the process to `stderr`.
106fn print_pricing_error(error: &warning::Error<price::Warning>) {
107    eprintln!(
108        "ERR: Unable to price CDR due to error at path `{}`: {}",
109        error.element().path,
110        error.warning()
111    );
112}
examples/generate_cdr_from_tariff.rs (line 39)
11fn main() {
12    const TARIFF_JSON: &str = include_str!("data/tariff_time_and_parking_time_separate.json");
13
14    // Parse the raw JSON into a `json::Document` and guess the OCPI version of the tariff, falling
15    // back to `v2.2.1` when the version is uncertain.
16    let doc = json::parse_object(TARIFF_JSON).unwrap();
17    let tariff = tariff::infer_version(doc).certain_or(Version::V221);
18    let version = tariff.version();
19
20    // Check the tariff against the OCPI tariff schema. When `tariff::build` rejects the tariff,
21    // a schema warning names the field it couldn't use.
22    let (tariff, schema_warnings) = tariff::check(tariff.into_doc(), version).into_parts();
23
24    if !schema_warnings.is_empty() {
25        eprintln!(
26            "WARN: {} warnings from checking the tariff:\n{:#?}",
27            schema_warnings.len_warnings(),
28            schema_warnings.path_msg_map()
29        );
30    }
31
32    let tariff = match tariff::build(tariff) {
33        Ok(tariff) => tariff.ignore_warnings(),
34        Err(failure) => {
35            let (error, warnings) = failure.into_parts();
36            eprintln!(
37                "ERR: Unable to build the tariff due to error at path `{}`: {}",
38                error.element().path,
39                error.warning()
40            );
41            eprintln!(
42                "WARN: {} warnings from building the tariff:\n{:#?}",
43                warnings.len_warnings(),
44                warnings.path_msg_map()
45            );
46            return;
47        }
48    };
49
50    let config = generate::Config {
51        timezone: jiff::tz::get!("Europe/Amsterdam"),
52        start_date_time: Timestamp::from_str("2025-06-12 18:22:33+00:00").unwrap(),
53        end_date_time: Timestamp::from_str("2025-06-12 22:33:44+00:00").unwrap(),
54        max_current_supply_amp: Decimal::from(4),
55        requested_kwh: Decimal::from(24),
56        max_power_supply_kw: Decimal::from(80),
57    };
58    let report = match cdr::generate_from_tariff(&tariff, &config) {
59        Ok(r) => r,
60        Err(failure) => {
61            let (error, warnings) = failure.into_parts();
62            print_error(&error);
63            print_warnings(&warnings);
64            return;
65        }
66    };
67    let (report, warnings) = report.into_parts();
68
69    print_warnings(&warnings);
70
71    let generate::Report {
72        tariff_id,
73        tariff_currency_code,
74        partial_cdr,
75    } = report;
76
77    println!(
78        "CDR generated for tariff with id: `{tariff_id}` and currency code: `{tariff_currency_code}`"
79    );
80    println!("{partial_cdr:#?}");
81}
82
83/// Print `generate::Warning` that halted the example to `stderr`.
84fn print_error(error: &warning::Error<generate::Warning>) {
85    eprintln!(
86        "ERR: Unable to generate CDR due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
Source

pub fn into_warning(self) -> W

Consume the Error and return the Warning.

Source

pub fn element(&self) -> &Element

Return a reference to the Element that caused the Warning.

Examples found in repository?
examples/price_cdr_with_known_version.rs (line 78)
75fn print_pricing_error(error: &warning::Error<price::Warning>) {
76    eprintln!(
77        "ERR: Unable to price CDR due to error at path `{}`: {}",
78        error.element().path,
79        error.warning()
80    );
81}
More examples
Hide additional examples
examples/price_cdr_with_unknown_version.rs (line 87)
84fn print_timezone_error(error: &warning::Error<timezone::Warning>) {
85    eprintln!(
86        "ERR: Unable to find timezone due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
91
92/// Print the `timezone::Warning`s to `stderr`.
93fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
94    if warnings.is_empty() {
95        return;
96    }
97
98    eprintln!(
99        "WARN: {} warnings from the timezone search:\n{:#?}",
100        warnings.len_warnings(),
101        warnings.path_msg_map()
102    );
103}
104
105/// Print the `price::Warning` that halted the process to `stderr`.
106fn print_pricing_error(error: &warning::Error<price::Warning>) {
107    eprintln!(
108        "ERR: Unable to price CDR due to error at path `{}`: {}",
109        error.element().path,
110        error.warning()
111    );
112}
examples/generate_cdr_from_tariff.rs (line 38)
11fn main() {
12    const TARIFF_JSON: &str = include_str!("data/tariff_time_and_parking_time_separate.json");
13
14    // Parse the raw JSON into a `json::Document` and guess the OCPI version of the tariff, falling
15    // back to `v2.2.1` when the version is uncertain.
16    let doc = json::parse_object(TARIFF_JSON).unwrap();
17    let tariff = tariff::infer_version(doc).certain_or(Version::V221);
18    let version = tariff.version();
19
20    // Check the tariff against the OCPI tariff schema. When `tariff::build` rejects the tariff,
21    // a schema warning names the field it couldn't use.
22    let (tariff, schema_warnings) = tariff::check(tariff.into_doc(), version).into_parts();
23
24    if !schema_warnings.is_empty() {
25        eprintln!(
26            "WARN: {} warnings from checking the tariff:\n{:#?}",
27            schema_warnings.len_warnings(),
28            schema_warnings.path_msg_map()
29        );
30    }
31
32    let tariff = match tariff::build(tariff) {
33        Ok(tariff) => tariff.ignore_warnings(),
34        Err(failure) => {
35            let (error, warnings) = failure.into_parts();
36            eprintln!(
37                "ERR: Unable to build the tariff due to error at path `{}`: {}",
38                error.element().path,
39                error.warning()
40            );
41            eprintln!(
42                "WARN: {} warnings from building the tariff:\n{:#?}",
43                warnings.len_warnings(),
44                warnings.path_msg_map()
45            );
46            return;
47        }
48    };
49
50    let config = generate::Config {
51        timezone: jiff::tz::get!("Europe/Amsterdam"),
52        start_date_time: Timestamp::from_str("2025-06-12 18:22:33+00:00").unwrap(),
53        end_date_time: Timestamp::from_str("2025-06-12 22:33:44+00:00").unwrap(),
54        max_current_supply_amp: Decimal::from(4),
55        requested_kwh: Decimal::from(24),
56        max_power_supply_kw: Decimal::from(80),
57    };
58    let report = match cdr::generate_from_tariff(&tariff, &config) {
59        Ok(r) => r,
60        Err(failure) => {
61            let (error, warnings) = failure.into_parts();
62            print_error(&error);
63            print_warnings(&warnings);
64            return;
65        }
66    };
67    let (report, warnings) = report.into_parts();
68
69    print_warnings(&warnings);
70
71    let generate::Report {
72        tariff_id,
73        tariff_currency_code,
74        partial_cdr,
75    } = report;
76
77    println!(
78        "CDR generated for tariff with id: `{tariff_id}` and currency code: `{tariff_currency_code}`"
79    );
80    println!("{partial_cdr:#?}");
81}
82
83/// Print `generate::Warning` that halted the example to `stderr`.
84fn print_error(error: &warning::Error<generate::Warning>) {
85    eprintln!(
86        "ERR: Unable to generate CDR due to error at path `{}`: {}",
87        error.element().path,
88        error.warning()
89    );
90}
Source

pub fn parts(&self) -> (&W, &Element)

Return the constituent parts.

Source

pub fn into_parts(self) -> (W, Element)

Consume the Cause and return the constituent parts.

Trait Implementations§

Source§

impl<W: Debug + Warning> Debug for Error<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W: Warning> Display for Error<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W: Warning> Error for Error<W>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Replaced by Error::source.
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

§

impl<W> Forget for Error<W>

§

impl<W> Freeze for Error<W>
where W: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl<W> Move for Error<W>

§

impl<W> RefUnwindSafe for Error<W>
where W: RefUnwindSafe,

§

impl<W> Send for Error<W>
where RawVecInner: Send,

§

impl<W> Sync for Error<W>
where RawVecInner: Sync,

§

impl<W> Unpin for Error<W>
where W: Unpin, *const u8 is !null: Unpin,

§

impl<W> UnsafeUnpin for Error<W>

§

impl<W> UnwindSafe for Error<W>
where W: UnwindSafe, RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more