Add context to errors.

This commit is contained in:
Nuno Cruces 2022-05-03 14:20:22 +01:00
parent 828102067b
commit 9c94cda0a4
7 changed files with 27 additions and 12 deletions

View File

@ -187,7 +187,7 @@ func TestSelectFileMutiple_script(t *testing.T) {
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err == zenity.ErrUnsupported {
if errors.Is(err, zenity.ErrUnsupported) {
t.Skip("was not unsupported:", err)
}
if lst == nil || err != nil {

View File

@ -1,6 +1,7 @@
package zenity
import (
"fmt"
"path/filepath"
"reflect"
"syscall"
@ -199,7 +200,7 @@ func pickFolders(opts options, multi bool) (str string, lst []string, err error)
_IID_IFileOpenDialog, uintptr(unsafe.Pointer(&dialog)))
if int32(hr) < 0 {
if multi {
return "", nil, ErrUnsupported
return "", nil, fmt.Errorf("%w: multiple directory", ErrUnsupported)
}
return browseForFolder(opts)
}

View File

@ -12,7 +12,7 @@ func List(text string, items []string, options ...Option) (string, error) {
// ListItems displays the list dialog.
//
// May return: ErrCanceled.
// May return: ErrCanceled, ErrUnsupported.
func ListItems(text string, items ...string) (string, error) {
return List(text, items)
}
@ -29,7 +29,7 @@ func ListMultiple(text string, items []string, options ...Option) ([]string, err
// ListMultipleItems displays the list dialog, allowing multiple items to be selected.
//
// May return: ErrCanceled.
// May return: ErrCanceled, ErrUnsupported.
func ListMultipleItems(text string, items ...string) ([]string, error) {
return ListMultiple(text, items)
}

View File

@ -1,12 +1,17 @@
package zenity
import (
"fmt"
"github.com/ncruces/zenity/internal/zenutil"
)
func list(text string, items []string, opts options) (string, error) {
if len(items) == 0 || opts.extraButton != nil {
return "", ErrUnsupported
if len(items) == 0 {
return "", fmt.Errorf("%w: empty items list", ErrUnsupported)
}
if opts.extraButton != nil {
return "", fmt.Errorf("%w: extra button", ErrUnsupported)
}
var data zenutil.List
@ -23,8 +28,11 @@ func list(text string, items []string, opts options) (string, error) {
}
func listMultiple(text string, items []string, opts options) ([]string, error) {
if len(items) == 0 || opts.extraButton != nil {
return nil, ErrUnsupported
if len(items) == 0 {
return nil, fmt.Errorf("%w: empty items list", ErrUnsupported)
}
if opts.extraButton != nil {
return nil, fmt.Errorf("%w: extra button", ErrUnsupported)
}
var data zenutil.List

View File

@ -1,12 +1,14 @@
package zenity
import (
"fmt"
"github.com/ncruces/zenity/internal/zenutil"
)
func progress(opts options) (ProgressDialog, error) {
if opts.extraButton != nil {
return nil, ErrUnsupported
return nil, fmt.Errorf("%w: extra button", ErrUnsupported)
}
var data zenutil.Progress

View File

@ -2,9 +2,11 @@
package zenity
import "fmt"
func password(opts options) (string, string, error) {
if opts.username {
return "", "", ErrUnsupported
return "", "", fmt.Errorf("%w: username", ErrUnsupported)
}
opts.hideText = true
str, err := entry("Password:", opts)

View File

@ -61,7 +61,9 @@ func TestPassword_username(t *testing.T) {
t.Skip("skipping:", err)
}
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
if err != zenity.ErrUnsupported {
if errors.Is(err, zenity.ErrUnsupported) {
t.Skip("was not unsupported:", err)
} else {
t.Error("was not unsupported:", err)
}
} else {
@ -92,7 +94,7 @@ func TestPassword_script(t *testing.T) {
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if err == zenity.ErrUnsupported {
if errors.Is(err, zenity.ErrUnsupported) {
t.Skip("was not unsupported:", err)
}
if usr != tt.usr || pwd != tt.pwd || err != tt.err {