all: Modernize error wrapping (#8491)
This replaces old style errors.Wrap with modern fmt.Errorf and removes the (direct) dependency on github.com/pkg/errors. A couple of cases are adjusted by hand as previously errors.Wrap(nil, ...) would return nil, which is not what fmt.Errorf does.
This commit is contained in:
+11
-11
@@ -14,13 +14,13 @@ import (
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/rand"
|
||||
)
|
||||
|
||||
@@ -90,7 +90,7 @@ func SecureDefaultWithTLS12() *tls.Config {
|
||||
func generateCertificate(commonName string, lifetimeDays int) (*pem.Block, *pem.Block, error) {
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "generate key")
|
||||
return nil, nil, fmt.Errorf("generate key: %w", err)
|
||||
}
|
||||
|
||||
notBefore := time.Now().Truncate(24 * time.Hour)
|
||||
@@ -117,13 +117,13 @@ func generateCertificate(commonName string, lifetimeDays int) (*pem.Block, *pem.
|
||||
|
||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "create cert")
|
||||
return nil, nil, fmt.Errorf("create cert: %w", err)
|
||||
}
|
||||
|
||||
certBlock := &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}
|
||||
keyBlock, err := pemBlockForKey(priv)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "save key")
|
||||
return nil, nil, fmt.Errorf("save key: %w", err)
|
||||
}
|
||||
|
||||
return certBlock, keyBlock, nil
|
||||
@@ -138,24 +138,24 @@ func NewCertificate(certFile, keyFile string, commonName string, lifetimeDays in
|
||||
|
||||
certOut, err := os.Create(certFile)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
||||
return tls.Certificate{}, fmt.Errorf("save cert: %w", err)
|
||||
}
|
||||
if err = pem.Encode(certOut, certBlock); err != nil {
|
||||
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
||||
return tls.Certificate{}, fmt.Errorf("save cert: %w", err)
|
||||
}
|
||||
if err = certOut.Close(); err != nil {
|
||||
return tls.Certificate{}, errors.Wrap(err, "save cert")
|
||||
return tls.Certificate{}, fmt.Errorf("save cert: %w", err)
|
||||
}
|
||||
|
||||
keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %w", err)
|
||||
}
|
||||
if err = pem.Encode(keyOut, keyBlock); err != nil {
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %w", err)
|
||||
}
|
||||
if err = keyOut.Close(); err != nil {
|
||||
return tls.Certificate{}, errors.Wrap(err, "save key")
|
||||
return tls.Certificate{}, fmt.Errorf("save key: %w", err)
|
||||
}
|
||||
|
||||
return tls.X509KeyPair(pem.EncodeToMemory(certBlock), pem.EncodeToMemory(keyBlock))
|
||||
|
||||
Reference in New Issue
Block a user