1use crate::media_container::server::Feature;
2use isahc::{AsyncBody, AsyncReadResponseExt, Response as HttpResponse};
3use serde::Deserialize;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum Error {
8 #[error("Authenticated client must be provided.")]
9 ClientNotAuthenticated,
10 #[error("Non-authenticated client must be provided.")]
11 ClientAuthenticated,
12 #[error("Unable to deserialize JSON: {source}.")]
13 JsonDeserealiseError {
14 #[from]
15 source: serde_json::Error,
16 },
17 #[error("Unable to deserialize XML: {source}.")]
18 XmlDeserealiseError {
19 #[from]
20 source: quick_xml::de::DeError,
21 },
22 #[error("{source}")]
23 UrlencodingError {
24 #[from]
25 source: serde_urlencoded::ser::Error,
26 },
27 #[error("{source}")]
28 HttpError {
29 #[from]
30 source: http::Error,
31 },
32 #[error("{source}")]
33 IsahcError {
34 #[from]
35 source: isahc::Error,
36 },
37 #[error("{source}")]
38 StdIoError {
39 #[from]
40 source: std::io::Error,
41 },
42 #[error("Error while communicating with MyPlexApi: {errors:?}.")]
43 MyPlexErrorResponse { errors: Vec<Self> },
44 #[error("Error occurred while communicating to MyPlex API: #{code} - {message}.")]
45 MyPlexApiError { code: i32, message: String },
46 #[error("Failed to get claim token: {0}.")]
47 FailedToGetClaimToken(String),
48 #[error("Unexpected API response: HTTP {status_code}, content: {content}.")]
49 UnexpectedApiResponse { status_code: u16, content: String },
50 #[error("The requested webhook wasn't found: {0}.")]
51 WebhookNotFound(String),
52 #[error("The mandatory feature is not available: {0}.")]
53 SubscriptionFeatureNotAvailable(Feature),
54 #[error("OTP is required for the authentication.")]
55 OtpRequired,
56 #[error("OTP is provided, but no username/password.")]
57 UselessOtp,
58 #[error("Connecting to the device is not supported.")]
59 DeviceConnectionNotSupported,
60 #[error("Device doesn't have any exposed connection endpoints.")]
61 DeviceConnectionsIsEmpty,
62 #[error("Requested unknown setting: {0}.")]
63 RequestedSettingNotFound(String),
64 #[error("You can't set setting to a value of a different type.")]
65 IncompatibleSettingValues,
66 #[error("Provided pin is already expired.")]
67 PinExpired,
68 #[error("Provided pin is not linked yet.")]
69 PinNotLinked,
70 #[error("Item requested was not found on the server.")]
71 ItemNotFound,
72 #[error("The requested transcode parameters were invalid.")]
73 InvalidTranscodeSettings,
74 #[error("The transcode request failed: {0}.")]
75 TranscodeError(String),
76 #[error("The server thinks the client should just play the original media.")]
77 TranscodeRefused,
78 #[error("Only invites with status pending_received can be accepted.")]
79 InviteAcceptingNotPendingReceived,
80 #[error("Unexpected error. Please create a bug report.")]
81 UnexpectedError,
82}
83
84const PLEX_API_ERROR_CODE_AUTH_OTP_REQUIRED: i32 = 1029;
85
86impl Error {
87 pub async fn from_response(mut response: HttpResponse<AsyncBody>) -> Self {
88 let status_code = response.status().as_u16();
89 let response_body = match response.text().await {
90 Ok(body) => body,
91 Err(err) => {
92 return err.into();
93 }
94 };
95
96 let err: Result<MyPlexApiErrorResponse, Error>;
97 if let Some(content_type) = response.headers().get("Content-type") {
98 match content_type.to_str().unwrap().split("; ").next().unwrap() {
99 "application/xml" => {
100 err = quick_xml::de::from_str::<MyPlexApiErrorResponse>(&response_body)
101 .map_err(|e| e.into())
102 }
103 _ => {
104 err = serde_json::from_str::<MyPlexApiErrorResponse>(&response_body)
105 .map_err(|e| e.into())
106 }
107 }
108 } else {
109 err = serde_json::from_str::<MyPlexApiErrorResponse>(&response_body)
110 .map_err(|e| e.into());
111 }
112
113 match err {
114 Ok(r) => {
115 if r.errors.len() == 1 && r.errors[0].code == PLEX_API_ERROR_CODE_AUTH_OTP_REQUIRED
116 {
117 Self::OtpRequired
118 } else {
119 r.into()
120 }
121 }
122 Err(_) => Self::UnexpectedApiResponse {
123 status_code,
124 content: response_body,
125 },
126 }
127 }
128}
129
130#[derive(Deserialize, Debug, Clone)]
131#[cfg_attr(feature = "tests_deny_unknown_fields", serde(deny_unknown_fields))]
132struct MyPlexApiError {
133 code: i32,
134 message: String,
135
136 #[allow(dead_code)]
138 status: u16,
139}
140
141#[derive(Deserialize, Debug, Clone)]
142#[cfg_attr(feature = "tests_deny_unknown_fields", serde(deny_unknown_fields))]
143pub(crate) struct MyPlexApiErrorResponse {
144 errors: Vec<MyPlexApiError>,
145}
146
147impl From<MyPlexApiError> for Error {
148 fn from(error: MyPlexApiError) -> Self {
149 Self::MyPlexApiError {
150 code: error.code,
151 message: error.message,
152 }
153 }
154}
155
156impl From<MyPlexApiErrorResponse> for Error {
157 fn from(r: MyPlexApiErrorResponse) -> Self {
158 Self::MyPlexErrorResponse {
159 errors: r.errors.into_iter().map(|e| e.into()).collect(),
160 }
161 }
162}