Skip to main content

laravel_casts/
error.rs

1//! The error of a cast, typed or dynamic.
2
3/// A value that cannot be cast, or a cast that cannot be understood.
4///
5/// `laravel::Error` takes it with `?` as a `500`. When the value came from a request, match on it and answer a `422`
6/// for [`CastError::Rejected`] and [`CastError::Invalid`].
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8#[non_exhaustive]
9pub enum CastError {
10    /// The cast name is not one this port knows.
11    #[error("unsupported cast `{0}`")]
12    Unsupported(String),
13    /// The cast is known but its arguments or the value are not usable.
14    #[error("cast `{cast}`: {reason}")]
15    Invalid {
16        /// The cast, as written.
17        cast: String,
18        /// What is wrong.
19        reason: String,
20    },
21    /// A date pattern the cast was given cannot be used.
22    #[error(transparent)]
23    Pattern(#[from] php_dates::Error),
24    /// The cast hashes, and hashing is asynchronous: use the `_async` form.
25    #[error(
26        "cast `{0}` hashes on the blocking pool: use `set_async`, `apply_async` or `cast_array_async`"
27    )]
28    NeedsAsync(String),
29    /// The framework could not hash or encrypt the value, or decrypt it.
30    #[error("cast `{cast}`: {message}")]
31    Failed {
32        /// The cast, as written.
33        cast: String,
34        /// What the framework said.
35        message: String,
36    },
37    /// A value that came from outside (a request, JSON) and that the cast refuses, in the cast's own words.
38    /// What a [`crate::ColumnCast`] of your own answers, and what [`crate::canonical`] shows.
39    #[error("{0}")]
40    Rejected(String),
41}