From 9c94cda0a4053eb61b0d8a5bf2ce64936ceca53a Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 3 May 2022 14:20:22 +0100 Subject: [PATCH] Add context to errors. --- file_test.go | 2 +- file_windows.go | 3 ++- list.go | 4 ++-- list_darwin.go | 16 ++++++++++++---- progress_darwin.go | 4 +++- pwd_stub.go | 4 +++- pwd_test.go | 6 ++++-- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/file_test.go b/file_test.go index d1d5219..af3f4be 100644 --- a/file_test.go +++ b/file_test.go @@ -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 { diff --git a/file_windows.go b/file_windows.go index fa0e526..f120f89 100644 --- a/file_windows.go +++ b/file_windows.go @@ -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) } diff --git a/list.go b/list.go index 8cf47ba..1e72a90 100644 --- a/list.go +++ b/list.go @@ -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) } diff --git a/list_darwin.go b/list_darwin.go index 9e6a93c..e4069c8 100644 --- a/list_darwin.go +++ b/list_darwin.go @@ -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 diff --git a/progress_darwin.go b/progress_darwin.go index 5df70f3..c8e3b82 100644 --- a/progress_darwin.go +++ b/progress_darwin.go @@ -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 diff --git a/pwd_stub.go b/pwd_stub.go index e52a10c..16f5a0f 100644 --- a/pwd_stub.go +++ b/pwd_stub.go @@ -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) diff --git a/pwd_test.go b/pwd_test.go index 0cfb210..bbca524 100644 --- a/pwd_test.go +++ b/pwd_test.go @@ -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 {