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
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    #[error(transparent)]
30    Io(#[from] tokio::io::Error),
31
32    /// Usually not happen.
33    #[error("Response not found of request id `{id}`")]
34    RequestIdNotFound {
35        /// The request ID that was not found
36        id: u16,
37    },
38
39    /// Usually not happen.
40    #[error("Response not found of request id `{id}`")]
41    ResponseNotFound {
42        /// The request ID for which no response was found
43        id: u16,
44    },
45
46    /// Maybe unimplemented request type received fom response.
47    #[error("Response not found of request id `{request_type}`")]
48    UnknownRequestType {
49        /// The unknown request type received
50        request_type: RequestType,
51    },
52
53    /// Response not complete, first is protocol status and second is app
54    /// status, see fastcgi protocol.
55    #[error("This app can't multiplex [CantMpxConn]; AppStatus: {app_status}")]
56    EndRequestCantMpxConn {
57        /// The application status code
58        app_status: u32,
59    },
60
61    /// Response not complete, first is protocol status and second is app
62    /// status, see fastcgi protocol.
63    #[error("New request rejected; too busy [OVERLOADED]; AppStatus: {app_status}")]
64    EndRequestOverloaded {
65        /// The application status code
66        app_status: u32,
67    },
68
69    /// Response not complete, first is protocol status and second is app
70    /// status, see fastcgi protocol.
71    #[error("Role value not known [UnknownRole]; AppStatus: {app_status}")]
72    EndRequestUnknownRole {
73        /// The application status code
74        app_status: u32,
75    },
76}
77
78impl ClientError {
79    /// Creates a new end request error based on the protocol status.
80    ///
81    /// # Arguments
82    ///
83    /// * `protocol_status` - The protocol status returned by the FastCGI server
84    /// * `app_status` - The application status code
85    pub(crate) fn new_end_request_with_protocol_status(
86        protocol_status: ProtocolStatus, app_status: u32,
87    ) -> Self {
88        match protocol_status {
89            ProtocolStatus::CantMpxConn => ClientError::EndRequestCantMpxConn { app_status },
90            ProtocolStatus::Overloaded => ClientError::EndRequestOverloaded { app_status },
91            _ => ClientError::EndRequestUnknownRole { app_status },
92        }
93    }
94}