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

vocdoni / saas-backend / 19574187329

21 Nov 2025 02:50PM UTC coverage: 62.366% (-0.006%) from 62.372%
19574187329

push

github

emmdim
db: clarify errors of CensusParticipants methods

1 of 4 new or added lines in 1 file covered. (25.0%)

100 existing lines in 3 files now uncovered.

6531 of 10472 relevant lines covered (62.37%)

38.98 hits per line

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

80.0
/errors/errors.go
1
package errors
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "net/http"
7
        "runtime"
8

9
        "go.vocdoni.io/dvote/log"
10
)
11

12
// Error is used by handler functions to wrap errors, assigning a unique error code
13
// and also specifying which HTTP Status should be used.
14
type Error struct {
15
        Err        error  // Original error
16
        Code       int    // Error code
17
        HTTPstatus int    // HTTP status code to return
18
        LogLevel   string // Log level for this error (defaults to "debug")
19
        Data       any    // Optional data to include in the error response
20
}
21

22
// MarshalJSON returns a JSON containing Err.Error() and Code. Field HTTPstatus is ignored.
23
//
24
// Example output: {"error":"account not found","code":4003}
25
func (e Error) MarshalJSON() ([]byte, error) {
160✔
26
        // This anon struct is needed to actually include the error string,
160✔
27
        // since it wouldn't be marshaled otherwise. (json.Marshal doesn't call Err.Error())
160✔
28
        return json.Marshal(
160✔
29
                struct {
160✔
30
                        Error string `json:"error"`
160✔
31
                        Code  int    `json:"code"`
160✔
32
                        Data  any    `json:"data,omitempty"`
160✔
33
                }{
160✔
34
                        Error: e.Err.Error(),
160✔
35
                        Code:  e.Code,
160✔
36
                        Data:  e.Data,
160✔
37
                })
160✔
38
}
160✔
39

40
// Error returns the Message contained inside the APIerror
41
func (e Error) Error() string {
157✔
42
        return e.Err.Error()
157✔
43
}
157✔
44

45
// Unwrap returns the error contained inside
UNCOV
46
func (e Error) Unwrap() error {
×
UNCOV
47
        return e.Err
×
UNCOV
48
}
×
49

50
// Write serializes a JSON msg using Error.Err and Error.Code
51
// and passes that to http.Error(). It also logs the error with appropriate level.
52
func (e Error) Write(w http.ResponseWriter) {
155✔
53
        msg, err := json.Marshal(e)
155✔
54
        if err != nil {
155✔
UNCOV
55
                log.Warn(err)
×
UNCOV
56
                http.Error(w, "marshal failed", http.StatusInternalServerError)
×
UNCOV
57
                return
×
UNCOV
58
        }
×
59

60
        // Get caller information for better logging
61
        pc, file, line, _ := runtime.Caller(1)
155✔
62
        caller := runtime.FuncForPC(pc).Name()
155✔
63

155✔
64
        // Log the error with appropriate level
155✔
65
        logLevel := e.LogLevel
155✔
66
        if logLevel == "" {
226✔
67
                // Default log level based on HTTP status
71✔
68
                if e.HTTPstatus >= 500 {
71✔
UNCOV
69
                        logLevel = "error"
×
70
                } else {
71✔
71
                        logLevel = "debug"
71✔
72
                }
71✔
73
        }
74

75
        // For 5xx errors, always log with Error level and include internal error details
76
        if e.HTTPstatus >= 500 {
173✔
77
                // For internal errors, log the full error details
18✔
78
                log.Errorw(e.Err, fmt.Sprintf("API error response [%d]: %s (code: %d, caller: %s, file: %s:%d)",
18✔
79
                        e.HTTPstatus, e.Error(), e.Code, caller, file, line))
18✔
80
        } else if log.Level() == log.LogLevelDebug {
292✔
81
                // For 4xx errors, log with debug level
137✔
82
                errMsg := fmt.Sprintf("API error response [%d]: %s (code: %d, caller: %s)",
137✔
83
                        e.HTTPstatus, e.Error(), e.Code, caller)
137✔
84

137✔
85
                switch logLevel {
137✔
86
                case "debug":
71✔
87
                        log.Debugw(errMsg)
71✔
88
                case "info":
66✔
89
                        log.Infow(errMsg)
66✔
UNCOV
90
                case "warn":
×
UNCOV
91
                        log.Warnw(errMsg)
×
UNCOV
92
                default:
×
UNCOV
93
                        log.Debugw(errMsg) // Default to debug level for unknown log levels
×
94
                }
95
        }
96

97
        // Set the content type to JSON
98
        w.Header().Set("Content-Type", "application/json")
155✔
99
        http.Error(w, string(msg), e.HTTPstatus)
155✔
100
}
101

102
// Withf returns a copy of Error with the Sprintf formatted string appended at the end of e.Err
103
func (e Error) Withf(format string, args ...any) Error {
71✔
104
        return Error{
71✔
105
                Err:        fmt.Errorf("%w: %v", e.Err, fmt.Sprintf(format, args...)),
71✔
106
                Code:       e.Code,
71✔
107
                HTTPstatus: e.HTTPstatus,
71✔
108
                LogLevel:   e.LogLevel,
71✔
109
        }
71✔
110
}
71✔
111

112
// With returns a copy of Error with the string appended at the end of e.Err
113
func (e Error) With(s string) Error {
17✔
114
        return Error{
17✔
115
                Err:        fmt.Errorf("%w: %v", e.Err, s),
17✔
116
                Code:       e.Code,
17✔
117
                HTTPstatus: e.HTTPstatus,
17✔
118
                LogLevel:   e.LogLevel,
17✔
119
        }
17✔
120
}
17✔
121

122
// WithErr returns a copy of Error with err.Error() appended at the end of e.Err
123
// The original error is preserved for logging purposes
124
func (e Error) WithErr(err error) Error {
7✔
125
        return Error{
7✔
126
                Err:        fmt.Errorf("%w: %v", e.Err, err.Error()),
7✔
127
                Code:       e.Code,
7✔
128
                HTTPstatus: e.HTTPstatus,
7✔
129
                LogLevel:   e.LogLevel,
7✔
130
        }
7✔
131
}
7✔
132

133
// WithLogLevel returns a copy of Error with the specified log level
134
func (e Error) WithLogLevel(level string) Error {
×
135
        return Error{
×
136
                Err:        e.Err,
×
UNCOV
137
                Code:       e.Code,
×
UNCOV
138
                HTTPstatus: e.HTTPstatus,
×
UNCOV
139
                LogLevel:   level,
×
UNCOV
140
        }
×
UNCOV
141
}
×
142

143
func (e Error) WithData(data any) Error {
4✔
144
        return Error{
4✔
145
                Err:        e.Err,
4✔
146
                Code:       e.Code,
4✔
147
                HTTPstatus: e.HTTPstatus,
4✔
148
                LogLevel:   e.LogLevel,
4✔
149
                Data:       data,
4✔
150
        }
4✔
151
}
4✔
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

© 2026 Coveralls, Inc