Document simplest error example

This commit is contained in:
Martin Algesten
2020-12-20 11:12:41 +01:00
parent 234eb5572f
commit 1f0d84279a
2 changed files with 42 additions and 5 deletions

View File

@@ -19,18 +19,37 @@ use crate::Response;
/// message. Or you may want to handle certain error code responses /// message. Or you may want to handle certain error code responses
/// differently. /// differently.
/// ///
/// # Examples
///
/// Example of matching out all unexpected server status codes.
///
/// ```no_run
/// use ureq::Error;
///
/// match ureq::get("http://mypage.example.com/").call() {
/// Ok(response) => { /* it worked */},
/// Err(Error::Status(code, response)) => {
/// /* the server returned an unexpected status
/// code (such as 400, 500 etc) */
/// }
/// Err(_) => { /* some kind of io/transport error */ }
/// }
/// ```
///
/// An example of a function that handles HTTP 429 and 500 errors differently
/// than other errors. They get retried after a suitable delay, up to 4 times.
///
/// ``` /// ```
/// use std::{result::Result, time::Duration, thread}; /// use std::{result::Result, time::Duration, thread};
/// use ureq::{Response, Error, Error::Status}; /// use ureq::{Response, Error, Error::Status};
/// # fn main(){ ureq::is_test(true); get_response( "http://httpbin.org/status/500" ); } /// # fn main(){ ureq::is_test(true); get_response( "http://httpbin.org/status/500" ); }
/// ///
/// // An example of a function that handles HTTP 429 and 500 errors differently
/// // than other errors. They get retried after a suitable delay, up to 4 times.
/// fn get_response(url: &str) -> Result<Response, Error> { /// fn get_response(url: &str) -> Result<Response, Error> {
/// for _ in 1..4 { /// for _ in 1..4 {
/// match ureq::get(url).call() { /// match ureq::get(url).call() {
/// Err(Status(503, r)) | Err(Status(429, r)) => { /// Err(Status(503, r)) | Err(Status(429, r)) => {
/// let retry: Option<u64> = r.header("retry-after").and_then(|h| h.parse().ok()); /// let retry: Option<u64> = r.header("retry-after")
/// .and_then(|h| h.parse().ok());
/// let retry = retry.unwrap_or(5); /// let retry = retry.unwrap_or(5);
/// eprintln!("{} for {}, retry in {}", r.status(), r.get_url(), retry); /// eprintln!("{} for {}, retry in {}", r.status(), r.get_url(), retry);
/// thread::sleep(Duration::from_secs(retry)); /// thread::sleep(Duration::from_secs(retry));
@@ -52,7 +71,7 @@ use crate::Response;
/// # ureq::is_test(true); /// # ureq::is_test(true);
/// let resp = ureq::get("http://example.com/") /// let resp = ureq::get("http://example.com/")
/// .call() /// .call()
/// .or_else(|e| match e { /// .or_else(|e| match e {
/// Status(_, r) => Ok(r), // turn status errors into Ok Responses. /// Status(_, r) => Ok(r), // turn status errors into Ok Responses.
/// _ => Err(e), /// _ => Err(e),
/// })?; /// })?;

View File

@@ -86,7 +86,25 @@
//! //!
//! ureq returns errors via `Result<T, ureq::Error>`. That includes I/O errors, //! ureq returns errors via `Result<T, ureq::Error>`. That includes I/O errors,
//! protocol errors, and status code errors (when the server responded 4xx or //! protocol errors, and status code errors (when the server responded 4xx or
//! 5xx). More details on the [Error] type. //! 5xx).
//!
//! ```rust
//! use ureq::Error;
//!
//! # fn req() {
//! match ureq::get("http://mypage.example.com/").call() {
//! Ok(response) => { /* it worked */},
//! Err(Error::Status(code, response)) => {
//! /* the server returned an unexpected status
//! code (such as 400, 500 etc) */
//! }
//! Err(_) => { /* some kind of io/transport error */ }
//! }
//! # }
//! # fn main() {}
//! ```
//!
//! More details on the [Error] type.
//! //!
//! ## Features //! ## Features
//! //!