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

mendersoftware / mender / 1012473002

21 Sep 2023 02:17PM UTC coverage: 78.107% (-0.3%) from 78.44%
1012473002

push

gitlab-ci

lluiscampos
chore: log stdout output on errors when parsing UM yes/no commands

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>

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

6468 of 8281 relevant lines covered (78.11%)

11106.28 hits per line

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

0.0
/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__ ":" #line " failed. This is a bug."); \
32
                }                                                                                    \
33
        }
34

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

47
namespace mender {
48
namespace common {
49
namespace error {
50

51
class Error {
52
public:
53
        std::error_condition code;
54
        std::string message;
55

56
        Error() {
57
        }
58
        Error(const std::error_condition &ec, const std::string &msg) :
59
                code {ec},
60
                message {msg} {
61
        }
62
        Error(const Error &e) :
×
63
                code {e.code},
64
                message {e.message} {
×
65
        }
×
66

67
        bool operator==(const Error &other) const {
68
                return this->message == other.message && this->code == other.code;
69
        }
70

71
        bool operator!=(const Error &other) const {
72
                return this->message != other.message || this->code != other.code;
73
        }
74

75
        std::string String() const {
76
                return code.message() + ": " + message;
77
        }
78

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

85
        Error FollowedBy(const Error &err) const;
86

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

92
extern const Error NoError;
93

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

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

112
Error MakeError(ErrorCode code, const std::string &msg);
113

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

121
} // namespace error
122
} // namespace common
123
} // namespace mender
124

125
#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

© 2025 Coveralls, Inc