Expose 'Context::{push_error,push_errors}'.

Closes #1582.

Co-authored-by: Francois Stephany <francois@tamere.eu>
This commit is contained in:
Sergio Benitez 2021-05-23 18:18:24 -07:00
parent 8a9000a9cb
commit 9e9c708a16
2 changed files with 52 additions and 11 deletions

View File

@ -299,19 +299,57 @@ impl<'v> Context<'v> {
self.status self.status
} }
pub(crate) fn push_error(&mut self, e: Error<'v>) { /// Inject a single error `error` into the context.
self.status = std::cmp::max(self.status, e.status()); ///
match e.name { /// # Example
///
/// ```rust
/// # use rocket::post;
/// # type T = String;
/// use rocket::http::Status;
/// use rocket::form::{Form, Contextual, Error};
///
/// #[post("/submit", data = "<form>")]
/// fn submit(mut form: Form<Contextual<'_, T>>) {
/// let error = Error::validation("a good error message")
/// .with_name("field_name")
/// .with_value("some field value");
///
/// form.context.push_error(error);
/// }
/// ```
pub fn push_error(&mut self, error: Error<'v>) {
self.status = std::cmp::max(self.status, error.status());
match error.name {
Some(ref name) => match self.errors.get_mut(name) { Some(ref name) => match self.errors.get_mut(name) {
Some(errors) => errors.push(e), Some(errors) => errors.push(error),
None => { self.errors.insert(name.clone(), e.into()); }, None => { self.errors.insert(name.clone(), error.into()); },
} }
None => self.form_errors.push(e) None => self.form_errors.push(error)
} }
} }
pub(crate) fn push_errors(&mut self, errors: Errors<'v>) { /// Inject all of the errors in `errors` into the context.
errors.into_iter().for_each(|e| self.push_error(e)) ///
/// # Example
///
/// ```rust
/// # use rocket::post;
/// # type T = String;
/// use rocket::http::Status;
/// use rocket::form::{Form, Contextual, Error};
///
/// #[post("/submit", data = "<form>")]
/// fn submit(mut form: Form<Contextual<'_, T>>) {
/// let error = Error::validation("a good error message")
/// .with_name("field_name")
/// .with_value("some field value");
///
/// form.context.push_errors(vec![error]);
/// }
/// ```
pub fn push_errors<E: Into<Errors<'v>>>(&mut self, errors: E) {
errors.into().into_iter().for_each(|e| self.push_error(e))
} }
} }

View File

@ -74,19 +74,22 @@ pub struct Errors<'v>(Vec<Error<'v>>);
/// ///
/// # Contructing /// # Contructing
/// ///
/// An `Error` can be constructed from anything that an [`ErrorKind`] can be /// An `Error` can be constructed via [`Error::validation()`],
/// constructed from. See [`ErrorKind`](ErrorKind#constructing). /// [`Error::custom()`], or anything that an [`ErrorKind`] can be constructed
/// from. See [`ErrorKind`](ErrorKind#constructing).
/// ///
/// ```rust /// ```rust
/// use rocket::form::Error; /// use rocket::form::Error;
/// ///
/// fn at_most_10() -> Result<usize, Error<'static>> { /// fn at_most_10_not_even() -> Result<usize, Error<'static>> {
/// // Using `From<PartIntError> => ErrorKind::Int`. /// // Using `From<PartIntError> => ErrorKind::Int`.
/// let i: usize = "foo".parse()?; /// let i: usize = "foo".parse()?;
/// ///
/// if i > 10 { /// if i > 10 {
/// // `From<(Option<isize>, Option<isize>)> => ErrorKind::OutOfRange` /// // `From<(Option<isize>, Option<isize>)> => ErrorKind::OutOfRange`
/// return Err((None, Some(10isize)).into()); /// return Err((None, Some(10isize)).into());
/// } else if i % 2 == 0 {
/// return Err(Error::validation("integer cannot be even"));
/// } /// }
/// ///
/// Ok(i) /// Ok(i)