kvarn_fastcgi_client/
error.rs

1// Copyright 2022 jmjoy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::meta::{ProtocolStatus, RequestType};
16
17pub type ClientResult<T> = Result<T, ClientError>;
18
19#[derive(Debug, thiserror::Error)]
20pub enum ClientError {
21    /// Wapper of `tokio::io::Error`
22    #[error(transparent)]
23    Io(#[from] tokio::io::Error),
24
25    /// Usually not happen.
26    #[error("Response not found of request id `{id}`")]
27    RequestIdNotFound { id: u16 },
28
29    /// Usually not happen.
30    #[error("Response not found of request id `{id}`")]
31    ResponseNotFound { id: u16 },
32
33    /// Maybe unimplemented request type received fom response.
34    #[error("Response not found of request id `{request_type}`")]
35    UnknownRequestType { request_type: RequestType },
36
37    /// Response not complete, first is protocol status and second is app
38    /// status, see fastcgi protocol.
39    #[error("This app can't multiplex [CantMpxConn]; AppStatus: {app_status}")]
40    EndRequestCantMpxConn { app_status: u32 },
41
42    /// Response not complete, first is protocol status and second is app
43    /// status, see fastcgi protocol.
44    #[error("New request rejected; too busy [OVERLOADED]; AppStatus: {app_status}")]
45    EndRequestOverloaded { app_status: u32 },
46
47    /// Response not complete, first is protocol status and second is app
48    /// status, see fastcgi protocol.
49    #[error("Role value not known [UnknownRole]; AppStatus: {app_status}")]
50    EndRequestUnknownRole { app_status: u32 },
51}
52
53impl ClientError {
54    pub(crate) fn new_end_request_with_protocol_status(
55        protocol_status: ProtocolStatus, app_status: u32,
56    ) -> Self {
57        match protocol_status {
58            ProtocolStatus::CantMpxConn => ClientError::EndRequestCantMpxConn { app_status },
59            ProtocolStatus::Overloaded => ClientError::EndRequestOverloaded { app_status },
60            _ => ClientError::EndRequestUnknownRole { app_status },
61        }
62    }
63}