neocities_client/error.rs
1//////// This file is part of the source code for neocities-client, a Rust ////////
2//////// library for interacting with the https://neocities.org/ API. ////////
3//////// ////////
4//////// Copyright © 2024 André Kugland ////////
5//////// ////////
6//////// This program is free software: you can redistribute it and/or modify ////////
7//////// it under the terms of the GNU General Public License as published by ////////
8//////// the Free Software Foundation, either version 3 of the License, or ////////
9//////// (at your option) any later version. ////////
10//////// ////////
11//////// This program is distributed in the hope that it will be useful, ////////
12//////// but WITHOUT ANY WARRANTY; without even the implied warranty of ////////
13//////// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ////////
14//////// GNU General Public License for more details. ////////
15//////// ////////
16//////// You should have received a copy of the GNU General Public License ////////
17//////// along with this program. If not, see https://www.gnu.org/licenses/. ////////
18
19//! This module contains the types used for error handling in this crate.
20
21use parse_display::{Display, FromStr};
22use thiserror::Error as ThisError;
23
24#[derive(ThisError, Debug)]
25pub enum Error {
26 /// Transport errors returned by [`ureq`].
27 #[error("Transport error: {0}")]
28 Transport(#[from] ureq::Transport),
29
30 /// Errors when deserializing JSON.
31 #[error("JSON error: {0}")]
32 Json(#[from] serde_json::Error),
33
34 /// API error are the errors returned as part of a JSON response body from the API.
35 #[error("API error: {message} ({kind})")]
36 Api {
37 /// The error message returned by the API.
38 message: String,
39 /// The kind of error returned by the API.
40 kind: ErrorKind,
41 },
42}
43
44/// Kinds of error returned by the API.
45///
46/// These errors are not clearly documented by the API, this list is a reverse-engineered list of
47/// errors returned by the API.
48///
49/// [`ErrorKind::Status`] is not returned as part of a JSON response body, but is instead generated
50/// when the server returns a 4xx or 5xx status code and we can't parse the response as JSON.
51#[derive(Display, FromStr, Debug, PartialEq)]
52#[display(style = "snake_case")]
53pub enum ErrorKind {
54 /// The site you asked for doesn't exist.
55 SiteNotFound,
56 /// Authentication failed
57 InvalidAuth,
58 /// You tried to delete `/index.html`.
59 CannotDeleteIndex,
60 /// You tried to delete `/`.
61 CannotDeleteSiteDirectory,
62 /// You tried to delete a file that doesn't exist.
63 MissingFiles,
64 /// You tried to upload a file with a prohibited extension.
65 InvalidFileType,
66 /// Server returned a 4xx/5xx status code and we couldn't parse the response as JSON.
67 #[from_str(ignore)]
68 Status,
69 /// An unknown error occurred.
70 #[from_str(ignore)]
71 Unknown,
72}
73
74/// The result type used by this crate.
75pub type Result<T> = std::result::Result<T, Error>;