move from pkg/errors to go1.13 errors

This commit is contained in:
sentriz
2020-05-01 21:32:56 +01:00
parent d583c2fbd8
commit 31b2b65ea6
11 changed files with 37 additions and 36 deletions

View File

@@ -10,7 +10,6 @@ import (
"time" "time"
"github.com/peterbourgon/ff" "github.com/peterbourgon/ff"
"github.com/pkg/errors"
"go.senan.xyz/gonic/version" "go.senan.xyz/gonic/version"
) )
@@ -81,7 +80,7 @@ func processAsset(c *config, f *file, out io.Writer) {
func processAssets(c *config, files []string) error { func processAssets(c *config, files []string) error {
outWriter, err := os.Create(c.outPath) outWriter, err := os.Create(c.outPath)
if err != nil { if err != nil {
return errors.Wrap(err, "creating out path") return fmt.Errorf("creating out path: %w", err)
} }
if c.tagList != "" { if c.tagList != "" {
c.tagList = fmt.Sprintf("+build %s", c.tagList) c.tagList = fmt.Sprintf("+build %s", c.tagList)
@@ -95,14 +94,14 @@ func processAssets(c *config, files []string) error {
for _, path := range files { for _, path := range files {
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil { if err != nil {
return errors.Wrap(err, "stating asset") return fmt.Errorf("stating asset: %w", err)
} }
if info.IsDir() { if info.IsDir() {
continue continue
} }
data, err := os.Open(path) data, err := os.Open(path)
if err != nil { if err != nil {
return errors.Wrap(err, "opening asset") return fmt.Errorf("opening asset: %w", err)
} }
defer data.Close() defer data.Close()
processAsset(c, &file{ processAsset(c, &file{

1
go.mod
View File

@@ -23,7 +23,6 @@ require (
github.com/oklog/run v1.1.0 github.com/oklog/run v1.1.0
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/peterbourgon/ff v1.2.0 github.com/peterbourgon/ff v1.2.0
github.com/pkg/errors v0.8.1
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be
github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755 github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755
golang.org/x/exp v0.0.0-20190121172915-509febef88a4 // indirect golang.org/x/exp v0.0.0-20190121172915-509febef88a4 // indirect

View File

@@ -1,6 +1,8 @@
package assets package assets
import "strings" import (
"strings"
)
// PrefixDo runs a given callback for every path in our assets with // PrefixDo runs a given callback for every path in our assets with
// the given prefix // the given prefix

View File

@@ -8,7 +8,6 @@ import (
"strings" "strings"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/pkg/errors"
"go.senan.xyz/gonic/server/db" "go.senan.xyz/gonic/server/db"
) )
@@ -28,7 +27,7 @@ func playlistParseLine(c *Controller, path string) (int, error) {
case gorm.IsRecordNotFoundError(err): case gorm.IsRecordNotFoundError(err):
return 0, fmt.Errorf("couldn't match track %q", path) return 0, fmt.Errorf("couldn't match track %q", path)
case err != nil: case err != nil:
return 0, errors.Wrap(err, "while matching") return 0, fmt.Errorf("while matching: %w", err)
default: default:
return track.ID, nil return track.ID, nil
} }

View File

@@ -3,12 +3,11 @@ package ctrlsubsonic
import ( import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"github.com/pkg/errors"
"go.senan.xyz/gonic/server/ctrlbase" "go.senan.xyz/gonic/server/ctrlbase"
"go.senan.xyz/gonic/server/ctrlsubsonic/params" "go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec" "go.senan.xyz/gonic/server/ctrlsubsonic/spec"
@@ -55,14 +54,14 @@ func writeResp(w http.ResponseWriter, r *http.Request, resp *spec.Response) erro
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(res) data, err := json.Marshal(res)
if err != nil { if err != nil {
return errors.Wrap(err, "marshal to json") return fmt.Errorf("marshal to json: %w", err)
} }
ew.write(data) ew.write(data)
case "jsonp": case "jsonp":
w.Header().Set("Content-Type", "application/javascript") w.Header().Set("Content-Type", "application/javascript")
data, err := json.Marshal(res) data, err := json.Marshal(res)
if err != nil { if err != nil {
return errors.Wrap(err, "marshal to jsonp") return fmt.Errorf("marshal to jsonp: %w", err)
} }
// TODO: error if no callback provided instead of using a default // TODO: error if no callback provided instead of using a default
pCall := params.GetOr("callback", "cb") pCall := params.GetOr("callback", "cb")
@@ -74,7 +73,7 @@ func writeResp(w http.ResponseWriter, r *http.Request, resp *spec.Response) erro
w.Header().Set("Content-Type", "application/xml") w.Header().Set("Content-Type", "application/xml")
data, err := xml.MarshalIndent(res, "", " ") data, err := xml.MarshalIndent(res, "", " ")
if err != nil { if err != nil {
return errors.Wrap(err, "marshal to xml") return fmt.Errorf("marshal to xml: %w", err)
} }
ew.write(data) ew.write(data)
} }

View File

@@ -1,6 +1,8 @@
package spec package spec
import "go.senan.xyz/gonic/server/db" import (
"go.senan.xyz/gonic/server/db"
)
func NewPlaylist(p *db.Playlist) *Playlist { func NewPlaylist(p *db.Playlist) *Playlist {
return &Playlist{ return &Playlist{

View File

@@ -1,12 +1,13 @@
package db package db
import ( import (
"fmt"
"log" "log"
"net/url" "net/url"
"os" "os"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/pkg/errors"
"gopkg.in/gormigrate.v1" "gopkg.in/gormigrate.v1"
) )
@@ -56,7 +57,7 @@ func New(path string) (*DB, error) {
url.RawQuery = dbOptions.Encode() url.RawQuery = dbOptions.Encode()
db, err := gorm.Open("sqlite3", url.String()) db, err := gorm.Open("sqlite3", url.String())
if err != nil { if err != nil {
return nil, errors.Wrap(err, "with gorm") return nil, fmt.Errorf("with gorm: %w", err)
} }
db.SetLogger(log.New(os.Stdout, "gorm ", 0)) db.SetLogger(log.New(os.Stdout, "gorm ", 0))
db.DB().SetMaxOpenConns(dbMaxOpenConns) db.DB().SetMaxOpenConns(dbMaxOpenConns)
@@ -70,7 +71,7 @@ func New(path string) (*DB, error) {
migrationAddAlbumIDX, migrationAddAlbumIDX,
)) ))
if err = migr.Migrate(); err != nil { if err = migr.Migrate(); err != nil {
return nil, errors.Wrap(err, "migrating to latest version") return nil, fmt.Errorf("migrating to latest version: %w", err)
} }
return &DB{DB: db}, nil return &DB{DB: db}, nil
} }

View File

@@ -11,7 +11,6 @@ import (
"os/exec" "os/exec"
"github.com/cespare/xxhash" "github.com/cespare/xxhash"
"github.com/pkg/errors"
) )
type Profile struct { type Profile struct {
@@ -104,7 +103,7 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat
// create cache file // create cache file
cacheFile, err := os.Create(cachePath) cacheFile, err := os.Create(cachePath)
if err != nil { if err != nil {
return errors.Wrapf(err, "writing to cache file %q: %v", cachePath, err) return fmt.Errorf("writing to cache file %q: %v: %w", cachePath, err, err)
} }
// still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput) // still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput)
// initial goal here is to start streaming response asap, with smallest ttfb. more testing needed // initial goal here is to start streaming response asap, with smallest ttfb. more testing needed
@@ -114,12 +113,12 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat
go writeCmdOutput(out, cacheFile, pipeReader) go writeCmdOutput(out, cacheFile, pipeReader)
// run ffmpeg // run ffmpeg
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "running ffmpeg") return fmt.Errorf("running ffmpeg: %w", err)
} }
// close all pipes and flush cache file // close all pipes and flush cache file
pipeWriter.Close() pipeWriter.Close()
if err := cacheFile.Sync(); err != nil { if err := cacheFile.Sync(); err != nil {
return errors.Wrapf(err, "flushing %q", cachePath) return fmt.Errorf("flushing %q: %w", cachePath, err)
} }
cacheFile.Close() cacheFile.Close()
return nil return nil

View File

@@ -11,8 +11,6 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/pkg/errors"
"go.senan.xyz/gonic/server/db" "go.senan.xyz/gonic/server/db"
) )
@@ -47,13 +45,13 @@ func makeRequest(method string, params url.Values) (LastFM, error) {
req.URL.RawQuery = params.Encode() req.URL.RawQuery = params.Encode()
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return LastFM{}, errors.Wrap(err, "get") return LastFM{}, fmt.Errorf("get: %w", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
decoder := xml.NewDecoder(resp.Body) decoder := xml.NewDecoder(resp.Body)
lastfm := LastFM{} lastfm := LastFM{}
if err = decoder.Decode(&lastfm); err != nil { if err = decoder.Decode(&lastfm); err != nil {
return LastFM{}, errors.Wrap(err, "decoding") return LastFM{}, fmt.Errorf("decoding: %w", err)
} }
if lastfm.Error.Code != 0 { if lastfm.Error.Code != 0 {
return LastFM{}, fmt.Errorf("parsing: %v", lastfm.Error.Value) return LastFM{}, fmt.Errorf("parsing: %v", lastfm.Error.Value)
@@ -69,7 +67,7 @@ func GetSession(apiKey, secret, token string) (string, error) {
params.Add("api_sig", getParamSignature(params, secret)) params.Add("api_sig", getParamSignature(params, secret))
resp, err := makeRequest("GET", params) resp, err := makeRequest("GET", params)
if err != nil { if err != nil {
return "", errors.Wrap(err, "making session GET") return "", fmt.Errorf("making session GET: %w", err)
} }
return resp.Session.Key, nil return resp.Session.Key, nil
} }
@@ -109,7 +107,7 @@ func ArtistGetInfo(apiKey string, artist *db.Artist) (Artist, error) {
params.Add("artist", artist.Name) params.Add("artist", artist.Name)
resp, err := makeRequest("GET", params) resp, err := makeRequest("GET", params)
if err != nil { if err != nil {
return Artist{}, errors.Wrap(err, "making artist GET") return Artist{}, fmt.Errorf("making artist GET: %w", err)
} }
return resp.Artist, nil return resp.Artist, nil
} }

View File

@@ -1,6 +1,8 @@
package lastfm package lastfm
import "encoding/xml" import (
"encoding/xml"
)
type LastFM struct { type LastFM struct {
XMLName xml.Name `xml:"lfm"` XMLName xml.Name `xml:"lfm"`

View File

@@ -1,6 +1,8 @@
package scanner package scanner
import ( import (
"errors"
"fmt"
"log" "log"
"os" "os"
"path" "path"
@@ -12,7 +14,6 @@ import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/karrick/godirwalk" "github.com/karrick/godirwalk"
"github.com/pkg/errors"
"github.com/rainycape/unidecode" "github.com/rainycape/unidecode"
"go.senan.xyz/gonic/server/db" "go.senan.xyz/gonic/server/db"
@@ -96,7 +97,7 @@ func (s *Scanner) cleanTracks() (int, error) {
Pluck("id", &previous). Pluck("id", &previous).
Error Error
if err != nil { if err != nil {
return 0, errors.Wrap(err, "plucking ids") return 0, fmt.Errorf("plucking ids: %w", err)
} }
for _, prev := range previous { for _, prev := range previous {
if _, ok := s.seenTracks[prev]; !ok { if _, ok := s.seenTracks[prev]; !ok {
@@ -117,7 +118,7 @@ func (s *Scanner) cleanFolders() (int, error) {
Pluck("id", &previous). Pluck("id", &previous).
Error Error
if err != nil { if err != nil {
return 0, errors.Wrap(err, "plucking ids") return 0, fmt.Errorf("plucking ids: %w", err)
} }
for _, prev := range previous { for _, prev := range previous {
if _, ok := s.seenFolders[prev]; !ok { if _, ok := s.seenFolders[prev]; !ok {
@@ -171,11 +172,11 @@ func (s *Scanner) Start(opts ScanOptions) error {
Unsorted: true, Unsorted: true,
FollowSymbolicLinks: true, FollowSymbolicLinks: true,
ErrorCallback: func(s string, err error) godirwalk.ErrorAction { ErrorCallback: func(s string, err error) godirwalk.ErrorAction {
return return godirwalk.SkipNode
}, },
}) })
if err != nil { if err != nil {
return errors.Wrap(err, "walking filesystem") return fmt.Errorf("walking filesystem: %w", err)
} }
log.Printf("finished scan in %s, +%d/%d tracks (%d err)\n", log.Printf("finished scan in %s, +%d/%d tracks (%d err)\n",
durSince(start), durSince(start),
@@ -235,11 +236,11 @@ var coverFilenames = map[string]struct{}{
func (s *Scanner) callbackItem(fullPath string, info *godirwalk.Dirent) error { func (s *Scanner) callbackItem(fullPath string, info *godirwalk.Dirent) error {
stat, err := os.Stat(fullPath) stat, err := os.Stat(fullPath)
if err != nil { if err != nil {
return errors.Wrap(err, "stating") return fmt.Errorf("stating: %w", err)
} }
relPath, err := filepath.Rel(s.musicPath, fullPath) relPath, err := filepath.Rel(s.musicPath, fullPath)
if err != nil { if err != nil {
return errors.Wrap(err, "getting relative path") return fmt.Errorf("getting relative path: %w", err)
} }
directory, filename := path.Split(relPath) directory, filename := path.Split(relPath)
it := &item{ it := &item{
@@ -251,7 +252,7 @@ func (s *Scanner) callbackItem(fullPath string, info *godirwalk.Dirent) error {
} }
isDir, err := info.IsDirOrSymlinkToDir() isDir, err := info.IsDirOrSymlinkToDir()
if err != nil { if err != nil {
return errors.Wrap(err, "stating link to dir") return fmt.Errorf("stating link to dir: %w", err)
} }
if isDir { if isDir {
return s.handleFolder(it) return s.handleFolder(it)