Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of archived pkg/errors. #238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 11 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,10 @@ 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")
if err != nil {
err = fmt.Errorf("gopenpgp: couldn't write attachment data: %w", err)
}
return err
}

// Finish tells the processor to finalize encryption.
Expand All @@ -52,10 +56,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 +134,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 +175,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
20 changes: 10 additions & 10 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 All @@ -439,7 +439,7 @@ func generateKey(
prime1, prime2, prime3, prime4 []byte,
) (*Key, error) {
if len(email) == 0 && len(name) == 0 {
return nil, errors.New("gopenpgp: neither name nor email set.")
return nil, errors.New("gopenpgp: neither name nor email set")
}

comments := ""
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
}
Loading