Pull request: replace agherr with golibs' errors

Merge in DNS/adguard-home from golibs-errors to master

Squashed commit of the following:

commit 5aba278a31c5a213bd9e08273ce7277c57713b22
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon May 24 17:05:18 2021 +0300

    all: imp code

commit f447eb875b81779fa9e391d98c31c1eeba7ef323
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon May 24 15:33:45 2021 +0300

    replace agherr with golibs' errors
This commit is contained in:
Ainar Garipov
2021-05-24 17:28:11 +03:00
parent 14250821ab
commit 03a828ef51
60 changed files with 406 additions and 672 deletions

View File

@@ -2,7 +2,6 @@
package querylog
import (
"errors"
"fmt"
"net"
"os"
@@ -11,6 +10,7 @@ import (
"time"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)

View File

@@ -8,15 +8,15 @@ import (
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
)
// Timestamp not found errors.
const (
ErrTSNotFound agherr.Error = "ts not found"
ErrTSTooLate agherr.Error = "ts too late"
ErrTSTooEarly agherr.Error = "ts too early"
ErrTSNotFound errors.Error = "ts not found"
ErrTSTooLate errors.Error = "ts too late"
ErrTSTooEarly errors.Error = "ts too early"
)
// TODO: Find a way to grow buffer instead of relying on this value when reading strings

View File

@@ -2,7 +2,6 @@ package querylog
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
@@ -12,6 +11,7 @@ import (
"testing"
"time"
"github.com/AdguardTeam/golibs/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

View File

@@ -1,12 +1,11 @@
package querylog
import (
"errors"
"fmt"
"io"
"os"
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
)
@@ -154,7 +153,7 @@ func closeQFiles(qFiles []*QLogFile) error {
}
if len(errs) > 0 {
return agherr.Many("Error while closing QLogReader", errs...)
return errors.List("error while closing QLogReader", errs...)
}
return nil

View File

@@ -6,8 +6,8 @@ import (
"path/filepath"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
@@ -78,13 +78,13 @@ type AddParams struct {
func (p *AddParams) validate() (err error) {
switch {
case p.Question == nil:
return agherr.Error("question is nil")
return errors.Error("question is nil")
case len(p.Question.Question) != 1:
return agherr.Error("more than one question")
return errors.Error("more than one question")
case len(p.Question.Question[0].Name) == 0:
return agherr.Error("no host in question")
return errors.Error("no host in question")
case p.ClientIP == nil:
return agherr.Error("no client ip")
return errors.Error("no client ip")
default:
return nil
}

View File

@@ -3,10 +3,10 @@ package querylog
import (
"bytes"
"encoding/json"
"errors"
"os"
"time"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
)
@@ -39,7 +39,7 @@ func (l *queryLog) flushLogBuffer(fullFlush bool) error {
}
// flushToFile saves the specified log entries to the query log file
func (l *queryLog) flushToFile(buffer []*logEntry) error {
func (l *queryLog) flushToFile(buffer []*logEntry) (err error) {
if len(buffer) == 0 {
log.Debug("querylog: there's nothing to write to a file")
return nil
@@ -49,9 +49,10 @@ func (l *queryLog) flushToFile(buffer []*logEntry) error {
var b bytes.Buffer
e := json.NewEncoder(&b)
for _, entry := range buffer {
err := e.Encode(entry)
err = e.Encode(entry)
if err != nil {
log.Error("Failed to marshal entry: %s", err)
return err
}
}
@@ -59,7 +60,6 @@ func (l *queryLog) flushToFile(buffer []*logEntry) error {
elapsed := time.Since(start)
log.Debug("%d elements serialized via json in %v: %d kB, %v/entry, %v/entry", len(buffer), elapsed, b.Len()/1024, float64(b.Len())/float64(len(buffer)), elapsed/time.Duration(len(buffer)))
var err error
var zb bytes.Buffer
filename := l.logFile
zb = b
@@ -71,7 +71,7 @@ func (l *queryLog) flushToFile(buffer []*logEntry) error {
log.Error("failed to create file \"%s\": %s", filename, err)
return err
}
defer f.Close()
defer func() { err = errors.WithDeferred(err, f.Close()) }()
n, err := f.Write(zb.Bytes())
if err != nil {
@@ -109,7 +109,12 @@ func (l *queryLog) readFileFirstTimeValue() int64 {
if err != nil {
return -1
}
defer f.Close()
defer func() {
derr := f.Close()
if derr != nil {
log.Error("querylog: closing file: %s", derr)
}
}()
buf := make([]byte, 500)
r, err := f.Read(buf)

View File

@@ -142,7 +142,12 @@ func (l *queryLog) searchFiles(
return entries, oldest, 0
}
defer r.Close()
defer func() {
derr := r.Close()
if derr != nil {
log.Error("querylog: closing file: %s", err)
}
}()
if params.olderThan.IsZero() {
err = r.SeekStart()