Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #pragma once
18 : :
19 : : #include <boost/system/error_code.hpp>
20 : : #include <mutex>
21 : : #include <optional>
22 : : #include <string>
23 : : #include <string_view>
24 : : #include <utility>
25 : :
26 : : #include "wirestead/diagnostics/error_types.hpp"
27 : :
28 : : // Extracted from what used to be identical, independently-maintained
29 : : // record_error()/last_error_info_ logic in TcpClient::Impl and
30 : : // UdsClient::Impl (jwsung91/wirestead#445), now shared so every transport can
31 : : // implement interface::Channel::last_error_info() the same way instead of
32 : : // only two of seven having any detailed-error mechanism at all.
33 : : namespace wirestead {
34 : : namespace transport {
35 : :
36 : : class ErrorInfoHolder {
37 : : public:
38 : 480 : explicit ErrorInfoHolder(std::string component) : component_(std::move(component)) {}
39 : :
40 : 199 : void record_error(diagnostics::ErrorLevel lvl, diagnostics::ErrorCategory cat, std::string_view operation,
41 : : const boost::system::error_code& ec, std::string_view msg, bool retryable, uint32_t retry_count) {
42 : 199 : diagnostics::ErrorInfo info(lvl, cat, component_, operation, msg, ec, retryable);
43 : 199 : info.retry_count = retry_count;
44 : 199 : std::lock_guard<std::mutex> lock(mtx_);
45 : 199 : last_error_info_ = std::move(info);
46 : 199 : }
47 : :
48 : 230 : std::optional<diagnostics::ErrorInfo> last_error_info() const {
49 : 230 : std::lock_guard<std::mutex> lock(mtx_);
50 : 460 : return last_error_info_;
51 : 230 : }
52 : :
53 : : private:
54 : : std::string component_;
55 : : mutable std::mutex mtx_;
56 : : std::optional<diagnostics::ErrorInfo> last_error_info_;
57 : : };
58 : :
59 : : } // namespace transport
60 : : } // namespace wirestead
|