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 { if skip, err := skip(err); skip {
t.Skip("skipping:", err) t.Skip("skipping:", err)
} }
if err == zenity.ErrUnsupported { if errors.Is(err, zenity.ErrUnsupported) {
t.Skip("was not unsupported:", err) t.Skip("was not unsupported:", err)
} }
if lst == nil || err != nil { if lst == nil || err != nil {

View file

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

View file

@ -12,7 +12,7 @@ func List(text string, items []string, options ...Option) (string, error) {
// ListItems displays the list dialog. // ListItems displays the list dialog.
// //
// May return: ErrCanceled. // May return: ErrCanceled, ErrUnsupported.
func ListItems(text string, items ...string) (string, error) { func ListItems(text string, items ...string) (string, error) {
return List(text, items) 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. // 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) { func ListMultipleItems(text string, items ...string) ([]string, error) {
return ListMultiple(text, items) return ListMultiple(text, items)
} }

View file

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

View file

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

View file

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

View file

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