Skip to main content

rust_wistia/models/
error.rs

1//! Library-specific errors, including types and implementations.
2//!
3use hyper::http::uri::InvalidUri;
4
5use std::io;
6
7use serde::{Deserialize, Serialize};
8use thiserror::Error;
9
10/// Base error types raised by this library.
11///
12/// # Note
13/// The `rust-wistia` crate should *only* ever raise the error kinds as
14/// defined under here.
15///
16#[derive(Debug, Error)]
17#[non_exhaustive]
18pub enum RustWistiaError {
19    #[error("no such file: the source file path `{0}` should exist")]
20    FileNotFound(String),
21    #[error("environment variable `{name}` must be set")]
22    EnvVarNotFound { name: String },
23    /// An invalid media asset type is specified
24    #[error("{video_id}: no such asset ({r#type}) for video.\n  Valid Assets: {valid_types:?}")]
25    AssetNotFound {
26        r#type: String,
27        video_id: String,
28        valid_types: Vec<String>,
29    },
30    /// A `media_id` or `media` argument is not specified
31    #[error("An argument for `media_id` or `media` is required.")]
32    MediaIsRequired,
33    /// `RequestError` is raised when the Wistia API responds back with a
34    /// *non-* "OK" response.
35    ///
36    /// More specifically, this error is raised when the status code of
37    /// a response is between 400 and 600, which indicates its either a client
38    /// error or a server error.
39    ///
40    /// # Note
41    ///
42    /// The `error` and `message` fields are mutually-exclusive; if we cannot
43    /// de-serialize `error`, the `message` will be populated instead with the
44    /// response data.
45    ///
46    #[error(
47        "invalid request\n  status: {status_code:?})\n  reason: {reason}\n  error: {error:#?}"
48    )]
49    Request {
50        status_code: u16,
51        reason: String,
52        error: WistiaError,
53    },
54    #[error("unknown rust-wistia error")]
55    Unknown,
56    // Transparent (pass-through) errors
57    #[error(transparent)]
58    Io(#[from] io::Error),
59    #[error(transparent)]
60    Hyper(#[from] hyper::Error),
61    #[error(transparent)]
62    Http(#[from] hyper::http::Error),
63    #[error(transparent)]
64    InvalidUri(#[from] InvalidUri),
65    #[error(transparent)]
66    SerdeJson(#[from] serde_json::Error),
67    #[error(transparent)]
68    SerdeUrlEncodedSer(#[from] serde_urlencoded::ser::Error),
69}
70
71/// An error returned from the Wistia API, along with a custom error
72/// code from the Wistia side.
73///
74#[derive(Debug, Default, Deserialize, Serialize)]
75pub struct WistiaError {
76    #[serde(default)]
77    #[serde(rename = "error")]
78    pub message: String,
79    pub code: Option<String>,
80    pub detail: Option<String>,
81}