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

mendersoftware / go-lib-micro / 915880509

pending completion
915880509

Pull #200

gitlab-ci

alfrunes
chore: Constify inline strings

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #200: Fix logging caller for helper functions and expose configuration options

29 of 95 new or added lines in 2 files covered. (30.53%)

4 existing lines in 2 files now uncovered.

1398 of 1798 relevant lines covered (77.75%)

4.06 hits per line

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

0.0
/rest_utils/response_helpers.go
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
package rest_utils
15

16
import (
17
        "net/http"
18

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

23
        "github.com/mendersoftware/go-lib-micro/log"
24
        "github.com/mendersoftware/go-lib-micro/requestid"
25
)
26

27
// return selected http code + error message directly taken from error
28
// log error
29
func RestErrWithLog(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int) {
×
NEW
30
        l = l.WithCallerContext(1)
×
NEW
31
        restErrWithLogMsg(w, r, l, e, code, "", logrus.ErrorLevel)
×
UNCOV
32
}
×
33

34
// return http 500, with an "internal error" message
35
// log full error
36
func RestErrWithLogInternal(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error) {
×
37
        msg := "internal error"
×
NEW
38
        l = l.WithCallerContext(1)
×
NEW
39
        restErrWithLogMsg(w, r, l, e, http.StatusInternalServerError, msg, logrus.ErrorLevel)
×
UNCOV
40
}
×
41

42
// return an error code with an overriden message (to avoid exposing the details)
43
// log full error as debug
44
func RestErrWithDebugMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
45
        l = l.WithCallerContext(1)
×
46
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.DebugLevel)
×
47
}
×
48

49
// return an error code with an overriden message (to avoid exposing the details)
50
// log full error as info
51
func RestErrWithInfoMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
52
        l = l.WithCallerContext(1)
×
53
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.InfoLevel)
×
54
}
×
55

56
// return an error code with an overriden message (to avoid exposing the details)
57
// log full error as warning
58
func RestErrWithWarningMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
59
        l = l.WithCallerContext(1)
×
60
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.WarnLevel)
×
61
}
×
62

63
// same as RestErrWithErrorMsg - for backward compatibility purpose
64
// return an error code with an overriden message (to avoid exposing the details)
65
// log full error as error
66
func RestErrWithLogMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
67
        l = l.WithCallerContext(1)
×
68
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.ErrorLevel)
×
69
}
×
70

71
// return an error code with an overriden message (to avoid exposing the details)
72
// log full error as error
73
func RestErrWithErrorMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
74
        l = l.WithCallerContext(1)
×
75
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.ErrorLevel)
×
76
}
×
77

78
// return an error code with an overriden message (to avoid exposing the details)
79
// log full error as fatal
80
func RestErrWithFatalMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
81
        l = l.WithCallerContext(1)
×
82
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.FatalLevel)
×
83
}
×
84

85
// return an error code with an overriden message (to avoid exposing the details)
86
// log full error as panic
87
func RestErrWithPanicMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger, e error, code int, msg string) {
×
NEW
88
        l = l.WithCallerContext(1)
×
89
        restErrWithLogMsg(w, r, l, e, code, msg, logrus.PanicLevel)
×
90
}
×
91

92
// return an error code with an overriden message (to avoid exposing the details)
93
// log full error with given log level
94
func restErrWithLogMsg(w rest.ResponseWriter, r *rest.Request, l *log.Logger,
95
        e error, code int, msg string, logLevel logrus.Level) {
×
96

×
97
        if msg != "" {
×
98
                e = errors.Wrap(e, msg)
×
99
        } else {
×
100
                msg = e.Error()
×
101
        }
×
102

103
        w.WriteHeader(code)
×
104
        err := w.WriteJson(ApiError{
×
105
                Err:   msg,
×
106
                ReqId: requestid.GetReqId(r),
×
107
        })
×
108
        if err != nil {
×
109
                panic(err)
×
110
        }
111
        switch logLevel {
×
112
        case logrus.DebugLevel:
×
NEW
113
                l.Debug(e.Error())
×
114
        case logrus.InfoLevel:
×
NEW
115
                l.Info(e.Error())
×
116
        case logrus.WarnLevel:
×
NEW
117
                l.Warn(e.Error())
×
118
        case logrus.ErrorLevel:
×
NEW
119
                l.Error(e.Error())
×
120
        case logrus.FatalLevel:
×
NEW
121
                l.Fatal(e.Error())
×
122
        case logrus.PanicLevel:
×
NEW
123
                l.Panic(e.Error())
×
124
        }
125
}
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