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

mendersoftware / mender-server / 1833359013

23 May 2025 01:25PM UTC coverage: 66.318% (+0.5%) from 65.861%
1833359013

Pull #674

gitlab-ci

mzedel
fix(gui): prevented device tag editor to be shown when no tags exist

- this is to reduce confusion about tags defined async to the current session not being visible

Ticket: ME-528
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #674: ME-529, ME-528 - adjustments to device tag editing

29554 of 44564 relevant lines covered (66.32%)

1.45 hits per line

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

57.5
/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

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

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

47
// return http 500, with an "internal error" message
48
// log full error
49
func RestErrWithLogInternal(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error) {
2✔
50
        msg := "internal error"
2✔
51
        restErrWithLogMsg(w, r, l, e, http.StatusInternalServerError, msg, logrus.ErrorLevel)
2✔
52
}
2✔
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,
76
) {
1✔
77
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.InfoLevel)
1✔
78
}
1✔
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,
89
) {
2✔
90
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.WarnLevel)
2✔
91
}
2✔
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,
149
        e error, code int, msg string, logLevel logrus.Level) {
5✔
150
        if msg != "" {
9✔
151
                e = errors.WithMessage(e, msg)
4✔
152
        } else {
9✔
153
                msg = e.Error()
5✔
154
        }
5✔
155
        var emitLog bool
5✔
156
        if lc := accesslog.GetContext(r.Context()); lc != nil {
10✔
157
                err := e
5✔
158
                // Try push error to accesslog middleware.
5✔
159
                f := getCalleeFrame(2)
5✔
160
                if f != nil {
10✔
161
                        // Add the call frame to the error message:
5✔
162
                        // "<call frame>: <error message>"
5✔
163
                        err = errors.WithMessage(e, log.FmtCaller(*f))
5✔
164
                }
5✔
165
                emitLog = !lc.PushError(err)
5✔
166
        }
167

168
        w.WriteHeader(code)
5✔
169
        err := w.WriteJson(ApiError{
5✔
170
                Err:   msg,
5✔
171
                ReqId: requestid.GetReqId(r),
5✔
172
        })
5✔
173
        if err != nil {
5✔
174
                panic(err)
×
175
        }
176
        if emitLog {
5✔
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