Skip to content

Commit

Permalink
pgp: further improve import error format
Browse files Browse the repository at this point in the history
Looking more at this, it would actually be great if we would detect
multi-line errors from GnuPG in `Import()`, `Decrypt()` and `Encrypt()`
so that we can slightly improve the formatting of the errors with a
newline seperator before the `gpg: ...\ngpg: ...` output. As this would
likely increase readability.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Oct 9, 2023
1 parent d148ddf commit d021b6b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions pgp/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ func (d GnuPGHome) Import(armoredKey []byte) error {
}

args := []string{"--batch", "--import"}
_, stderrBuf, err := gpgExec(d.String(), args, bytes.NewReader(armoredKey))
_, stderr, err := gpgExec(d.String(), args, bytes.NewReader(armoredKey))
if err != nil {
stderr := stderrBuf.String()
stderrStr := strings.TrimSpace(stderr.String())
errStr := err.Error()
var sb strings.Builder
sb.WriteString("failed to import armored key data into GnuPG keyring")
if len(stderr) > 0 {
fmt.Fprintf(&sb, ": %s", stderr)
if len(stderrStr) > 0 {
if len(errStr) > 0 {
fmt.Fprintf(&sb, ": %s", errStr)
fmt.Fprintf(&sb, " (%s)", errStr)
}
fmt.Fprintf(&sb, ": %s", stderrStr)
} else if len(errStr) > 0 {
fmt.Fprintf(&sb, ": %s", errStr)
}
Expand Down
2 changes: 1 addition & 1 deletion pgp/keysource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestGnuPGHome_Import(t *testing.T) {

err = gnuPGHome.Import([]byte("invalid armored data"))
assert.Error(t, err)
assert.ErrorContains(t, err, "gpg: no valid OpenPGP data found.\ngpg: Total number processed: 0\n: exit status 2")
assert.ErrorContains(t, err, "(exit status 2): gpg: no valid OpenPGP data found.\ngpg: Total number processed: 0")
assert.Error(t, GnuPGHome("").Import(b))
}

Expand Down

0 comments on commit d021b6b

Please sign in to comment.