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

mendersoftware / mender-server / 1807452801

08 May 2025 01:22PM UTC coverage: 65.386% (+0.1%) from 65.27%
1807452801

Pull #653

gitlab-ci

bahaa-ghazal
refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

Ticket: MEN-8236
Changelog: Title
Signed-off-by: Bahaa Aldeen Ghazal <bahaa.ghazal@northern.tech>
Pull Request #653: refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

476 of 525 new or added lines in 6 files covered. (90.67%)

62 existing lines in 9 files now uncovered.

31949 of 48862 relevant lines covered (65.39%)

1.37 hits per line

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

53.75
/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 {
4✔
30
        const calleeDepth = 2
4✔
31
        var pc [1]uintptr
4✔
32
        i := runtime.Callers(calleeDepth+skip, pc[:])
4✔
33
        frame, _ := runtime.CallersFrames(pc[:i]).
4✔
34
                Next()
4✔
35
        if frame.Func != nil {
8✔
36
                return &frame
4✔
37
        }
4✔
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) {
4✔
44
        restErrWithLogMsg(w, r, l, e, code, "", logrus.ErrorLevel)
4✔
45
}
4✔
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,
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,
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) {
4✔
150
        if msg != "" {
7✔
151
                e = errors.WithMessage(e, msg)
3✔
152
        } else {
7✔
153
                msg = e.Error()
4✔
154
        }
4✔
155
        var emitLog bool
4✔
156
        if lc := accesslog.GetContext(r.Context()); lc != nil {
8✔
157
                err := e
4✔
158
                // Try push error to accesslog middleware.
4✔
159
                f := getCalleeFrame(2)
4✔
160
                if f != nil {
8✔
161
                        // Add the call frame to the error message:
4✔
162
                        // "<call frame>: <error message>"
4✔
163
                        err = errors.WithMessage(e, log.FmtCaller(*f))
4✔
164
                }
4✔
165
                emitLog = !lc.PushError(err)
4✔
166
        }
167

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