• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1582712564

10 Dec 2024 06:44PM UTC coverage: 79.706% (-0.2%) from 79.882%
1582712564

push

gitlab-ci

web-flow
Merge pull request #1713 from lluiscampos/4.0.x-MEN-7810-fix-and-changelog

4.0.x: MEN-7810 fix and changelog

11 of 11 new or added lines in 1 file covered. (100.0%)

43 existing lines in 10 files now uncovered.

7207 of 9042 relevant lines covered (79.71%)

12100.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.0
/src/common/error.hpp
1
// Copyright 2023 Northern.tech AS
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
#include <functional>
16
#include <string>
17
#include <system_error>
18
#include <type_traits>
19

20
#ifndef MENDER_COMMON_ERROR_HPP
21
#define MENDER_COMMON_ERROR_HPP
22

23
// Note that this may cause condition to be evaluated twice!
24
#define AssertOrReturnError(condition) AssertOrReturnErrorOnLine(condition, __LINE__)
25
#define AssertOrReturnErrorOnLine(condition, line)                           \
26
        {                                                                        \
27
                if (!(condition)) {                                                  \
28
                        assert(condition);                                               \
29
                        return mender::common::error::MakeError(                         \
30
                                mender::common::error::ProgrammingError,                     \
31
                                "Assert `" #condition "` in " __FILE__ ":" + to_string(line) \
32
                                        + " failed. This is a bug.");                            \
33
                }                                                                    \
34
        }
35

36
// Note that this may cause condition to be evaluated twice!
37
#define AssertOrReturnUnexpected(condition) AssertOrReturnUnexpectedOnLine(condition, __LINE__)
38
#define AssertOrReturnUnexpectedOnLine(condition, line)                      \
39
        {                                                                        \
40
                if (!(condition)) {                                                  \
41
                        assert(condition);                                               \
42
                        return expected::unexpected(mender::common::error::MakeError(    \
43
                                mender::common::error::ProgrammingError,                     \
44
                                "Assert `" #condition "` in " __FILE__ ":" + to_string(line) \
45
                                        + " failed. This is a bug."));                           \
46
                }                                                                    \
47
        }
48

49
namespace mender {
50
namespace common {
51
namespace error {
52

53
class Error {
151✔
54
public:
55
        std::error_condition code;
56
        std::string message;
57

58
        Error() {
7✔
59
        }
7✔
UNCOV
60
        Error(const std::error_condition &ec, const std::string &msg) :
×
61
                code {ec},
62
                message {msg} {
8✔
63
        }
2✔
64
        Error(const Error &e) :
×
65
                code {e.code},
66
                message {e.message} {
×
67
        }
×
68

69
        bool operator==(const Error &other) const {
351✔
70
                return this->message == other.message && this->code == other.code;
351✔
71
        }
72

73
        bool operator!=(const Error &other) const {
92✔
74
                return this->message != other.message || this->code != other.code;
92✔
75
        }
76

UNCOV
77
        std::string String() const {
×
UNCOV
78
                return code.message() + ": " + message;
×
79
        }
80

81
        bool IsErrno(int errno_value) {
82
                return (
83
                        (this->code.category() == std::generic_category())
84
                        && (this->code.value() == errno_value));
85
        }
86

87
        Error FollowedBy(const Error &err) const;
88

89
        // Produces a new error with a context prefix, with the same error code.
90
        Error WithContext(const std::string &context) const;
91
};
92
std::ostream &operator<<(std::ostream &os, const Error &err);
93

94
extern const Error NoError;
95

96
enum ErrorCode {
97
        ErrorCodeNoError, // Conflicts with above name, we don't really need it so prefix it.
98
        ProgrammingError,
99
        GenericError, // For when you have no underlying error code, provide message instead.
100
        // Means that we do have an error, but don't print anything. Used for errors where the cli
101
        // already prints a nicely formatted message.
102
        ExitWithFailureError,
103
        // Means that we want to prevent further processing. Used for `--version` option.
104
        ExitWithSuccessError,
105
};
106

107
class CommonErrorCategoryClass : public std::error_category {
108
public:
109
        const char *name() const noexcept override;
110
        std::string message(int code) const override;
111
};
112
extern const CommonErrorCategoryClass CommonErrorCategory;
113

114
Error MakeError(ErrorCode code, const std::string &msg);
115

116
// Some parts of standard C++, such as regex, require exceptions. Use this function in cases where
117
// an exception cannot be avoided; it will automatically catch exceptions thrown by the given
118
// function, and produce an error from it. On platforms where exceptions are disabled, any attempt
119
// to throw them will abort instead, so if you use this function, do your utmost to avoid an
120
// exception ahead of time (make sure the regex is valid, for instance).
121
Error ExceptionToErrorOrAbort(std::function<void()> func);
122

123
} // namespace error
124
} // namespace common
125
} // namespace mender
126

127
#endif // MENDER_COMMON_ERROR_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc