Skip to content

Commit 90ef44c

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 90ef44c

File tree

37 files changed

+88
-60
lines changed

37 files changed

+88
-60
lines changed

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (c *Client) UserByID(userID int64) (*User, error) {
125125

126126
// UserByUsername returns a single user.
127127
func (c *Client) UserByUsername(username string) (*User, error) {
128-
body, err := c.request.Get(fmt.Sprintf("/v1/users/%s", username))
128+
body, err := c.request.Get("/v1/users/" + username)
129129
if err != nil {
130130
return nil, err
131131
}

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: 4 additions & 5 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
}
@@ -280,7 +279,7 @@ func (h *handler) editTagHandler(w http.ResponseWriter, r *http.Request) {
280279
return
281280
}
282281
if len(addTags) == 0 && len(removeTags) == 0 {
283-
err = fmt.Errorf("googlreader: add or/and remove tags should be supplied")
282+
err = errors.New("googlreader: add or/and remove tags should be supplied")
284283
json.ServerError(w, r, err)
285284
return
286285
}
@@ -1014,7 +1013,7 @@ func (h *handler) userInfoHandler(w http.ResponseWriter, r *http.Request) {
10141013
json.ServerError(w, r, err)
10151014
return
10161015
}
1017-
userInfo := userInfoResponse{UserID: fmt.Sprint(user.ID), UserName: user.Username, UserProfileID: fmt.Sprint(user.ID), UserEmail: user.Username}
1016+
userInfo := userInfoResponse{UserID: strconv.FormatInt(user.ID, 10), UserName: user.Username, UserProfileID: strconv.FormatInt(user.ID, 10), UserEmail: user.Username}
10181017
json.OK(w, r, userInfo)
10191018
}
10201019

@@ -1048,7 +1047,7 @@ func (h *handler) streamItemIDsHandler(w http.ResponseWriter, r *http.Request) {
10481047
)
10491048

10501049
if len(rm.Streams) != 1 {
1051-
json.ServerError(w, r, fmt.Errorf("googlereader: only one stream type expected"))
1050+
json.ServerError(w, r, errors.New("googlereader: only one stream type expected"))
10521051
return
10531052
}
10541053
switch rm.Streams[0].Type {

internal/googlereader/item.go

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

66
import (
7+
"errors"
78
"fmt"
89
"net/http"
910
"strconv"
@@ -58,7 +59,7 @@ func parseItemID(itemIDValue string) (int64, error) {
5859
func parseItemIDsFromRequest(r *http.Request) ([]int64, error) {
5960
items := r.Form[paramItemIDs]
6061
if len(items) == 0 {
61-
return nil, fmt.Errorf("googlereader: no items requested")
62+
return nil, errors.New("googlereader: no items requested")
6263
}
6364

6465
itemIDs := make([]int64, len(items))

internal/googlereader/request_modifier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (r RequestModifiers) String() string {
4949

5050
results = append(results, fmt.Sprintf("Count: %d", r.Count))
5151
results = append(results, fmt.Sprintf("Offset: %d", r.Offset))
52-
results = append(results, fmt.Sprintf("Sort Direction: %s", r.SortDirection))
53-
results = append(results, fmt.Sprintf("Continuation Token: %s", r.ContinuationToken))
52+
results = append(results, "Sort Direction: "+r.SortDirection)
53+
results = append(results, "Continuation Token: "+r.ContinuationToken)
5454
results = append(results, fmt.Sprintf("Start Time: %d", r.StartTime))
5555
results = append(results, fmt.Sprintf("Stop Time: %d", r.StopTime))
5656

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{}

0 commit comments

Comments
 (0)