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

mendersoftware / mender-flash / 1001644023

22 Jun 2023 08:46AM UTC coverage: 67.647% (-1.1%) from 68.75%
1001644023

push

gitlab-ci

web-flow
Merge pull request #4 from lluiscampos/ci-rename-job

ci: Rename unit tests job for consistency with the other projects

207 of 306 relevant lines covered (67.65%)

17.65 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 <string>
16
#include <system_error>
17
#include <type_traits>
18

19
#ifndef MENDER_COMMON_ERROR_HPP
20
#define MENDER_COMMON_ERROR_HPP
21

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

34
namespace mender {
35
namespace common {
36
namespace error {
37

38
class Error {
39
public:
40
        std::error_condition code;
41
        std::string message;
42

43
        Error(const std::error_condition &ec, const std::string &msg) :
×
44
                code(ec),
45
                message(msg) {
×
46
        }
×
47
        Error(const Error &e) :
48
                code(e.code),
49
                message(e.message) {
50
        }
51

52
        bool operator==(const Error &other) const {
53
                return this->message == other.message && this->code == other.code;
54
        }
55

56
        bool operator!=(const Error &other) const {
57
                return this->message != other.message || this->code != other.code;
58
        }
59

60
        std::string String() const {
×
61
                return code.message() + ": " + message;
×
62
        }
63
};
64
std::ostream &operator<<(std::ostream &os, const Error &err);
65

66
extern const Error NoError;
67

68
enum ErrorCode {
69
        ErrorCodeNoError, // Conflicts with above name, we don't really need it so prefix it.
70
        ProgrammingError,
71
        GenericError, // For when you have no underlying error code, provide message instead.
72
};
73

74
class CommonErrorCategoryClass : public std::error_category {
75
public:
76
        const char *name() const noexcept override;
77
        std::string message(int code) const override;
78
};
79
extern const CommonErrorCategoryClass CommonErrorCategory;
80

81
Error MakeError(ErrorCode code, const std::string &msg);
82

83
} // namespace error
84
} // namespace common
85
} // namespace mender
86

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