Skip to main content

rocketmq_model/common/
faq.rs

1// Copyright 2023 The RocketMQ Rust Authors
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
15pub struct FAQUrl;
16
17impl FAQUrl {
18    pub const APPLY_TOPIC_URL: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
19    pub const CLIENT_PARAMETER_CHECK_URL: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
20    pub const CLIENT_SERVICE_NOT_OK: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
21    pub const GROUP_NAME_DUPLICATE_URL: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
22    pub const LOAD_JSON_EXCEPTION: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
23    const MORE_INFORMATION: &'static str = "For more information, please visit the url, ";
24    pub const MQLIST_NOT_EXIST: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
25    pub const NAME_SERVER_ADDR_NOT_EXIST_URL: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
26    // FAQ: No route info of this topic, TopicABC
27    pub const NO_TOPIC_ROUTE_INFO: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
28    pub const SAME_GROUP_DIFFERENT_TOPIC: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
29    pub const SEND_MSG_FAILED: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
30    pub const SUBSCRIPTION_GROUP_NOT_EXIST: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
31    const TIP_STRING_BEGIN: &'static str = "\nSee ";
32    const TIP_STRING_END: &'static str = " for further details.";
33    pub const UNEXPECTED_EXCEPTION_URL: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
34    pub const UNKNOWN_HOST_EXCEPTION: &'static str = "https://rocketmq.apache.org/docs/bestPractice/06FAQ";
35
36    pub fn suggest_todo(url: &'static str) -> String {
37        format!("{}{}{}", Self::TIP_STRING_BEGIN, url, Self::TIP_STRING_END)
38    }
39
40    pub fn attach_default_url(error_message: Option<&str>) -> String {
41        if let Some(err_msg) = error_message {
42            let already_has_see_tip = err_msg.contains(Self::TIP_STRING_BEGIN);
43            let already_has_default = err_msg.contains(Self::MORE_INFORMATION);
44            if !already_has_see_tip && !already_has_default {
45                return format!(
46                    "{}\n{}{}",
47                    err_msg,
48                    Self::MORE_INFORMATION,
49                    Self::UNEXPECTED_EXCEPTION_URL
50                );
51            }
52        }
53        error_message.unwrap_or_default().to_string()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn attach_default_url_is_idempotent_and_preserves_format() {
63        let ordinary_error = "Test error";
64        let first_message = FAQUrl::attach_default_url(Some(ordinary_error));
65        assert_eq!(
66            first_message,
67            format!(
68                "{}\n{}{}",
69                ordinary_error,
70                FAQUrl::MORE_INFORMATION,
71                FAQUrl::UNEXPECTED_EXCEPTION_URL
72            )
73        );
74        let second_message = FAQUrl::attach_default_url(Some(&first_message));
75        assert_eq!(second_message, first_message);
76        let err_message_with_tip = format!("Error with tip {}", FAQUrl::TIP_STRING_BEGIN);
77        assert_eq!(
78            err_message_with_tip,
79            FAQUrl::attach_default_url(Some(&err_message_with_tip))
80        );
81        assert_eq!("", FAQUrl::attach_default_url(None));
82    }
83}