Skip to content

Commit

Permalink
Remove usage of archived pkg/errors.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mpass99 committed May 6, 2023
1 parent d37f4eb commit 85165bb
Show file tree
Hide file tree
Showing 27 changed files with 191 additions and 191 deletions.
10 changes: 5 additions & 5 deletions armor/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package armor

import (
"bytes"
"fmt"
"io"
"io/ioutil"

"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/gopenpgp/v2/constants"
"github.com/ProtonMail/gopenpgp/v2/internal"
"github.com/pkg/errors"
)

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

if err != nil {
return "", errors.Wrap(err, "gopengp: unable to encode armoring")
return "", fmt.Errorf("gopengp: unable to encode armoring: %w", err)
}
if _, err = w.Write(input); err != nil {
return "", errors.Wrap(err, "gopengp: unable to write armored to buffer")
return "", fmt.Errorf("gopengp: unable to write armored to buffer: %w", err)
}
if err := w.Close(); err != nil {
return "", errors.Wrap(err, "gopengp: unable to close armor buffer")
return "", fmt.Errorf("gopengp: unable to close armor buffer: %w", err)
}
return b.String(), nil
}
12 changes: 6 additions & 6 deletions crypto/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crypto

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"runtime"
Expand All @@ -10,7 +11,6 @@ import (

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/pkg/errors"
)

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

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

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

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

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

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

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

return &PlainMessage{
Expand Down
15 changes: 8 additions & 7 deletions crypto/attachment_manual.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package crypto

import (
"errors"
"fmt"
"io"
"io/ioutil"
"runtime"
Expand All @@ -10,7 +12,6 @@ import (

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/pkg/errors"
)

// ManualAttachmentProcessor keeps track of the progress of encrypting an attachment
Expand Down Expand Up @@ -42,7 +43,7 @@ func (ap *ManualAttachmentProcessor) GetDataLength() int {
func (ap *ManualAttachmentProcessor) Process(plainData []byte) error {
defer runtime.GC()
_, err := ap.plaintextWriter.Write(plainData)
return errors.Wrap(err, "gopenpgp: couldn't write attachment data")
return fmt.Errorf("gopenpgp: couldn't write attachment data: %w", err)
}

// Finish tells the processor to finalize encryption.
Expand All @@ -52,10 +53,10 @@ func (ap *ManualAttachmentProcessor) Finish() error {
return ap.err
}
if err := ap.plaintextWriter.Close(); err != nil {
return errors.Wrap(err, "gopengpp: unable to close the plaintext writer")
return fmt.Errorf("gopengpp: unable to close the plaintext writer: %w", err)
}
if err := ap.ciphertextWriter.Close(); err != nil {
return errors.Wrap(err, "gopengpp: unable to close the dataPacket writer")
return fmt.Errorf("gopengpp: unable to close the dataPacket writer: %w", err)
}
ap.done.Wait()
if ap.err != nil {
Expand Down Expand Up @@ -130,15 +131,15 @@ func (keyRing *KeyRing) NewManualAttachmentProcessor(
var encryptErr error
ew, encryptErr = openpgp.EncryptSplit(keyWriter, dataWriter, keyRing.entities, nil, hints, config)
if encryptErr != nil {
return nil, errors.Wrap(encryptErr, "gopengpp: unable to encrypt attachment")
return nil, fmt.Errorf("gopengpp: unable to encrypt attachment: %w", encryptErr)
}

attachmentProc.plaintextWriter = ew
attachmentProc.ciphertextWriter = dataWriter

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

// Check if the goroutines encountered errors
Expand Down Expand Up @@ -171,7 +172,7 @@ func readAll(buffer []byte, reader io.Reader) (int, error) {
if errors.Is(err, io.EOF) {
break
}
return 0, errors.Wrap(err, "gopenpgp: couldn't read data from the encrypted reader")
return 0, fmt.Errorf("gopenpgp: couldn't read data from the encrypted reader: %w", err)
}
if offset == bufferLen {
// Here we've reached the end of the buffer
Expand Down
3 changes: 1 addition & 2 deletions crypto/attachment_manual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package crypto

import (
"bytes"
"errors"
"io"
"testing"

"github.com/pkg/errors"
)

func TestManualAttachmentProcessor(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"math/big"
Expand All @@ -13,7 +14,6 @@ import (

"github.com/ProtonMail/gopenpgp/v2/armor"
"github.com/ProtonMail/gopenpgp/v2/constants"
"github.com/pkg/errors"

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

for _, sub := range lockedKey.entity.Subkeys {
if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() {
if err := sub.PrivateKey.Encrypt(passphrase); err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in locking sub key")
return nil, fmt.Errorf("gopenpgp: error in locking sub key: %w", err)
}
}
}
Expand Down Expand Up @@ -162,14 +162,14 @@ func (key *Key) Unlock(passphrase []byte) (*Key, error) {
if unlockedKey.entity.PrivateKey != nil && !unlockedKey.entity.PrivateKey.Dummy() {
err = unlockedKey.entity.PrivateKey.Decrypt(passphrase)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in unlocking key")
return nil, fmt.Errorf("gopenpgp: error in unlocking key: %w", err)
}
}

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

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

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

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

if len(entities) > 1 {
Expand Down Expand Up @@ -473,7 +473,7 @@ func generateKey(

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

if newEntity.PrivateKey == nil {
Expand Down
7 changes: 4 additions & 3 deletions crypto/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package crypto

import (
"bytes"
"errors"
"fmt"
"time"

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/pkg/errors"
)

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

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

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

if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to copy key: error in reading entity")
return nil, fmt.Errorf("gopenpgp: unable to copy key: error in reading entity: %w", err)
}
}
newKeyRing.entities = entities
Expand Down
13 changes: 7 additions & 6 deletions crypto/keyring_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package crypto

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"time"

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/gopenpgp/v2/constants"
"github.com/pkg/errors"
)

// Encrypt encrypts a PlainMessage, outputs a PGPMessage.
Expand Down Expand Up @@ -219,12 +220,12 @@ func asymmetricEncrypt(

_, err = encryptWriter.Write(plainMessage.GetBinary())
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in writing to message")
return nil, fmt.Errorf("gopenpgp: error in writing to message: %w", err)
}

err = encryptWriter.Close()
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in closing message")
return nil, fmt.Errorf("gopenpgp: error in closing message: %w", err)
}

return &PGPMessage{outBuf.Bytes()}, nil
Expand Down Expand Up @@ -268,7 +269,7 @@ func asymmetricEncryptStream(
encryptWriter, err = openpgp.EncryptTextSplit(keyPacketWriter, dataPacketWriter, publicKey.entities, signEntity, hints, config)
}
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in encrypting asymmetrically")
return nil, fmt.Errorf("gopenpgp: error in encrypting asymmetrically: %w", err)
}
return encryptWriter, nil
}
Expand All @@ -294,7 +295,7 @@ func asymmetricDecrypt(

body, err := ioutil.ReadAll(messageDetails.UnverifiedBody)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in reading message body")
return nil, fmt.Errorf("gopenpgp: error in reading message body: %w", err)
}

if verifyKey != nil {
Expand Down Expand Up @@ -349,7 +350,7 @@ func asymmetricDecryptStream(

messageDetails, err = openpgp.ReadMessage(encryptedIO, privKeyEntries, nil, config)
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: error in reading message")
return nil, fmt.Errorf("gopenpgp: error in reading message: %w", err)
}
return messageDetails, err
}
12 changes: 6 additions & 6 deletions crypto/keyring_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package crypto

import (
"bytes"
"errors"
"fmt"
"strconv"

"github.com/pkg/errors"

"github.com/ProtonMail/go-crypto/openpgp/packet"
)

Expand Down Expand Up @@ -55,11 +55,11 @@ Loop:
}

if !hasPacket {
return nil, errors.Wrap(err, "gopenpgp: couldn't find a session key packet")
return nil, fmt.Errorf("gopenpgp: couldn't find a session key packet: %w", err)
}

if decryptErr != nil {
return nil, errors.Wrap(decryptErr, "gopenpgp: error in decrypting")
return nil, fmt.Errorf("gopenpgp: error in decrypting: %w", decryptErr)
}

if ek == nil || ek.Key == nil {
Expand All @@ -75,7 +75,7 @@ func (keyRing *KeyRing) EncryptSessionKey(sk *SessionKey) ([]byte, error) {
outbuf := &bytes.Buffer{}
cf, err := sk.GetCipherFunc()
if err != nil {
return nil, errors.Wrap(err, "gopenpgp: unable to encrypt session key")
return nil, fmt.Errorf("gopenpgp: unable to encrypt session key: %w", err)
}

pubKeys := make([]*packet.PublicKey, 0, len(keyRing.entities))
Expand All @@ -92,7 +92,7 @@ func (keyRing *KeyRing) EncryptSessionKey(sk *SessionKey) ([]byte, error) {

for _, pub := range pubKeys {
if err := packet.SerializeEncryptedKey(outbuf, pub, cf, sk.Key, nil); err != nil {
return nil, errors.Wrap(err, "gopenpgp: cannot set key")
return nil, fmt.Errorf("gopenpgp: cannot set key: %w", err)
}
}
return outbuf.Bytes(), nil
Expand Down
Loading

0 comments on commit 85165bb

Please sign in to comment.