Skip to content

Commit 7527590

Browse files
committed
all: use error wrap when possible
1 parent ebe9262 commit 7527590

File tree

11 files changed

+32
-32
lines changed

11 files changed

+32
-32
lines changed

acme/acme.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (c *Client) authorize(ctx context.Context, typ, val string) (*Authorization
373373

374374
var v wireAuthz
375375
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
376-
return nil, fmt.Errorf("acme: invalid response: %v", err)
376+
return nil, fmt.Errorf("acme: invalid response: %w", err)
377377
}
378378
if v.Status != StatusPending && v.Status != StatusValid {
379379
return nil, fmt.Errorf("acme: unexpected status: %s", v.Status)
@@ -397,7 +397,7 @@ func (c *Client) GetAuthorization(ctx context.Context, url string) (*Authorizati
397397
defer res.Body.Close()
398398
var v wireAuthz
399399
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
400-
return nil, fmt.Errorf("acme: invalid response: %v", err)
400+
return nil, fmt.Errorf("acme: invalid response: %w", err)
401401
}
402402
return v.authorization(url), nil
403403
}
@@ -500,7 +500,7 @@ func (c *Client) GetChallenge(ctx context.Context, url string) (*Challenge, erro
500500
defer res.Body.Close()
501501
v := wireChallenge{URI: url}
502502
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
503-
return nil, fmt.Errorf("acme: invalid response: %v", err)
503+
return nil, fmt.Errorf("acme: invalid response: %w", err)
504504
}
505505
return v.challenge(), nil
506506
}
@@ -525,7 +525,7 @@ func (c *Client) Accept(ctx context.Context, chal *Challenge) (*Challenge, error
525525

526526
var v wireChallenge
527527
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
528-
return nil, fmt.Errorf("acme: invalid response: %v", err)
528+
return nil, fmt.Errorf("acme: invalid response: %w", err)
529529
}
530530
return v.challenge(), nil
531531
}

acme/autocert/internal/acmetest/ca.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (ca *CAServer) handle(w http.ResponseWriter, r *http.Request) {
467467
func (ca *CAServer) storedOrder(i string) (*order, error) {
468468
idx, err := strconv.Atoi(i)
469469
if err != nil {
470-
return nil, fmt.Errorf("storedOrder: %v", err)
470+
return nil, fmt.Errorf("storedOrder: %w", err)
471471
}
472472
if idx < 0 {
473473
return nil, fmt.Errorf("storedOrder: invalid order index %d", idx)
@@ -485,7 +485,7 @@ func (ca *CAServer) storedOrder(i string) (*order, error) {
485485
func (ca *CAServer) storedAuthz(i string) (*authorization, error) {
486486
idx, err := strconv.Atoi(i)
487487
if err != nil {
488-
return nil, fmt.Errorf("storedAuthz: %v", err)
488+
return nil, fmt.Errorf("storedAuthz: %w", err)
489489
}
490490
if idx < 0 {
491491
return nil, fmt.Errorf("storedAuthz: invalid authz index %d", idx)
@@ -700,7 +700,7 @@ func (ca *CAServer) verifyALPNChallenge(a *authorization) error {
700700
}
701701

702702
if err := crt.VerifyHostname(a.domain); err != nil {
703-
return fmt.Errorf("verifyALPNChallenge: VerifyHostname: %v", err)
703+
return fmt.Errorf("verifyALPNChallenge: VerifyHostname: %w", err)
704704
}
705705
// See RFC 8737, Section 6.1.
706706
oid := asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 31}

acme/internal/acmeprobe/prober.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (p *prober) fulfill(ctx context.Context, z *acme.Authorization) error {
301301
func (p *prober) runTLSALPN01(ctx context.Context, z *acme.Authorization, chal *acme.Challenge) error {
302302
tokenCert, err := p.client.TLSALPN01ChallengeCert(chal.Token, z.Identifier.Value)
303303
if err != nil {
304-
return fmt.Errorf("TLSALPN01ChallengeCert: %v", err)
304+
return fmt.Errorf("TLSALPN01ChallengeCert: %w", err)
305305
}
306306
s := &http.Server{
307307
Addr: p.localAddr,
@@ -321,7 +321,7 @@ func (p *prober) runTLSALPN01(ctx context.Context, z *acme.Authorization, chal *
321321
defer s.Close()
322322

323323
if _, err := p.client.Accept(ctx, chal); err != nil {
324-
return fmt.Errorf("Accept(%q): %v", chal.URI, err)
324+
return fmt.Errorf("Accept(%q): %w", chal.URI, err)
325325
}
326326
_, zerr := p.client.WaitAuthorization(ctx, z.URI)
327327
return zerr
@@ -330,7 +330,7 @@ func (p *prober) runTLSALPN01(ctx context.Context, z *acme.Authorization, chal *
330330
func (p *prober) runHTTP01(ctx context.Context, z *acme.Authorization, chal *acme.Challenge) error {
331331
body, err := p.client.HTTP01ChallengeResponse(chal.Token)
332332
if err != nil {
333-
return fmt.Errorf("HTTP01ChallengeResponse: %v", err)
333+
return fmt.Errorf("HTTP01ChallengeResponse: %w", err)
334334
}
335335
s := &http.Server{
336336
Addr: p.localAddr,
@@ -347,7 +347,7 @@ func (p *prober) runHTTP01(ctx context.Context, z *acme.Authorization, chal *acm
347347
defer s.Close()
348348

349349
if _, err := p.client.Accept(ctx, chal); err != nil {
350-
return fmt.Errorf("Accept(%q): %v", chal.URI, err)
350+
return fmt.Errorf("Accept(%q): %w", chal.URI, err)
351351
}
352352
_, zerr := p.client.WaitAuthorization(ctx, z.URI)
353353
return zerr
@@ -356,7 +356,7 @@ func (p *prober) runHTTP01(ctx context.Context, z *acme.Authorization, chal *acm
356356
func (p *prober) runDNS01(ctx context.Context, z *acme.Authorization, chal *acme.Challenge) error {
357357
token, err := p.client.DNS01ChallengeRecord(chal.Token)
358358
if err != nil {
359-
return fmt.Errorf("DNS01ChallengeRecord: %v", err)
359+
return fmt.Errorf("DNS01ChallengeRecord: %w", err)
360360
}
361361

362362
name := fmt.Sprintf("_acme-challenge.%s", z.Identifier.Value)
@@ -365,11 +365,11 @@ func (p *prober) runDNS01(ctx context.Context, z *acme.Authorization, chal *acme
365365
cmd.Stdout = os.Stdout
366366
cmd.Stderr = os.Stderr
367367
if err := cmd.Run(); err != nil {
368-
return fmt.Errorf("%s: %v", p.dnsScript, err)
368+
return fmt.Errorf("%s: %w", p.dnsScript, err)
369369
}
370370

371371
if _, err := p.client.Accept(ctx, chal); err != nil {
372-
return fmt.Errorf("Accept(%q): %v", chal.URI, err)
372+
return fmt.Errorf("Accept(%q): %w", chal.URI, err)
373373
}
374374
_, zerr := p.client.WaitAuthorization(ctx, z.URI)
375375
return zerr
@@ -389,7 +389,7 @@ func checkCert(derChain [][]byte, id []acme.AuthzID) error {
389389
for i, b := range derChain {
390390
crt, err := x509.ParseCertificate(b)
391391
if err != nil {
392-
return fmt.Errorf("%d: ParseCertificate: %v", i, err)
392+
return fmt.Errorf("%d: ParseCertificate: %w", i, err)
393393
}
394394
log.Printf("%d: serial: 0x%s", i, crt.SerialNumber)
395395
log.Printf("%d: subject: %s", i, crt.Subject)

acme/rfc8555.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *Client) registerRFC(ctx context.Context, acct *Account, prompt func(tos
6060
if acct.ExternalAccountBinding != nil {
6161
eabJWS, err := c.encodeExternalAccountBinding(acct.ExternalAccountBinding)
6262
if err != nil {
63-
return nil, fmt.Errorf("acme: failed to encode external account binding: %v", err)
63+
return nil, fmt.Errorf("acme: failed to encode external account binding: %w", err)
6464
}
6565
req.ExternalAccountBinding = eabJWS
6666
}
@@ -140,7 +140,7 @@ func responseAccount(res *http.Response) (*Account, error) {
140140
Orders string
141141
}
142142
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
143-
return nil, fmt.Errorf("acme: invalid account response: %v", err)
143+
return nil, fmt.Errorf("acme: invalid account response: %w", err)
144144
}
145145
return &Account{
146146
URI: res.Header.Get("Location"),
@@ -307,7 +307,7 @@ func responseOrder(res *http.Response) (*Order, error) {
307307
Certificate string
308308
}
309309
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
310-
return nil, fmt.Errorf("acme: error reading order: %v", err)
310+
return nil, fmt.Errorf("acme: error reading order: %w", err)
311311
}
312312
o := &Order{
313313
URI: res.Header.Get("Location"),
@@ -391,7 +391,7 @@ func (c *Client) fetchCertRFC(ctx context.Context, url string, bundle bool) ([][
391391
const max = maxCertChainSize + maxCertChainSize/33
392392
b, err := io.ReadAll(io.LimitReader(res.Body, max+1))
393393
if err != nil {
394-
return nil, fmt.Errorf("acme: fetch cert response stream: %v", err)
394+
return nil, fmt.Errorf("acme: fetch cert response stream: %w", err)
395395
}
396396
if len(b) > max {
397397
return nil, errors.New("acme: certificate chain is too big")
@@ -469,7 +469,7 @@ func (c *Client) ListCertAlternates(ctx context.Context, url string) ([]string,
469469
// We don't need the body but we need to discard it so we don't end up
470470
// preventing keep-alive
471471
if _, err := io.Copy(io.Discard, res.Body); err != nil {
472-
return nil, fmt.Errorf("acme: cert alternates response stream: %v", err)
472+
return nil, fmt.Errorf("acme: cert alternates response stream: %w", err)
473473
}
474474
alts := linkHeader(res.Header, "alternate")
475475
return alts, nil

ssh/agent/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ type Key struct {
240240
}
241241

242242
func clientErr(err error) error {
243-
return fmt.Errorf("agent: client error: %v", err)
243+
return fmt.Errorf("agent: client error: %w", err)
244244
}
245245

246246
// String returns the storage form of an agent key with the format, base64
@@ -269,7 +269,7 @@ func (k *Key) Marshal() []byte {
269269
func (k *Key) Verify(data []byte, sig *ssh.Signature) error {
270270
pubKey, err := ssh.ParsePublicKey(k.Blob)
271271
if err != nil {
272-
return fmt.Errorf("agent: bad public key: %v", err)
272+
return fmt.Errorf("agent: bad public key: %w", err)
273273
}
274274
return pubKey.Verify(data, sig)
275275
}

ssh/agent/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func parseRSACert(req []byte) (*AddedKey, error) {
387387
N *big.Int
388388
}
389389
if err := ssh.Unmarshal(cert.Key.Marshal(), &rsaPub); err != nil {
390-
return nil, fmt.Errorf("agent: Unmarshal failed to parse public key: %v", err)
390+
return nil, fmt.Errorf("agent: Unmarshal failed to parse public key: %w", err)
391391
}
392392

393393
if rsaPub.E.BitLen() > 30 {
@@ -431,7 +431,7 @@ func parseDSACert(req []byte) (*AddedKey, error) {
431431
P, Q, G, Y *big.Int
432432
}
433433
if err := ssh.Unmarshal(cert.Key.Marshal(), &w); err != nil {
434-
return nil, fmt.Errorf("agent: Unmarshal failed to parse public key: %v", err)
434+
return nil, fmt.Errorf("agent: Unmarshal failed to parse public key: %w", err)
435435
}
436436

437437
priv := &dsa.PrivateKey{

ssh/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewClientConn(c net.Conn, addr string, config *ClientConfig) (Conn, <-chan
8282

8383
if err := conn.clientHandshake(addr, &fullConf); err != nil {
8484
c.Close()
85-
return nil, nil, nil, fmt.Errorf("ssh: handshake failed: %v", err)
85+
return nil, nil, nil, fmt.Errorf("ssh: handshake failed: %w", err)
8686
}
8787
conn.mux = newMux(conn.transport)
8888
return conn, conn.mux.incomingChannels, conn.mux.incomingRequests, nil

ssh/keys.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ func ParseRawPrivateKeyWithPassphrase(pemBytes, passphrase []byte) (interface{},
11391139
if err == x509.IncorrectPasswordError {
11401140
return nil, err
11411141
}
1142-
return nil, fmt.Errorf("ssh: cannot decode encrypted private keys: %v", err)
1142+
return nil, fmt.Errorf("ssh: cannot decode encrypted private keys: %w", err)
11431143
}
11441144

11451145
switch block.Type {
@@ -1277,7 +1277,7 @@ func parseOpenSSHPrivateKey(key []byte, decrypt openSSHDecryptFunc) (crypto.Priv
12771277
if err, ok := err.(*PassphraseMissingError); ok {
12781278
pub, errPub := ParsePublicKey(w.PubKey)
12791279
if errPub != nil {
1280-
return nil, fmt.Errorf("ssh: failed to parse embedded public key: %v", errPub)
1280+
return nil, fmt.Errorf("ssh: failed to parse embedded public key: %w", errPub)
12811281
}
12821282
err.PublicKey = pub
12831283
}

ssh/knownhosts/knownhosts.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ func (db *hostKeyDB) check(address string, remote net.Addr, remoteKey ssh.Public
333333

334334
host, port, err := net.SplitHostPort(remote.String())
335335
if err != nil {
336-
return fmt.Errorf("knownhosts: SplitHostPort(%s): %v", remote, err)
336+
return fmt.Errorf("knownhosts: SplitHostPort(%s): %w", remote, err)
337337
}
338338

339339
hostToCheck := addr{host, port}
340340
if address != "" {
341341
// Give preference to the hostname if available.
342342
host, port, err := net.SplitHostPort(address)
343343
if err != nil {
344-
return fmt.Errorf("knownhosts: SplitHostPort(%s): %v", address, err)
344+
return fmt.Errorf("knownhosts: SplitHostPort(%s): %w", address, err)
345345
}
346346

347347
hostToCheck = addr{host, port}
@@ -402,7 +402,7 @@ func (db *hostKeyDB) Read(r io.Reader, filename string) error {
402402
}
403403

404404
if err := db.parseLine(line, filename, lineNum); err != nil {
405-
return fmt.Errorf("knownhosts: %s:%d: %v", filename, lineNum, err)
405+
return fmt.Errorf("knownhosts: %s:%d: %w", filename, lineNum, err)
406406
}
407407
}
408408
return scanner.Err()

ssh/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func checkSourceAddress(addr net.Addr, sourceAddrs string) error {
309309
} else {
310310
_, ipNet, err := net.ParseCIDR(sourceAddr)
311311
if err != nil {
312-
return fmt.Errorf("ssh: error parsing source-address restriction %q: %v", sourceAddr, err)
312+
return fmt.Errorf("ssh: error parsing source-address restriction %q: %w", sourceAddr, err)
313313
}
314314

315315
if ipNet.Contains(tcpAddr.IP) {

ssh/tcpip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (c *Client) autoPortListenWorkaround(laddr *net.TCPAddr) (net.Listener, err
8181
return sshListener, err
8282
}
8383
}
84-
return nil, fmt.Errorf("ssh: listen on random port failed after %d tries: %v", tries, err)
84+
return nil, fmt.Errorf("ssh: listen on random port failed after %d tries: %w", tries, err)
8585
}
8686

8787
// RFC 4254 7.1

0 commit comments

Comments
 (0)