style: prefer stdlib idioms for errors and http methods

This commit is contained in:
Mathis Maquenne
2026-08-05 21:43:59 +02:00
parent a695fb4de3
commit c1775e5c04
12 changed files with 50 additions and 44 deletions
+5 -4
View File
@@ -3,6 +3,7 @@ package download
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
@@ -40,7 +41,7 @@ func findTrackURL(ctx context.Context, httpClient *http.Client, artist, title, d
values := neturl.Values{}
values.Add("query", fmt.Sprintf("%s %s", artist, title))
req, err := http.NewRequestWithContext(ctx, "POST", rootURL+"/searches", bytes.NewBufferString(values.Encode()))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, rootURL+"/searches", bytes.NewBufferString(values.Encode()))
if err != nil {
return "", err
}
@@ -102,14 +103,14 @@ func findTrackURL(ctx context.Context, httpClient *http.Client, artist, title, d
})
if matchURL == "" {
return "", fmt.Errorf("no data found")
return "", errors.New("no data found")
}
return rootURL + matchURL, nil
}
func fetchBPMPage(ctx context.Context, httpClient *http.Client, url string) (string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
@@ -138,7 +139,7 @@ func parseBPM(html string) (bpmKey, error) {
modeMatch := modeRegex.FindStringSubmatch(html)
if len(bpmMatch) != 2 || len(keyMatch) != 2 || len(modeMatch) != 2 {
return bpmKey{}, fmt.Errorf("no data found")
return bpmKey{}, errors.New("no data found")
}
bpm := bpmMatch[1]
+3 -2
View File
@@ -2,6 +2,7 @@ package download
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
@@ -50,14 +51,14 @@ func fetchGenre(ctx context.Context, httpClient *http.Client, artist, title stri
filtered := filterTags(tags)
if len(filtered) == 0 {
return "", fmt.Errorf("no data found")
return "", errors.New("no data found")
}
return formatTags(filtered), nil
}
func fetchGenrePage(ctx context.Context, httpClient *http.Client, url string) (*goquery.Document, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
+4 -3
View File
@@ -1,6 +1,7 @@
package download
import (
"errors"
"fmt"
"time"
@@ -27,14 +28,14 @@ func (o *Options) Validate(kind deezer.Kind) error {
return fmt.Errorf("invalid quality option: %s", o.Quality)
}
if o.Timeout <= 0 {
return fmt.Errorf("timeout must be a positive duration")
return errors.New("timeout must be a positive duration")
}
if kind == deezer.KindArtist {
if o.Limit <= 0 {
return fmt.Errorf("limit must be a positive integer")
return errors.New("limit must be a positive integer")
}
if o.Limit > 100 {
return fmt.Errorf("limit must not exceed 100")
return errors.New("limit must not exceed 100")
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ package download
import (
"fmt"
"math/rand"
"math/rand/v2"
"os"
"time"