Skip to main content

crossref_client/
error.rs

1use crate::limit::RateLimit;
2use crate::query::ResourceComponent;
3use crate::response::{Failures, MessageType};
4use std::result;
5use thiserror::Error as ThisError;
6
7/// A type alias for handling errors throughout crossref.
8pub type Result<T> = result::Result<T, Error>;
9
10/// All different error types this crate uses.
11#[derive(Debug, ThisError)]
12#[non_exhaustive]
13pub enum Error {
14    /// if an invalid type was requested
15    #[error("invalid type name: {name}")]
16    InvalidTypeName {
17        /// the type name that was invalid
18        name: String,
19    },
20
21    /// if there is a mismatch between the expected return type of the crossref api and this rust client
22    #[error("expected response item of type {expected} but got {got}")]
23    UnexpectedItem {
24        /// the message type this client expected
25        expected: MessageType,
26        /// the message type crossref actually returned
27        got: MessageType,
28    },
29
30    /// a filter value crossref has no way to read back
31    ///
32    /// Crossref splits the `filter` value on `,` after percent-decoding it, so
33    /// a value carrying one arrives as two filters -- `container-title:A, B`
34    /// becomes `container-title:A` plus a filter called ` B`. There is no form
35    /// that survives the split, so the request is refused rather than sent to
36    /// be rejected with a `400`.
37    #[error(
38        "`{value}` cannot be sent as a `{filter}` filter: crossref reads the `,` in it as the start of another filter"
39    )]
40    UnsendableFilterValue {
41        /// the filter whose value cannot be sent, e.g. `container-title`
42        filter: String,
43        /// the value that carries the `,`
44        value: String,
45    },
46
47    /// a config error
48    #[error("{msg}")]
49    Config {
50        /// the notification
51        msg: String,
52    },
53
54    /// an error that occurred while operating with [reqwest]
55    #[error(transparent)]
56    ReqWest {
57        /// the underlying transport error
58        #[from]
59        reqwest: reqwest::Error,
60    },
61
62    /// crossref refused the request and said why
63    ///
64    /// Answered with a `400` and a `validation-failure` body, which is what an
65    /// unknown filter, sort field or field query produces.
66    #[error("crossref rejected the request: {failures}")]
67    ValidationFailure {
68        /// what crossref objected to
69        failures: Failures,
70    },
71
72    /// crossref kept answering `429` until the retry budget ran out
73    ///
74    /// The client paces itself against the limit crossref reports, so this
75    /// normally means requests are also being made outside it -- from another
76    /// process, or another [`Crossref`](crate::Crossref) built separately
77    /// rather than cloned.
78    #[error("crossref rate limited the request; gave up after {attempts} attempts")]
79    RateLimited {
80        /// how many times the request was sent
81        attempts: u32,
82        /// the budget crossref last reported
83        limit: RateLimit,
84    },
85
86    /// When crossref could not find anything
87    #[error("Nothing was found for resource `{resource}`")]
88    ResourceNotFound {
89        /// the resource that could not be resolved
90        resource: Box<ResourceComponent>,
91    },
92
93    /// if a error in serde occurred
94    #[error("invalid serde: {error}")]
95    Serde {
96        /// the underlying (de)serialization error
97        #[from]
98        error: serde_json::Error,
99    },
100}