wallabag_api/
errors.rs

1// Copyright 2018 Samuel Walladge <samuel@swalladge.net>
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Client error types.
5
6use std::error::Error;
7use std::fmt;
8
9use serde::Deserialize;
10use serde_urlencoded;
11use surf::http::StatusCode;
12use surf::{self, http::url};
13
14pub type ClientResult<T> = std::result::Result<T, ClientError>;
15
16/// Type for the JSON error data returned on error from the HTTP API
17/// The API seems a bit unreliable on the format of errors returned...
18#[derive(Deserialize, Debug)]
19pub struct ResponseError {
20    pub error: String,
21    pub error_description: String,
22}
23
24/// Type for the JSON error data returned on forbidden error from HTTP API
25#[derive(Deserialize, Debug)]
26pub struct ResponseCodeMessageError {
27    pub error: CodeMessage,
28}
29
30#[derive(Deserialize, Debug)]
31pub struct CodeMessage {
32    pub code: u32,
33    pub message: String,
34}
35
36/// Represents all error possibilities that could be returned by the client.
37#[derive(Debug)]
38pub enum ClientError {
39    SurfError(surf::Error),
40    SerdeJsonError(serde_json::error::Error),
41    Unauthorized(ResponseError),
42    Forbidden(ResponseCodeMessageError),
43    ExpiredToken,
44    IOError(std::io::Error),
45    UrlParseError(url::ParseError),
46    UrlEncodeError(serde_urlencoded::ser::Error),
47    UnexpectedJsonStructure, // eg returned valid json but didn't fit model
48    NotFound(ResponseCodeMessageError), // 404
49    NotModified,             // 304
50    Other(StatusCode, String), // ¯\_(ツ)_/¯
51}
52
53impl fmt::Display for ClientError {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        use self::ClientError::*;
56        write!(
57            f,
58            "{}",
59            match self {
60                SerdeJsonError(e) => format!("Error deserializing json: {}", e),
61                e => format!("{:?}", e),
62            }
63        )
64    }
65}
66
67impl Error for ClientError {}
68
69// TODO: extract surf errors and turn them into more useful ClientErrors
70// TODO: maybe impl Error::cause to get the underlying surf or serde errors?
71
72impl From<serde_json::error::Error> for ClientError {
73    fn from(err: serde_json::error::Error) -> Self {
74        ClientError::SerdeJsonError(err)
75    }
76}
77
78impl From<std::io::Error> for ClientError {
79    fn from(err: std::io::Error) -> Self {
80        ClientError::IOError(err)
81    }
82}
83
84impl From<url::ParseError> for ClientError {
85    fn from(err: url::ParseError) -> Self {
86        ClientError::UrlParseError(err)
87    }
88}
89
90impl From<surf::Error> for ClientError {
91    fn from(err: surf::Error) -> Self {
92        ClientError::SurfError(err)
93    }
94}
95
96impl From<serde_urlencoded::ser::Error> for ClientError {
97    fn from(err: serde_urlencoded::ser::Error) -> Self {
98        ClientError::UrlEncodeError(err)
99    }
100}
101
102/// Represents possible errors building a `TagString`.
103#[derive(Debug)]
104pub enum TagStringError {
105    ContainsComma,
106}
107
108impl fmt::Display for TagStringError {
109    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110        write!(
111            f,
112            "{}",
113            match self {
114                TagStringError::ContainsComma => "Contains comma (invalid character)",
115            }
116        )
117    }
118}
119
120impl Error for TagStringError {}