pingap_core/
lib.rs

1// Copyright 2024-2025 Tree xie.
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 snafu::Snafu;
16
17pub static LOG_CATEGORY: &str = "core";
18
19#[derive(Debug, Snafu)]
20pub enum Error {
21    #[snafu(display("Invalid error: {message}"))]
22    Invalid { message: String },
23    #[snafu(display("Plugin {category} not found"))]
24    NotFound { category: String },
25}
26
27/// Creates a new internal error
28pub fn new_internal_error(status: u16, message: String) -> pingora::BError {
29    pingora::Error::because(
30        pingora::ErrorType::HTTPStatus(status),
31        message,
32        pingora::Error::new(pingora::ErrorType::InternalError),
33    )
34}
35
36mod ctx;
37mod http_header;
38mod http_response;
39mod notification;
40mod plugin;
41mod service;
42mod ttl_lru_limit;
43mod util;
44
45pub use ctx::*;
46pub use http_header::*;
47pub use http_response::*;
48pub use notification::*;
49pub use pingora_limits::inflight::*;
50pub use pingora_limits::rate::*;
51pub use plugin::*;
52pub use service::*;
53pub use tinyufo::TinyUfo;
54pub use ttl_lru_limit::*;
55pub use util::*;
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_new_internal_error() {
63        let err = new_internal_error(500, "Internal Server Error".to_string());
64        assert_eq!(
65            err.to_string().trim(),
66            "HTTPStatus context: Internal Server Error cause:  InternalError"
67        );
68    }
69}