Use fmt.Errorf instead of errors package. This is good convention after go 1.13

This commit is contained in:
Musab Gültekin 2021-04-17 11:11:29 +03:00
parent fbee722a38
commit 9ea67b3554
3 changed files with 10 additions and 11 deletions

View File

@ -2,11 +2,12 @@ package client
import ( import (
"context" "context"
"errors"
"fmt"
"github.com/chromedp/cdproto/dom" "github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/network" "github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
"github.com/geziyor/geziyor/internal" "github.com/geziyor/geziyor/internal"
"github.com/pkg/errors"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
"golang.org/x/text/transform" "golang.org/x/text/transform"
"io" "io"
@ -101,7 +102,7 @@ func (c *Client) DoRequest(req *Request) (resp *Response, err error) {
internal.Logger.Println("Retrying:", req.URL.String()) internal.Logger.Println("Retrying:", req.URL.String())
return c.DoRequest(req) return c.DoRequest(req)
} }
return resp, errors.Wrap(err, "Response error") return resp, err
} }
// Retry on http status codes // Retry on http status codes
@ -124,7 +125,7 @@ func (c *Client) doRequestClient(req *Request) (*Response, error) {
defer resp.Body.Close() defer resp.Body.Close()
} }
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("response: %w", err)
} }
// Limit response body reading // Limit response body reading
@ -138,9 +139,10 @@ func (c *Client) doRequestClient(req *Request) (*Response, error) {
} }
} else { } else {
if !c.opt.CharsetDetectDisabled { if !c.opt.CharsetDetectDisabled {
bodyReader, err = charset.NewReader(bodyReader, req.Header.Get("Content-Type")) contentType := req.Header.Get("Content-Type")
bodyReader, err = charset.NewReader(bodyReader, contentType)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Reading determined encoding error") return nil, fmt.Errorf("charset detection error on content-type %s: %w", contentType, err)
} }
} }
} }
@ -148,7 +150,7 @@ func (c *Client) doRequestClient(req *Request) (*Response, error) {
body, err := ioutil.ReadAll(bodyReader) body, err := ioutil.ReadAll(bodyReader)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Reading body error") return nil, fmt.Errorf("reading body: %w", err)
} }
response := Response{ response := Response{
@ -211,7 +213,7 @@ func (c *Client) doRequestChrome(req *Request) (*Response, error) {
return err return err
}), }),
); err != nil { ); err != nil {
return nil, errors.Wrap(err, "Request getting rendered error") return nil, fmt.Errorf("request getting rendered: %w", err)
} }
// Update changed data // Update changed data
@ -289,7 +291,7 @@ func ConvertMapToHeader(m map[string]interface{}) http.Header {
func NewRedirectionHandler(maxRedirect int) func(req *http.Request, via []*http.Request) error { func NewRedirectionHandler(maxRedirect int) func(req *http.Request, via []*http.Request) error {
return func(req *http.Request, via []*http.Request) error { return func(req *http.Request, via []*http.Request) error {
if len(via) >= maxRedirect { if len(via) >= maxRedirect {
return errors.Errorf("stopped after %d redirects", maxRedirect) return fmt.Errorf("stopped after %d redirects", maxRedirect)
} }
return nil return nil
} }

1
go.mod
View File

@ -11,7 +11,6 @@ require (
github.com/go-kit/kit v0.8.0 github.com/go-kit/kit v0.8.0
github.com/google/btree v1.0.0 // indirect github.com/google/btree v1.0.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible github.com/peterbourgon/diskv v2.0.1+incompatible
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.0.0 github.com/prometheus/client_golang v1.0.0
github.com/stretchr/testify v1.3.0 github.com/stretchr/testify v1.3.0
github.com/syndtr/goleveldb v1.0.0 github.com/syndtr/goleveldb v1.0.0

2
go.sum
View File

@ -63,8 +63,6 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=