Skip to content

Commit 94688b2

Browse files
committed
refactor(misc): replace fmt.Errorf with errors.New where possible
No need to to invoke the whole Printf machinery for constant strings. While this shouldn't have an impact on memory consumption nor allocation (as constructing errors to return is never in a hot path), this should reduce a bit the code size, as errors.New will be inlined to a simple struct initialization instead of a function call.
1 parent bf7f55e commit 94688b2

File tree

33 files changed

+78
-52
lines changed

33 files changed

+78
-52
lines changed

internal/cli/reset_password.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cli // import "miniflux.app/v2/internal/cli"
55

66
import (
7+
"errors"
78
"fmt"
89

910
"miniflux.app/v2/internal/model"
@@ -19,7 +20,7 @@ func resetPassword(store *storage.Storage) {
1920
}
2021

2122
if user == nil {
22-
printErrorAndExit(fmt.Errorf("user not found"))
23+
printErrorAndExit(errors.New("user not found"))
2324
}
2425

2526
userModificationRequest := &model.UserModificationRequest{

internal/config/parser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bufio"
88
"bytes"
99
"crypto/rand"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"net/url"
@@ -60,7 +61,7 @@ func (cp *configParser) postParsing() error {
6061

6162
scheme := strings.ToLower(parsedURL.Scheme)
6263
if scheme != "https" && scheme != "http" {
63-
return fmt.Errorf("BASE_URL scheme must be http or https")
64+
return errors.New("BASE_URL scheme must be http or https")
6465
}
6566

6667
cp.options.options["BASE_URL"].ParsedStringValue = baseURL
@@ -294,7 +295,7 @@ func readSecretFileValue(filename string) (string, error) {
294295

295296
value := string(bytes.TrimSpace(data))
296297
if value == "" {
297-
return "", fmt.Errorf("secret file is empty")
298+
return "", errors.New("secret file is empty")
298299
}
299300

300301
return value, nil

internal/config/validators.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package config // import "miniflux.app/v2/internal/config"
55

66
import (
7+
"errors"
78
"fmt"
89
"slices"
910
"strconv"
@@ -29,7 +30,7 @@ func validateListChoices(inputValues, choices []string) error {
2930
func validateGreaterThan(rawValue string, min int) error {
3031
intValue, err := strconv.Atoi(rawValue)
3132
if err != nil {
32-
return fmt.Errorf("value must be an integer")
33+
return errors.New("value must be an integer")
3334
}
3435
if intValue > min {
3536
return nil
@@ -40,7 +41,7 @@ func validateGreaterThan(rawValue string, min int) error {
4041
func validateGreaterOrEqualThan(rawValue string, min int) error {
4142
intValue, err := strconv.Atoi(rawValue)
4243
if err != nil {
43-
return fmt.Errorf("value must be an integer")
44+
return errors.New("value must be an integer")
4445
}
4546
if intValue >= min {
4647
return nil
@@ -51,7 +52,7 @@ func validateGreaterOrEqualThan(rawValue string, min int) error {
5152
func validateRange(rawValue string, min, max int) error {
5253
intValue, err := strconv.Atoi(rawValue)
5354
if err != nil {
54-
return fmt.Errorf("value must be an integer")
55+
return errors.New("value must be an integer")
5556
}
5657
if intValue < min || intValue > max {
5758
return fmt.Errorf("value must be between %d and %d", min, max)

internal/googlereader/handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ func checkOutputFormat(r *http.Request) error {
126126
output = request.QueryStringParam(r, "output", "")
127127
}
128128
if output != "json" {
129-
err := fmt.Errorf("googlereader: only json output is supported")
130-
return err
129+
return errors.New("googlereader: only json output is supported")
131130
}
132131
return nil
133132
}

internal/integration/apprise/apprise.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package apprise
66
import (
77
"bytes"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"log/slog"
1112
"net/http"
@@ -29,7 +30,7 @@ func NewClient(serviceURL, baseURL string) *Client {
2930

3031
func (c *Client) SendNotification(feed *model.Feed, entries model.Entries) error {
3132
if c.baseURL == "" || c.servicesURL == "" {
32-
return fmt.Errorf("apprise: missing base URL or services URL")
33+
return errors.New("apprise: missing base URL or services URL")
3334
}
3435

3536
for _, entry := range entries {

internal/integration/espial/espial.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package espial // import "miniflux.app/v2/internal/integration/espial"
66
import (
77
"bytes"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"net/http"
1112
"time"
@@ -27,7 +28,7 @@ func NewClient(baseURL, apiKey string) *Client {
2728

2829
func (c *Client) CreateLink(entryURL, entryTitle, espialTags string) error {
2930
if c.baseURL == "" || c.apiKey == "" {
30-
return fmt.Errorf("espial: missing base URL or API key")
31+
return errors.New("espial: missing base URL or API key")
3132
}
3233

3334
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/add")

internal/integration/instapaper/instapaper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package instapaper // import "miniflux.app/v2/internal/integration/instapaper"
55

66
import (
7+
"errors"
78
"fmt"
89
"net/http"
910
"net/url"
@@ -25,7 +26,7 @@ func NewClient(username, password string) *Client {
2526

2627
func (c *Client) AddURL(entryURL, entryTitle string) error {
2728
if c.username == "" || c.password == "" {
28-
return fmt.Errorf("instapaper: missing username or password")
29+
return errors.New("instapaper: missing username or password")
2930
}
3031

3132
values := url.Values{}

internal/integration/linkace/linkace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package linkace
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net/http"
89
"strings"
@@ -28,7 +29,7 @@ func NewClient(baseURL, apiKey, tags string, private bool, checkDisabled bool) *
2829

2930
func (c *Client) AddURL(entryURL, entryTitle string) error {
3031
if c.baseURL == "" || c.apiKey == "" {
31-
return fmt.Errorf("linkace: missing base URL or API key")
32+
return errors.New("linkace: missing base URL or API key")
3233
}
3334

3435
tagsSplitFn := func(c rune) bool {

internal/integration/linkding/linkding.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package linkding // import "miniflux.app/v2/internal/integration/linkding"
66
import (
77
"bytes"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"net/http"
1112
"strings"
@@ -30,7 +31,7 @@ func NewClient(baseURL, apiKey, tags string, unread bool) *Client {
3031

3132
func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
3233
if c.baseURL == "" || c.apiKey == "" {
33-
return fmt.Errorf("linkding: missing base URL or API key")
34+
return errors.New("linkding: missing base URL or API key")
3435
}
3536

3637
tagsSplitFn := func(c rune) bool {

internal/integration/linktaco/linktaco.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package linktaco // import "miniflux.app/v2/internal/integration/linktaco"
66
import (
77
"bytes"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"net/http"
1112
"strings"
@@ -44,7 +45,7 @@ func NewClient(apiToken, orgSlug, tags, visibility string) *Client {
4445

4546
func (c *Client) CreateBookmark(entryURL, entryTitle, entryContent string) error {
4647
if c.apiToken == "" || c.orgSlug == "" {
47-
return fmt.Errorf("linktaco: missing API token or organization slug")
48+
return errors.New("linktaco: missing API token or organization slug")
4849
}
4950

5051
description := entryContent

0 commit comments

Comments
 (0)