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"
"github.com/peterbourgon/ff"
"github.com/pkg/errors"
"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 {
outWriter, err := os.Create(c.outPath)
if err != nil {
return errors.Wrap(err, "creating out path")
return fmt.Errorf("creating out path: %w", err)
}
if 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 {
info, err := os.Stat(path)
if err != nil {
return errors.Wrap(err, "stating asset")
return fmt.Errorf("stating asset: %w", err)
}
if info.IsDir() {
continue
}
data, err := os.Open(path)
if err != nil {
return errors.Wrap(err, "opening asset")
return fmt.Errorf("opening asset: %w", err)
}
defer data.Close()
processAsset(c, &file{

1
go.mod
View File

@@ -23,7 +23,6 @@ require (
github.com/oklog/run v1.1.0
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
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/wader/gormstore v0.0.0-20190302154359-acb787ba3755
golang.org/x/exp v0.0.0-20190121172915-509febef88a4 // indirect

View File

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

View File

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

View File

@@ -3,12 +3,11 @@ package ctrlsubsonic
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"github.com/pkg/errors"
"go.senan.xyz/gonic/server/ctrlbase"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"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")
data, err := json.Marshal(res)
if err != nil {
return errors.Wrap(err, "marshal to json")
return fmt.Errorf("marshal to json: %w", err)
}
ew.write(data)
case "jsonp":
w.Header().Set("Content-Type", "application/javascript")
data, err := json.Marshal(res)
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
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")
data, err := xml.MarshalIndent(res, "", " ")
if err != nil {
return errors.Wrap(err, "marshal to xml")
return fmt.Errorf("marshal to xml: %w", err)
}
ew.write(data)
}

View File

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

View File

@@ -1,12 +1,13 @@
package db
import (
"fmt"
"log"
"net/url"
"os"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"gopkg.in/gormigrate.v1"
)
@@ -56,7 +57,7 @@ func New(path string) (*DB, error) {
url.RawQuery = dbOptions.Encode()
db, err := gorm.Open("sqlite3", url.String())
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.DB().SetMaxOpenConns(dbMaxOpenConns)
@@ -70,7 +71,7 @@ func New(path string) (*DB, error) {
migrationAddAlbumIDX,
))
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
}

View File

@@ -11,7 +11,6 @@ import (
"os/exec"
"github.com/cespare/xxhash"
"github.com/pkg/errors"
)
type Profile struct {
@@ -104,7 +103,7 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat
// create cache file
cacheFile, err := os.Create(cachePath)
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)
// 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)
// run ffmpeg
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
pipeWriter.Close()
if err := cacheFile.Sync(); err != nil {
return errors.Wrapf(err, "flushing %q", cachePath)
return fmt.Errorf("flushing %q: %w", cachePath, err)
}
cacheFile.Close()
return nil

View File

@@ -11,8 +11,6 @@ import (
"strconv"
"time"
"github.com/pkg/errors"
"go.senan.xyz/gonic/server/db"
)
@@ -47,13 +45,13 @@ func makeRequest(method string, params url.Values) (LastFM, error) {
req.URL.RawQuery = params.Encode()
resp, err := client.Do(req)
if err != nil {
return LastFM{}, errors.Wrap(err, "get")
return LastFM{}, fmt.Errorf("get: %w", err)
}
defer resp.Body.Close()
decoder := xml.NewDecoder(resp.Body)
lastfm := LastFM{}
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 {
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))
resp, err := makeRequest("GET", params)
if err != nil {
return "", errors.Wrap(err, "making session GET")
return "", fmt.Errorf("making session GET: %w", err)
}
return resp.Session.Key, nil
}
@@ -109,7 +107,7 @@ func ArtistGetInfo(apiKey string, artist *db.Artist) (Artist, error) {
params.Add("artist", artist.Name)
resp, err := makeRequest("GET", params)
if err != nil {
return Artist{}, errors.Wrap(err, "making artist GET")
return Artist{}, fmt.Errorf("making artist GET: %w", err)
}
return resp.Artist, nil
}

View File

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

View File

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