Skip to main content

fastcgi_connect/
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
15//! Error types and result type aliases for FastCGI operations.
16//!
17//! This module defines the error types that can occur during FastCGI
18//! communication and provides convenient type aliases for results.
19
20use crate::meta::{ProtocolStatus, RequestType};
21
22/// Result type alias for FastCGI client operations.
23pub type ClientResult<T> = Result<T, ClientError>;
24
25/// Error types that can occur during FastCGI communication.
26#[derive(Debug, thiserror::Error)]
27pub enum ClientError {
28    /// Wapper of `tokio::io::Error`
29    #[cfg(feature = "tokio")]
30    #[error(transparent)]
31    Io(#[from] tokio::io::Error),
32
33    /// Wapper of `smol::io::Error`
34    #[cfg(feature = "smol")]
35    #[error(transparent)]
36    Io(#[from] smol::io::Error),
37
38    /// Usually not happen.
39    #[error("Response not found of request id `{id}`")]
40    RequestIdNotFound {
41        /// The request ID that was not found
42        id: u16,
43    },
44
45    /// Usually not happen.
46    #[error("Response not found of request id `{id}`")]
47    ResponseNotFound {
48        /// The request ID for which no response was found
49        id: u16,
50    },
51
52    /// Maybe unimplemented request type received fom response.
53    #[error("Response not found of request id `{request_type}`")]
54    UnknownRequestType {
55        /// The unknown request type received
56        request_type: RequestType,
57    },
58
59    /// Response not complete, first is protocol status and second is app
60    /// status, see fastcgi protocol.
61    #[error("This app can't multiplex [CantMpxConn]; AppStatus: {app_status}")]
62    EndRequestCantMpxConn {
63        /// The application status code
64        app_status: u32,
65    },
66
67    /// Response not complete, first is protocol status and second is app
68    /// status, see fastcgi protocol.
69    #[error("New request rejected; too busy [OVERLOADED]; AppStatus: {app_status}")]
70    EndRequestOverloaded {
71        /// The application status code
72        app_status: u32,
73    },
74
75    /// Response not complete, first is protocol status and second is app
76    /// status, see fastcgi protocol.
77    #[error("Role value not known [UnknownRole]; AppStatus: {app_status}")]
78    EndRequestUnknownRole {
79        /// The application status code
80        app_status: u32,
81    },
82}
83
84impl ClientError {
85    /// Creates a new end request error based on the protocol status.
86    ///
87    /// # Arguments
88    ///
89    /// * `protocol_status` - The protocol status returned by the FastCGI server
90    /// * `app_status` - The application status code
91    pub(crate) fn new_end_request_with_protocol_status(
92        protocol_status: ProtocolStatus, app_status: u32,
93    ) -> Self {
94        match protocol_status {
95            ProtocolStatus::CantMpxConn => ClientError::EndRequestCantMpxConn { app_status },
96            ProtocolStatus::Overloaded => ClientError::EndRequestOverloaded { app_status },
97            _ => ClientError::EndRequestUnknownRole { app_status },
98        }
99    }
100}