Skip to content

Commit 17bf097

Browse files
committed
Update uses of errors.Error
1 parent a3b5ea0 commit 17bf097

File tree

23 files changed

+96
-116
lines changed

23 files changed

+96
-116
lines changed

auth/jwt.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ func (s *JWTService) GenerateTokenWithClaims(claims jwt.MapClaims, signingMethod
118118
return "", err
119119
}
120120
result, err := token.SignedString(key)
121-
if err != nil {
122-
err = errorutil.New(err)
123-
}
124-
return result, err
121+
return result, errorutil.New(err)
125122
}
126123

127124
// GetKey load a JWT signature key from the config.
@@ -159,10 +156,8 @@ func (s *JWTService) GetKey(entry string) (any, error) {
159156

160157
if err == nil {
161158
s.cache.Store(entry, key)
162-
} else {
163-
err = errorutil.New(err)
164159
}
165-
return key, err
160+
return key, errorutil.New(err)
166161
}
167162

168163
// GetPrivateKey loads the private key that corresponds to the given `signingMethod`.

config/config.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (l *loader) register(key string, entry Entry) {
7979
category, entryKey, exists := walk(l.defaults, key)
8080
if exists {
8181
if !reflect.DeepEqual(&entry, category[entryKey].(*Entry)) {
82-
panic(errors.New(fmt.Errorf("attempted to override registered config entry %q", key)))
82+
panic(errors.Errorf("attempted to override registered config entry %q", key))
8383
}
8484
} else {
8585
category[entryKey] = &entry
@@ -188,10 +188,8 @@ func (l *loader) readConfigFile(filesystem fs.FS, file string) (o object, err er
188188
}
189189
}()
190190
jsonParser := json.NewDecoder(configFile)
191-
err = jsonParser.Decode(&o)
192-
}
193-
194-
if err != nil {
191+
err = errors.New(jsonParser.Decode(&o))
192+
} else {
195193
err = errors.New(err)
196194
}
197195

@@ -243,7 +241,7 @@ func walk(currentCategory object, key string) (object, string, bool) {
243241
currentCategory = category
244242
} else {
245243
if dotIndex < len(key) {
246-
panic(errors.New(fmt.Errorf("attempted to add an entry to non-category %q", key[:dotIndex])))
244+
panic(errors.Errorf("attempted to add an entry to non-category %q", key[:dotIndex]))
247245
}
248246

249247
// Entry exists
@@ -263,7 +261,7 @@ func walk(currentCategory object, key string) (object, string, bool) {
263261
}
264262
}
265263

266-
panic(errors.New(fmt.Errorf("attempted to replace the %q category with an entry", key)))
264+
panic(errors.Errorf("attempted to replace the %q category with an entry", key))
267265
}
268266

269267
// createMissingCategories based on the key path, starting at the given index.
@@ -355,7 +353,7 @@ func (c *Config) Get(key string) any {
355353
return val
356354
}
357355

358-
panic(errors.New(fmt.Errorf("config entry \"%s\" doesn't exist", key)))
356+
panic(errors.Errorf("config entry \"%s\" doesn't exist", key))
359357
}
360358

361359
func (c *Config) get(key string) (any, bool) {
@@ -396,7 +394,7 @@ func (c *Config) get(key string) (any, bool) {
396394
func (c *Config) GetString(key string) string {
397395
str, ok := c.Get(key).(string)
398396
if !ok {
399-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not a string", key)))
397+
panic(errors.Errorf("config entry \"%s\" is not a string", key))
400398
}
401399
return str
402400
}
@@ -406,7 +404,7 @@ func (c *Config) GetString(key string) string {
406404
func (c *Config) GetBool(key string) bool {
407405
val, ok := c.Get(key).(bool)
408406
if !ok {
409-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not a bool", key)))
407+
panic(errors.Errorf("config entry \"%s\" is not a bool", key))
410408
}
411409
return val
412410
}
@@ -416,7 +414,7 @@ func (c *Config) GetBool(key string) bool {
416414
func (c *Config) GetInt(key string) int {
417415
val, ok := c.Get(key).(int)
418416
if !ok {
419-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not an int", key)))
417+
panic(errors.Errorf("config entry \"%s\" is not an int", key))
420418
}
421419
return val
422420
}
@@ -426,7 +424,7 @@ func (c *Config) GetInt(key string) int {
426424
func (c *Config) GetFloat(key string) float64 {
427425
val, ok := c.Get(key).(float64)
428426
if !ok {
429-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not a float64", key)))
427+
panic(errors.Errorf("config entry \"%s\" is not a float64", key))
430428
}
431429
return val
432430
}
@@ -436,7 +434,7 @@ func (c *Config) GetFloat(key string) float64 {
436434
func (c *Config) GetStringSlice(key string) []string {
437435
str, ok := c.Get(key).([]string)
438436
if !ok {
439-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not a string slice", key)))
437+
panic(errors.Errorf("config entry \"%s\" is not a string slice", key))
440438
}
441439
return str
442440
}
@@ -446,7 +444,7 @@ func (c *Config) GetStringSlice(key string) []string {
446444
func (c *Config) GetBoolSlice(key string) []bool {
447445
str, ok := c.Get(key).([]bool)
448446
if !ok {
449-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not a bool slice", key)))
447+
panic(errors.Errorf("config entry \"%s\" is not a bool slice", key))
450448
}
451449
return str
452450
}
@@ -456,7 +454,7 @@ func (c *Config) GetBoolSlice(key string) []bool {
456454
func (c *Config) GetIntSlice(key string) []int {
457455
str, ok := c.Get(key).([]int)
458456
if !ok {
459-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not an int slice", key)))
457+
panic(errors.Errorf("config entry \"%s\" is not an int slice", key))
460458
}
461459
return str
462460
}
@@ -466,7 +464,7 @@ func (c *Config) GetIntSlice(key string) []int {
466464
func (c *Config) GetFloatSlice(key string) []float64 {
467465
str, ok := c.Get(key).([]float64)
468466
if !ok {
469-
panic(errors.New(fmt.Errorf("config entry \"%s\" is not a float64 slice", key)))
467+
panic(errors.Errorf("config entry \"%s\" is not a float64 slice", key))
470468
}
471469
return str
472470
}

config/entry.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package config
22

33
import (
4-
"fmt"
54
"os"
65
"reflect"
76
"strconv"
@@ -54,7 +53,7 @@ func (e *Entry) validate(key string) error {
5453
message = "%q type must be %s"
5554
}
5655

57-
return errors.New(fmt.Errorf(message, key, e.Type))
56+
return errors.Errorf(message, key, e.Type)
5857
}
5958

6059
if len(e.AuthorizedValues) > 0 {
@@ -65,11 +64,11 @@ func (e *Entry) validate(key string) error {
6564
length := list.Len()
6665
for i := 0; i < length; i++ {
6766
if !lo.Contains(e.AuthorizedValues, list.Index(i).Interface()) {
68-
return errors.New(fmt.Errorf("%q elements must have one of the following values: %v", key, e.AuthorizedValues))
67+
return errors.Errorf("%q elements must have one of the following values: %v", key, e.AuthorizedValues)
6968
}
7069
}
7170
} else if !lo.Contains(e.AuthorizedValues, e.Value) {
72-
return errors.New(fmt.Errorf("%q must have one of the following values: %v", key, e.AuthorizedValues))
71+
return errors.Errorf("%q must have one of the following values: %v", key, e.AuthorizedValues)
7372
}
7473
}
7574

@@ -150,7 +149,7 @@ func (e *Entry) tryEnvVarConversion(key string) error {
150149
if err == nil && val != nil {
151150

152151
if e.IsSlice {
153-
return errors.New(fmt.Errorf("%q is a slice entry, it cannot be loaded from env", key))
152+
return errors.Errorf("%q is a slice entry, it cannot be loaded from env", key)
154153
}
155154

156155
e.Value = val
@@ -166,25 +165,25 @@ func (e *Entry) convertEnvVar(str, key string) (any, error) {
166165
varName := str[2 : len(str)-1]
167166
value, set := os.LookupEnv(varName)
168167
if !set {
169-
return nil, errors.New(fmt.Errorf("%q: %q environment variable is not set", key, varName))
168+
return nil, errors.Errorf("%q: %q environment variable is not set", key, varName)
170169
}
171170

172171
switch e.Type {
173172
case reflect.Int:
174173
if i, err := strconv.Atoi(value); err == nil {
175174
return i, nil
176175
}
177-
return nil, errors.New(fmt.Errorf("%q could not be converted to int from environment variable %q of value %q", key, varName, value))
176+
return nil, errors.Errorf("%q could not be converted to int from environment variable %q of value %q", key, varName, value)
178177
case reflect.Float64:
179178
if f, err := strconv.ParseFloat(value, 64); err == nil {
180179
return f, nil
181180
}
182-
return nil, errors.New(fmt.Errorf("%q could not be converted to float64 from environment variable %q of value %q", key, varName, value))
181+
return nil, errors.Errorf("%q could not be converted to float64 from environment variable %q of value %q", key, varName, value)
183182
case reflect.Bool:
184183
if b, err := strconv.ParseBool(value); err == nil {
185184
return b, nil
186185
}
187-
return nil, errors.New(fmt.Errorf("%q could not be converted to bool from environment variable %q of value %q", key, varName, value))
186+
return nil, errors.Errorf("%q could not be converted to bool from environment variable %q of value %q", key, varName, value)
188187
default:
189188
// Keep value as string if type is not supported and let validation do its job
190189
return value, nil

database/database.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package database
22

33
import (
44
"errors"
5-
"fmt"
65
"time"
76

87
"gorm.io/gorm"
@@ -27,12 +26,12 @@ func New(cfg *config.Config, logger func() *slog.Logger) (*gorm.DB, error) {
2726
driver := cfg.GetString("database.connection")
2827

2928
if driver == "none" {
30-
return nil, fmt.Errorf("Cannot create DB connection. Database is set to \"none\" in the config")
29+
return nil, errorutil.Errorf("Cannot create DB connection. Database is set to \"none\" in the config")
3130
}
3231

3332
dialect, ok := dialects[driver]
3433
if !ok {
35-
return nil, fmt.Errorf("DB Connection %q not supported, forgotten import?", driver)
34+
return nil, errorutil.Errorf("DB Connection %q not supported, forgotten import?", driver)
3635
}
3736

3837
dsn := dialect.buildDSN(cfg)
@@ -90,7 +89,7 @@ func initTimeoutPlugin(cfg *config.Config, db *gorm.DB) error {
9089
ReadTimeout: time.Duration(cfg.GetInt("database.defaultReadQueryTimeout")) * time.Millisecond,
9190
WriteTimeout: time.Duration(cfg.GetInt("database.defaultWriteQueryTimeout")) * time.Millisecond,
9291
}
93-
return db.Use(timeoutPlugin)
92+
return errorutil.New(db.Use(timeoutPlugin))
9493
}
9594

9695
func initSQLDB(cfg *config.Config, db *gorm.DB) {

database/dialect.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
54
"strconv"
65
"strings"
76
"sync"
@@ -64,7 +63,7 @@ func RegisterDialect(name, template string, initializer DialectorInitializer) {
6463
mu.Lock()
6564
defer mu.Unlock()
6665
if _, ok := dialects[name]; ok {
67-
panic(errors.New(fmt.Errorf("dialect %q already exists", name)))
66+
panic(errors.Errorf("dialect %q already exists", name))
6867
}
6968
dialects[name] = dialect{initializer, template}
7069
}

lang/lang.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package lang
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"strings"
76

87
"github.com/samber/lo"
@@ -86,7 +85,7 @@ func (l *Languages) Load(fs fsutil.FS, language, path string) error {
8685
return l.load(fs, language, path)
8786
}
8887

89-
return errors.New(fmt.Errorf("failed loading language \"%s\", directory \"%s\" doesn't exist or is not readable", language, path))
88+
return errors.Errorf("failed loading language \"%s\", directory \"%s\" doesn't exist or is not readable", language, path)
9089
}
9190

9291
func (l *Languages) load(fs fsutil.FS, lang string, path string) error {

log/log.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ func (w *Writer) PreWrite(b []byte) {
6969
func (w *Writer) Write(b []byte) (int, error) {
7070
w.length += len(b)
7171
n, err := w.writer.Write(b)
72-
if err != nil {
73-
err = errors.New(err)
74-
}
75-
return n, err
72+
return n, errors.New(err)
7673
}
7774

7875
// Close the writer and its child ResponseWriter, flushing response

middleware/compress/compress.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@ func (w *compressWriter) PreWrite(b []byte) {
4646

4747
func (w *compressWriter) Write(b []byte) (int, error) {
4848
n, err := w.WriteCloser.Write(b)
49-
if err != nil {
50-
err = errors.New(err)
51-
}
52-
return n, err
49+
return n, errors.New(err)
5350
}
5451

5552
func (w *compressWriter) Close() error {
56-
err := w.WriteCloser.Close()
53+
err := errors.New(w.WriteCloser.Close())
5754

5855
if wr, ok := w.childWriter.(io.Closer); ok {
59-
return wr.Close()
56+
return errors.New(wr.Close())
6057
}
6158

6259
return err

response.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ func (r *Response) PreWrite(b []byte) {
9090
func (r *Response) Write(data []byte) (int, error) {
9191
r.PreWrite(data)
9292
n, err := r.writer.Write(data)
93-
if err != nil {
94-
err = errorutil.New(err)
95-
}
96-
return n, err
93+
return n, errorutil.New(err)
9794
}
9895

9996
// WriteHeader sends an HTTP response header with the provided
@@ -144,10 +141,8 @@ func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
144141
c, b, e := hijacker.Hijack()
145142
if e == nil {
146143
r.hijacked = true
147-
} else {
148-
e = errorutil.New(e)
149144
}
150-
return c, b, e
145+
return c, b, errorutil.New(e)
151146
}
152147

153148
// Hijacked returns true if the underlying connection has been successfully hijacked
@@ -177,7 +172,7 @@ func (r *Response) SetWriter(writer io.Writer) {
177172

178173
func (r *Response) close() error {
179174
if wr, ok := r.writer.(io.Closer); ok {
180-
return wr.Close()
175+
return errorutil.New(wr.Close())
181176
}
182177
return nil
183178
}

route.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (r *Route) BuildURI(parameters ...string) string {
209209
fullURI, fullParameters := r.GetFullURIAndParameters()
210210

211211
if len(parameters) != len(fullParameters) {
212-
panic(errors.New(fmt.Errorf("BuildURI: route has %d parameters, %d given", len(fullParameters), len(parameters))))
212+
panic(errors.Errorf("BuildURI: route has %d parameters, %d given", len(fullParameters), len(parameters)))
213213
}
214214

215215
var builder strings.Builder

0 commit comments

Comments
 (0)