Skip to main content

FormError

Enum FormError 

Source
#[non_exhaustive]
pub enum FormError { Limit { limit: Limit, at: Location, }, Syntax { reason: SyntaxError, at: Location, }, Encoding { reason: EncodingError, at: Location, }, Policy { reason: PolicyRefusal, at: Location, }, Refused { refusal: Refusal, at: Location, }, Extract { reason: ExtractError, field: Option<&'static str>, }, UnsupportedMediaType, InvalidLimits { limit: Limit, }, BufferTooSmall { needed: usize, }, Io { kind: ErrorKind, }, Random, Poisoned, }
Expand description

Everything that can go wrong in form-net.

Input errors (Limit, Syntax, Encoding, Policy) carry a Location; caller mistakes (InvalidLimits, BufferTooSmall, Poisoned) and environment failures (Io, Random) do not. status_hint maps every error to an HTTP status and code to a stable name.

§Examples

use form_net::{FormError, Limit};

let err = form_net::parse_urlencoded(&vec![b'a'; 11 << 20]).unwrap_err();
assert!(matches!(err, FormError::Limit { limit: Limit::Body, .. }));
assert_eq!((err.status_hint(), err.code()), (413, "limit.body"));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Limit

A budget of Limits was exceeded.

Fields

§limit: Limit

Which limit.

§at: Location

Where.

§

Syntax

The input breaks the grammar.

Fields

§reason: SyntaxError

What was wrong.

§at: Location

Where.

§

Encoding

Bytes that do not decode: percent escapes, UTF-8, charsets.

Fields

§reason: EncodingError

What was wrong.

§at: Location

Where.

§

Policy

The input is well formed but a Policy refuses it.

Fields

§reason: PolicyRefusal

Which rule.

§at: Location

Where.

§

Refused

An inspector refused the input.

Fields

§refusal: Refusal

The inspector’s status and code.

§at: Location

Where.

§

Extract

Typed extraction failed.

Fields

§reason: ExtractError

What was wrong.

§field: Option<&'static str>

The Rust-side field name, when there is one.

§

UnsupportedMediaType

The content type is not one form-net parses.

§

InvalidLimits

Limits::validate refused the configuration.

Fields

§limit: Limit

The field it refused.

§

BufferTooSmall

A buffer the caller provided is too short.

Fields

§needed: usize

The length it must have.

§

Io

Available on crate feature std only.

The reader, the writer, or the file system failed.

Fields

§kind: ErrorKind

The kind of failure; never a path or an OS message.

§

Random

Available on crate feature std only.

The operating system’s random source failed.

§

Poisoned

A parser was called again after an error, without reset.

Implementations§

Source§

impl FormError

Source

pub const fn status_hint(&self) -> u16

The HTTP status a server should answer with.

413 for every limit, 400 for syntax and most encoding and policy errors, 415 for unsupported media types, charsets, transfer encodings, and nested multipart, 422 for extraction errors, 503 when the shared spill budget is full, 500 for caller mistakes and environment failures, and the inspector’s own status for a refusal (400 if that status is outside 400 to 599).

§Examples
use form_net::FormError;

assert_eq!(FormError::UnsupportedMediaType.status_hint(), 415);
assert_eq!(FormError::Poisoned.status_hint(), 500);
Source

pub const fn code(&self) -> &'static str

A stable, machine-readable name for the error: the variant and the reason in snake case, joined with a dot (limit.parts, syntax.boundary_in_content). A refusal returns the inspector’s own Refusal::code. Codes never change meaning after 1.0.

§Examples
use form_net::FormError;

assert_eq!(FormError::UnsupportedMediaType.code(), "unsupported_media_type");
let err = form_net::parse_urlencoded(b"a=%zz").unwrap_err();
assert_eq!(err.code(), "encoding.invalid_percent");
Source

pub const fn location(&self) -> Option<Location>

Where the error was found, for input errors and refusals.

§Examples
let err = form_net::parse_urlencoded(b"ok=1&bad=%zz").unwrap_err();
assert_eq!(err.location().map(|l| l.offset), Some(9));
assert_eq!(form_net::FormError::Poisoned.location(), None);

Trait Implementations§

Source§

impl Clone for FormError

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for FormError

Source§

impl Debug for FormError

Source§

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

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

impl Display for FormError

Source§

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

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

impl Eq for FormError

Source§

impl Error for FormError

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

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
Source§

impl PartialEq for FormError

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for FormError

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.