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

mendersoftware / mender-server / 1622978334

13 Jan 2025 03:51PM UTC coverage: 72.802% (-3.8%) from 76.608%
1622978334

Pull #300

gitlab-ci

alfrunes
fix: Deployment device count should not exceed max devices

Added a condition to skip deployments when the device count reaches max
devices.

Changelog: Title
Ticket: MEN-7847
Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #300: fix: Deployment device count should not exceed max devices

4251 of 6164 branches covered (68.96%)

Branch coverage included in aggregate %.

0 of 18 new or added lines in 1 file covered. (0.0%)

2544 existing lines in 83 files now uncovered.

42741 of 58384 relevant lines covered (73.21%)

21.49 hits per line

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

0.0
/backend/pkg/rest_utils/response_helpers.go
1
// Copyright 2024 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
package rest_utils
15

16
import (
17
        "net/http"
18
        "runtime"
19

20
        "github.com/ant0ine/go-json-rest/rest"
21
        "github.com/pkg/errors"
22
        "github.com/sirupsen/logrus"
23

24
        "github.com/mendersoftware/mender-server/pkg/accesslog"
25
        "github.com/mendersoftware/mender-server/pkg/log"
26
        "github.com/mendersoftware/mender-server/pkg/requestid"
27
)
28

UNCOV
29
func getCalleeFrame(skip int) *runtime.Frame {
×
UNCOV
30
        const calleeDepth = 2
×
UNCOV
31
        var pc [1]uintptr
×
UNCOV
32
        i := runtime.Callers(calleeDepth+skip, pc[:])
×
UNCOV
33
        frame, _ := runtime.CallersFrames(pc[:i]).
×
UNCOV
34
                Next()
×
UNCOV
35
        if frame.Func != nil {
×
UNCOV
36
                return &frame
×
UNCOV
37
        }
×
38
        return nil
×
39
}
40

41
// return selected http code + error message directly taken from error
42
// log error
UNCOV
43
func RestErrWithLog(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int) {
×
UNCOV
44
        restErrWithLogMsg(w, r, l, e, code, "", logrus.ErrorLevel)
×
UNCOV
45
}
×
46

47
// return http 500, with an "internal error" message
48
// log full error
UNCOV
49
func RestErrWithLogInternal(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error) {
×
UNCOV
50
        msg := "internal error"
×
UNCOV
51
        restErrWithLogMsg(w, r, l, e, http.StatusInternalServerError, msg, logrus.ErrorLevel)
×
UNCOV
52
}
×
53

54
// return an error code with an overriden message (to avoid exposing the details)
55
// log full error as debug
56
func RestErrWithDebugMsg(
57
        w rest.ResponseWriter,
58
        r *rest.Request,
59
        l *log.Logger,
60
        e error,
61
        code int,
62
        msg string,
63
) {
×
64
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.DebugLevel)
×
65
}
×
66

67
// return an error code with an overriden message (to avoid exposing the details)
68
// log full error as info
69
func RestErrWithInfoMsg(
70
        w rest.ResponseWriter,
71
        r *rest.Request,
72
        l *log.Logger,
73
        e error,
74
        code int,
75
        msg string,
UNCOV
76
) {
×
UNCOV
77
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.InfoLevel)
×
UNCOV
78
}
×
79

80
// return an error code with an overriden message (to avoid exposing the details)
81
// log full error as warning
82
func RestErrWithWarningMsg(
83
        w rest.ResponseWriter,
84
        r *rest.Request,
85
        l *log.Logger,
86
        e error,
87
        code int,
88
        msg string,
UNCOV
89
) {
×
UNCOV
90
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.WarnLevel)
×
UNCOV
91
}
×
92

93
// same as RestErrWithErrorMsg - for backward compatibility purpose
94
// return an error code with an overriden message (to avoid exposing the details)
95
// log full error as error
96
func RestErrWithLogMsg(
97
        w rest.ResponseWriter,
98
        r *rest.Request,
99
        l *log.Logger,
100
        e error,
101
        code int,
102
        msg string,
103
) {
×
104
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.ErrorLevel)
×
105
}
×
106

107
// return an error code with an overriden message (to avoid exposing the details)
108
// log full error as error
109
func RestErrWithErrorMsg(
110
        w rest.ResponseWriter,
111
        r *rest.Request,
112
        l *log.Logger,
113
        e error,
114
        code int,
115
        msg string,
116
) {
×
117
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.ErrorLevel)
×
118
}
×
119

120
// return an error code with an overriden message (to avoid exposing the details)
121
// log full error as fatal
122
func RestErrWithFatalMsg(
123
        w rest.ResponseWriter,
124
        r *rest.Request,
125
        l *log.Logger,
126
        e error,
127
        code int,
128
        msg string,
129
) {
×
130
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.FatalLevel)
×
131
}
×
132

133
// return an error code with an overriden message (to avoid exposing the details)
134
// log full error as panic
135
func RestErrWithPanicMsg(
136
        w rest.ResponseWriter,
137
        r *rest.Request,
138
        l *log.Logger,
139
        e error,
140
        code int,
141
        msg string,
142
) {
×
143
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.PanicLevel)
×
144
}
×
145

146
// return an error code with an overriden message (to avoid exposing the details)
147
// log full error with given log level
148
func restErrWithLogMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger,
UNCOV
149
        e error, code int, msg string, logLevel logrus.Level) {
×
UNCOV
150
        if msg != "" {
×
UNCOV
151
                e = errors.WithMessage(e, msg)
×
UNCOV
152
        } else {
×
UNCOV
153
                msg = e.Error()
×
UNCOV
154
        }
×
UNCOV
155
        var emitLog bool
×
UNCOV
156
        if lc := accesslog.GetContext(r.Context()); lc != nil {
×
UNCOV
157
                err := e
×
UNCOV
158
                // Try push error to accesslog middleware.
×
UNCOV
159
                f := getCalleeFrame(2)
×
UNCOV
160
                if f != nil {
×
UNCOV
161
                        // Add the call frame to the error message:
×
UNCOV
162
                        // "<call frame>: <error message>"
×
UNCOV
163
                        err = errors.WithMessage(e, log.FmtCaller(*f))
×
UNCOV
164
                }
×
UNCOV
165
                emitLog = !lc.PushError(err)
×
166
        }
167

UNCOV
168
        w.WriteHeader(code)
×
UNCOV
169
        err := w.WriteJson(ApiError{
×
UNCOV
170
                Err:   msg,
×
UNCOV
171
                ReqId: requestid.GetReqId(r),
×
UNCOV
172
        })
×
UNCOV
173
        if err != nil {
×
174
                panic(err)
×
175
        }
UNCOV
176
        if emitLog {
×
177
                // Only emit a log entry if we failed to push the error to
×
178
                // accessloger.
×
179
                // Set the caller frame for the entry
×
180
                l = l.WithCallerContext(2)
×
181
                switch logLevel {
×
182
                case logrus.DebugLevel:
×
183
                        l.Debug(e.Error())
×
184
                case logrus.InfoLevel:
×
185
                        l.Info(e.Error())
×
186
                case logrus.WarnLevel:
×
187
                        l.Warn(e.Error())
×
188
                case logrus.ErrorLevel:
×
189
                        l.Error(e.Error())
×
190
                case logrus.FatalLevel:
×
191
                        l.Fatal(e.Error())
×
192
                case logrus.PanicLevel:
×
193
                        l.Panic(e.Error())
×
194
                }
195
        }
196
}
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