Skip to content

Commit 63bc7e3

Browse files
committed
Remove usage of archived pkg/errors.
The repository github.com/pkg/errors is no longer maintained. Instead, the package is now part of the Go standard library (pkg.go.dev/errors) and further developed. In this commit we refactor the usages of the errors package to reference the current version.
1 parent d37f4eb commit 63bc7e3

27 files changed

+199
-192
lines changed

armor/armor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ package armor
44

55
import (
66
"bytes"
7+
"fmt"
78
"io"
89
"io/ioutil"
910

1011
"github.com/ProtonMail/go-crypto/openpgp/armor"
1112
"github.com/ProtonMail/gopenpgp/v2/constants"
1213
"github.com/ProtonMail/gopenpgp/v2/internal"
13-
"github.com/pkg/errors"
1414
)
1515

1616
// ArmorKey armors input as a public key.
@@ -46,7 +46,7 @@ func ArmorWithTypeAndCustomHeaders(input []byte, armorType, version, comment str
4646
func Unarmor(input string) ([]byte, error) {
4747
b, err := internal.Unarmor(input)
4848
if err != nil {
49-
return nil, errors.Wrap(err, "gopengp: unable to unarmor")
49+
return nil, fmt.Errorf("gopengp: unable to unarmor: %w", err)
5050
}
5151
return ioutil.ReadAll(b.Body)
5252
}
@@ -57,13 +57,13 @@ func armorWithTypeAndHeaders(input []byte, armorType string, headers map[string]
5757
w, err := armor.Encode(&b, armorType, headers)
5858

5959
if err != nil {
60-
return "", errors.Wrap(err, "gopengp: unable to encode armoring")
60+
return "", fmt.Errorf("gopengp: unable to encode armoring: %w", err)
6161
}
6262
if _, err = w.Write(input); err != nil {
63-
return "", errors.Wrap(err, "gopengp: unable to write armored to buffer")
63+
return "", fmt.Errorf("gopengp: unable to write armored to buffer: %w", err)
6464
}
6565
if err := w.Close(); err != nil {
66-
return "", errors.Wrap(err, "gopengp: unable to close armor buffer")
66+
return "", fmt.Errorf("gopengp: unable to close armor buffer: %w", err)
6767
}
6868
return b.String(), nil
6969
}

crypto/attachment.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package crypto
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"runtime"
@@ -10,7 +11,6 @@ import (
1011

1112
"github.com/ProtonMail/go-crypto/openpgp"
1213
"github.com/ProtonMail/go-crypto/openpgp/packet"
13-
"github.com/pkg/errors"
1414
)
1515

1616
// AttachmentProcessor keeps track of the progress of encrypting an attachment
@@ -41,7 +41,7 @@ func (ap *AttachmentProcessor) Finish() (*PGPSplitMessage, error) {
4141
}
4242

4343
if err := (*ap.w).Close(); err != nil {
44-
return nil, errors.Wrap(err, "gopengpp: unable to close writer")
44+
return nil, fmt.Errorf("gopengpp: unable to close writer: %w", err)
4545
}
4646

4747
if ap.garbageCollector > 0 {
@@ -50,7 +50,7 @@ func (ap *AttachmentProcessor) Finish() (*PGPSplitMessage, error) {
5050
}
5151

5252
if err := (*ap.pipe).Close(); err != nil {
53-
return nil, errors.Wrap(err, "gopengpp: unable to close pipe")
53+
return nil, fmt.Errorf("gopengpp: unable to close pipe: %w", err)
5454
}
5555

5656
ap.done.Wait()
@@ -107,7 +107,7 @@ func (keyRing *KeyRing) newAttachmentProcessor(
107107
var encryptErr error
108108
ew, encryptErr = openpgp.Encrypt(writer, keyRing.entities, nil, hints, config)
109109
if encryptErr != nil {
110-
return nil, errors.Wrap(encryptErr, "gopengpp: unable to encrypt attachment")
110+
return nil, fmt.Errorf("gopengpp: unable to encrypt attachment: %w", encryptErr)
111111
}
112112
attachmentProc.w = &ew
113113
attachmentProc.pipe = writer
@@ -167,13 +167,13 @@ func (keyRing *KeyRing) DecryptAttachment(message *PGPSplitMessage) (*PlainMessa
167167

168168
md, err := openpgp.ReadMessage(encryptedReader, privKeyEntries, nil, config)
169169
if err != nil {
170-
return nil, errors.Wrap(err, "gopengpp: unable to read attachment")
170+
return nil, fmt.Errorf("gopengpp: unable to read attachment: %w", err)
171171
}
172172

173173
decrypted := md.UnverifiedBody
174174
b, err := ioutil.ReadAll(decrypted)
175175
if err != nil {
176-
return nil, errors.Wrap(err, "gopengpp: unable to read attachment body")
176+
return nil, fmt.Errorf("gopengpp: unable to read attachment body: %w", err)
177177
}
178178

179179
return &PlainMessage{

crypto/attachment_manual.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package crypto
22

33
import (
4+
"errors"
5+
"fmt"
46
"io"
57
"io/ioutil"
68
"runtime"
@@ -10,7 +12,6 @@ import (
1012

1113
"github.com/ProtonMail/go-crypto/openpgp"
1214
"github.com/ProtonMail/go-crypto/openpgp/packet"
13-
"github.com/pkg/errors"
1415
)
1516

1617
// ManualAttachmentProcessor keeps track of the progress of encrypting an attachment
@@ -42,7 +43,10 @@ func (ap *ManualAttachmentProcessor) GetDataLength() int {
4243
func (ap *ManualAttachmentProcessor) Process(plainData []byte) error {
4344
defer runtime.GC()
4445
_, err := ap.plaintextWriter.Write(plainData)
45-
return errors.Wrap(err, "gopenpgp: couldn't write attachment data")
46+
if err != nil {
47+
err = fmt.Errorf("gopenpgp: couldn't write attachment data: %w", err)
48+
}
49+
return err
4650
}
4751

4852
// Finish tells the processor to finalize encryption.
@@ -52,10 +56,10 @@ func (ap *ManualAttachmentProcessor) Finish() error {
5256
return ap.err
5357
}
5458
if err := ap.plaintextWriter.Close(); err != nil {
55-
return errors.Wrap(err, "gopengpp: unable to close the plaintext writer")
59+
return fmt.Errorf("gopengpp: unable to close the plaintext writer: %w", err)
5660
}
5761
if err := ap.ciphertextWriter.Close(); err != nil {
58-
return errors.Wrap(err, "gopengpp: unable to close the dataPacket writer")
62+
return fmt.Errorf("gopengpp: unable to close the dataPacket writer: %w", err)
5963
}
6064
ap.done.Wait()
6165
if ap.err != nil {
@@ -130,15 +134,15 @@ func (keyRing *KeyRing) NewManualAttachmentProcessor(
130134
var encryptErr error
131135
ew, encryptErr = openpgp.EncryptSplit(keyWriter, dataWriter, keyRing.entities, nil, hints, config)
132136
if encryptErr != nil {
133-
return nil, errors.Wrap(encryptErr, "gopengpp: unable to encrypt attachment")
137+
return nil, fmt.Errorf("gopengpp: unable to encrypt attachment: %w", encryptErr)
134138
}
135139

136140
attachmentProc.plaintextWriter = ew
137141
attachmentProc.ciphertextWriter = dataWriter
138142

139143
// The key packet should have been already written, so we can close
140144
if err := keyWriter.Close(); err != nil {
141-
return nil, errors.Wrap(err, "gopenpgp: couldn't close the keyPacket writer")
145+
return nil, fmt.Errorf("gopenpgp: couldn't close the keyPacket writer: %w", err)
142146
}
143147

144148
// Check if the goroutines encountered errors
@@ -171,7 +175,7 @@ func readAll(buffer []byte, reader io.Reader) (int, error) {
171175
if errors.Is(err, io.EOF) {
172176
break
173177
}
174-
return 0, errors.Wrap(err, "gopenpgp: couldn't read data from the encrypted reader")
178+
return 0, fmt.Errorf("gopenpgp: couldn't read data from the encrypted reader: %w", err)
175179
}
176180
if offset == bufferLen {
177181
// Here we've reached the end of the buffer

crypto/attachment_manual_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package crypto
22

33
import (
44
"bytes"
5+
"errors"
56
"io"
67
"testing"
7-
8-
"github.com/pkg/errors"
98
)
109

1110
func TestManualAttachmentProcessor(t *testing.T) {

crypto/key.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto"
66
"crypto/sha256"
77
"encoding/hex"
8+
"errors"
89
"fmt"
910
"io"
1011
"math/big"
@@ -13,7 +14,6 @@ import (
1314

1415
"github.com/ProtonMail/gopenpgp/v2/armor"
1516
"github.com/ProtonMail/gopenpgp/v2/constants"
16-
"github.com/pkg/errors"
1717

1818
openpgp "github.com/ProtonMail/go-crypto/openpgp"
1919
packet "github.com/ProtonMail/go-crypto/openpgp/packet"
@@ -117,14 +117,14 @@ func (key *Key) Lock(passphrase []byte) (*Key, error) {
117117
if lockedKey.entity.PrivateKey != nil && !lockedKey.entity.PrivateKey.Dummy() {
118118
err = lockedKey.entity.PrivateKey.Encrypt(passphrase)
119119
if err != nil {
120-
return nil, errors.Wrap(err, "gopenpgp: error in locking key")
120+
return nil, fmt.Errorf("gopenpgp: error in locking key: %w", err)
121121
}
122122
}
123123

124124
for _, sub := range lockedKey.entity.Subkeys {
125125
if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() {
126126
if err := sub.PrivateKey.Encrypt(passphrase); err != nil {
127-
return nil, errors.Wrap(err, "gopenpgp: error in locking sub key")
127+
return nil, fmt.Errorf("gopenpgp: error in locking sub key: %w", err)
128128
}
129129
}
130130
}
@@ -162,14 +162,14 @@ func (key *Key) Unlock(passphrase []byte) (*Key, error) {
162162
if unlockedKey.entity.PrivateKey != nil && !unlockedKey.entity.PrivateKey.Dummy() {
163163
err = unlockedKey.entity.PrivateKey.Decrypt(passphrase)
164164
if err != nil {
165-
return nil, errors.Wrap(err, "gopenpgp: error in unlocking key")
165+
return nil, fmt.Errorf("gopenpgp: error in unlocking key: %w", err)
166166
}
167167
}
168168

169169
for _, sub := range unlockedKey.entity.Subkeys {
170170
if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() {
171171
if err := sub.PrivateKey.Decrypt(passphrase); err != nil {
172-
return nil, errors.Wrap(err, "gopenpgp: error in unlocking sub key")
172+
return nil, fmt.Errorf("gopenpgp: error in unlocking sub key: %w", err)
173173
}
174174
}
175175
}
@@ -198,7 +198,7 @@ func (key *Key) Serialize() ([]byte, error) {
198198
}
199199

200200
if err != nil {
201-
return nil, errors.Wrap(err, "gopenpgp: error in serializing key")
201+
return nil, fmt.Errorf("gopenpgp: error in serializing key: %w", err)
202202
}
203203

204204
return buffer.Bytes(), nil
@@ -254,7 +254,7 @@ func (key *Key) GetArmoredPublicKeyWithCustomHeaders(comment, version string) (s
254254
func (key *Key) GetPublicKey() (b []byte, err error) {
255255
var outBuf bytes.Buffer
256256
if err = key.entity.Serialize(&outBuf); err != nil {
257-
return nil, errors.Wrap(err, "gopenpgp: error in serializing public key")
257+
return nil, fmt.Errorf("gopenpgp: error in serializing public key: %w", err)
258258
}
259259

260260
return outBuf.Bytes(), nil
@@ -417,7 +417,7 @@ func (key *Key) readFrom(r io.Reader, armored bool) error {
417417
entities, err = openpgp.ReadKeyRing(r)
418418
}
419419
if err != nil {
420-
return errors.Wrap(err, "gopenpgp: error in reading key ring")
420+
return fmt.Errorf("gopenpgp: error in reading key ring: %w", err)
421421
}
422422

423423
if len(entities) > 1 {
@@ -439,7 +439,7 @@ func generateKey(
439439
prime1, prime2, prime3, prime4 []byte,
440440
) (*Key, error) {
441441
if len(email) == 0 && len(name) == 0 {
442-
return nil, errors.New("gopenpgp: neither name nor email set.")
442+
return nil, errors.New("gopenpgp: neither name nor email set")
443443
}
444444

445445
comments := ""
@@ -473,7 +473,7 @@ func generateKey(
473473

474474
newEntity, err := openpgp.NewEntity(name, comments, email, cfg)
475475
if err != nil {
476-
return nil, errors.Wrap(err, "gopengpp: error in encoding new entity")
476+
return nil, fmt.Errorf("gopengpp: error in encoding new entity: %w", err)
477477
}
478478

479479
if newEntity.PrivateKey == nil {

crypto/keyring.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package crypto
22

33
import (
44
"bytes"
5+
"errors"
6+
"fmt"
57
"time"
68

79
"github.com/ProtonMail/go-crypto/openpgp"
810
"github.com/ProtonMail/go-crypto/openpgp/packet"
9-
"github.com/pkg/errors"
1011
)
1112

1213
// KeyRing contains multiple private and public keys.
@@ -220,14 +221,14 @@ func (keyRing *KeyRing) Copy() (*KeyRing, error) {
220221
}
221222

222223
if err != nil {
223-
return nil, errors.Wrap(err, "gopenpgp: unable to copy key: error in serializing entity")
224+
return nil, fmt.Errorf("gopenpgp: unable to copy key: error in serializing entity: %w", err)
224225
}
225226

226227
bt := buffer.Bytes()
227228
entities[id], err = openpgp.ReadEntity(packet.NewReader(bytes.NewReader(bt)))
228229

229230
if err != nil {
230-
return nil, errors.Wrap(err, "gopenpgp: unable to copy key: error in reading entity")
231+
return nil, fmt.Errorf("gopenpgp: unable to copy key: error in reading entity: %w", err)
231232
}
232233
}
233234
newKeyRing.entities = entities

crypto/keyring_message.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package crypto
22

33
import (
44
"bytes"
5+
"errors"
6+
"fmt"
57
"io"
68
"io/ioutil"
79
"time"
810

911
"github.com/ProtonMail/go-crypto/openpgp"
1012
"github.com/ProtonMail/go-crypto/openpgp/packet"
1113
"github.com/ProtonMail/gopenpgp/v2/constants"
12-
"github.com/pkg/errors"
1314
)
1415

1516
// Encrypt encrypts a PlainMessage, outputs a PGPMessage.
@@ -219,12 +220,12 @@ func asymmetricEncrypt(
219220

220221
_, err = encryptWriter.Write(plainMessage.GetBinary())
221222
if err != nil {
222-
return nil, errors.Wrap(err, "gopenpgp: error in writing to message")
223+
return nil, fmt.Errorf("gopenpgp: error in writing to message: %w", err)
223224
}
224225

225226
err = encryptWriter.Close()
226227
if err != nil {
227-
return nil, errors.Wrap(err, "gopenpgp: error in closing message")
228+
return nil, fmt.Errorf("gopenpgp: error in closing message: %w", err)
228229
}
229230

230231
return &PGPMessage{outBuf.Bytes()}, nil
@@ -268,7 +269,7 @@ func asymmetricEncryptStream(
268269
encryptWriter, err = openpgp.EncryptTextSplit(keyPacketWriter, dataPacketWriter, publicKey.entities, signEntity, hints, config)
269270
}
270271
if err != nil {
271-
return nil, errors.Wrap(err, "gopenpgp: error in encrypting asymmetrically")
272+
return nil, fmt.Errorf("gopenpgp: error in encrypting asymmetrically: %w", err)
272273
}
273274
return encryptWriter, nil
274275
}
@@ -294,7 +295,7 @@ func asymmetricDecrypt(
294295

295296
body, err := ioutil.ReadAll(messageDetails.UnverifiedBody)
296297
if err != nil {
297-
return nil, errors.Wrap(err, "gopenpgp: error in reading message body")
298+
return nil, fmt.Errorf("gopenpgp: error in reading message body: %w", err)
298299
}
299300

300301
if verifyKey != nil {
@@ -349,7 +350,7 @@ func asymmetricDecryptStream(
349350

350351
messageDetails, err = openpgp.ReadMessage(encryptedIO, privKeyEntries, nil, config)
351352
if err != nil {
352-
return nil, errors.Wrap(err, "gopenpgp: error in reading message")
353+
return nil, fmt.Errorf("gopenpgp: error in reading message: %w", err)
353354
}
354355
return messageDetails, err
355356
}

0 commit comments

Comments
 (0)