diff --git a/_examples/notusingutil/OpenFile.go b/_examples/notusingutil/OpenFile.go index 99dd7af..3f309e6 100644 --- a/_examples/notusingutil/OpenFile.go +++ b/_examples/notusingutil/OpenFile.go @@ -34,14 +34,16 @@ func main() { go func() { time.Sleep(2 * time.Second) if err := openDialog.SetFileName("hello world"); err != nil { - panic(err) + log.Fatal(err) } }() if err := openDialog.Show(); err != nil { log.Fatal(err) } result, err := openDialog.GetResult() - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen file: %s\n", result) diff --git a/_examples/notusingutil/OpenMultipleFiles.go b/_examples/notusingutil/OpenMultipleFiles.go index 67606d7..ea1471d 100644 --- a/_examples/notusingutil/OpenMultipleFiles.go +++ b/_examples/notusingutil/OpenMultipleFiles.go @@ -34,7 +34,9 @@ func main() { log.Fatal(err) } results, err := openMultiDialog.GetResults() - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen file(s): %s\n", results) diff --git a/_examples/notusingutil/PickFolder.go b/_examples/notusingutil/PickFolder.go index 6d185e6..2c2f0ee 100644 --- a/_examples/notusingutil/PickFolder.go +++ b/_examples/notusingutil/PickFolder.go @@ -17,7 +17,9 @@ func main() { log.Fatal(err) } result, err := pickFolderDialog.GetResult() - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen folder: %s\n", result) diff --git a/_examples/notusingutil/SaveFile.go b/_examples/notusingutil/SaveFile.go index 8f8f1a4..31f9eb7 100644 --- a/_examples/notusingutil/SaveFile.go +++ b/_examples/notusingutil/SaveFile.go @@ -34,7 +34,9 @@ func main() { log.Fatal(err) } result, err := saveDialog.GetResult() - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen file: %s\n", result) diff --git a/_examples/usingutil/OpenFile.go b/_examples/usingutil/OpenFile.go index b852964..2555c38 100644 --- a/_examples/usingutil/OpenFile.go +++ b/_examples/usingutil/OpenFile.go @@ -28,7 +28,9 @@ func main() { FileName: "file.txt", DefaultExtension: "txt", }) - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen file: %s\n", result) diff --git a/_examples/usingutil/OpenMultipleFiles.go b/_examples/usingutil/OpenMultipleFiles.go index 1c9973f..cb54650 100644 --- a/_examples/usingutil/OpenMultipleFiles.go +++ b/_examples/usingutil/OpenMultipleFiles.go @@ -28,7 +28,9 @@ func main() { FileName: "file.txt", DefaultExtension: "txt", }) - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen file(s): %s\n", results) diff --git a/_examples/usingutil/PickFolder.go b/_examples/usingutil/PickFolder.go index cd11483..7663d25 100644 --- a/_examples/usingutil/PickFolder.go +++ b/_examples/usingutil/PickFolder.go @@ -12,7 +12,9 @@ func main() { Role: "PickFolderExample", Folder: "C:\\", }) - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen folder: %s\n", result) diff --git a/_examples/usingutil/SaveFile.go b/_examples/usingutil/SaveFile.go index 9d1e9a8..05cd853 100644 --- a/_examples/usingutil/SaveFile.go +++ b/_examples/usingutil/SaveFile.go @@ -28,7 +28,9 @@ func main() { FileName: "image.jpg", DefaultExtension: "jpg", }) - if err != nil { + if err == cfd.ErrorCancelled { + log.Fatal("Dialog was cancelled by the user.") + } else if err != nil { log.Fatal(err) } log.Printf("Chosen file: %s\n", result) diff --git a/cfd/errors.go b/cfd/errors.go new file mode 100644 index 0000000..c097c8e --- /dev/null +++ b/cfd/errors.go @@ -0,0 +1,7 @@ +package cfd + +import "errors" + +var ( + ErrorCancelled = errors.New("cancelled by user") +) diff --git a/cfd/iFileOpenDialog.go b/cfd/iFileOpenDialog.go index 170a9ff..e415f37 100644 --- a/cfd/iFileOpenDialog.go +++ b/cfd/iFileOpenDialog.go @@ -3,8 +3,6 @@ package cfd import ( - "errors" - "fmt" "github.com/go-ole/go-ole" "github.com/harry1453/go-common-file-dialog/util" "syscall" @@ -50,7 +48,8 @@ func (fileOpenDialog *iFileOpenDialog) ShowAndGetResult() (string, error) { return "", err } if isMultiselect { - return "", errors.New("use ShowAndGetResults for open multiple files dialog") // TODO don't repeat usages of errors.New + // We should panic as this error is caused by the developer using the library + panic("use ShowAndGetResults for open multiple files dialog") } if err := fileOpenDialog.Show(); err != nil { return "", err @@ -59,6 +58,14 @@ func (fileOpenDialog *iFileOpenDialog) ShowAndGetResult() (string, error) { } func (fileOpenDialog *iFileOpenDialog) ShowAndGetResults() ([]string, error) { + isMultiselect, err := fileOpenDialog.isMultiselect() + if err != nil { + return nil, err + } + if !isMultiselect { + // We should panic as this error is caused by the developer using the library + panic("use ShowAndGetResult for open single file dialog") + } if err := fileOpenDialog.Show(); err != nil { return nil, err } @@ -75,7 +82,8 @@ func (fileOpenDialog *iFileOpenDialog) GetResult() (string, error) { return "", err } if isMultiselect { - return "", errors.New("use GetResults for open multiple files dialog") + // We should panic as this error is caused by the developer using the library + panic("use GetResults for open multiple files dialog") } return fileOpenDialog.vtbl.getResultString(unsafe.Pointer(fileOpenDialog)) } @@ -103,6 +111,14 @@ func (fileOpenDialog *iFileOpenDialog) SetRole(role string) error { // This should only be callable when the user asks for a multi select because // otherwise they will be given the Dialog interface which does not expose this function. func (fileOpenDialog *iFileOpenDialog) GetResults() ([]string, error) { + isMultiselect, err := fileOpenDialog.isMultiselect() + if err != nil { + return nil, err + } + if !isMultiselect { + // We should panic as this error is caused by the developer using the library + panic("use GetResult for open single file dialog") + } return fileOpenDialog.vtbl.getResultsStrings(unsafe.Pointer(fileOpenDialog)) } @@ -161,7 +177,7 @@ func (vtbl *iFileOpenDialogVtbl) getResultsStrings(objPtr unsafe.Pointer) ([]str return nil, err } if shellItemArray == nil { - return nil, fmt.Errorf("cancelled by user") + return nil, ErrorCancelled } defer shellItemArray.vtbl.release(unsafe.Pointer(shellItemArray)) count, err := shellItemArray.vtbl.getCount(unsafe.Pointer(shellItemArray)) diff --git a/cfd/iShellItemArray.go b/cfd/iShellItemArray.go index a119d57..1f59441 100644 --- a/cfd/iShellItemArray.go +++ b/cfd/iShellItemArray.go @@ -3,7 +3,6 @@ package cfd import ( - "fmt" "github.com/go-ole/go-ole" "syscall" "unsafe" @@ -60,7 +59,7 @@ func (vtbl *iShellItemArrayVtbl) getItemAt(objPtr unsafe.Pointer, index uintptr) return "", err } if shellItem == nil { - return "", fmt.Errorf("cancelled by user") + return "", ErrorCancelled } defer shellItem.vtbl.release(unsafe.Pointer(shellItem)) return shellItem.vtbl.getDisplayName(unsafe.Pointer(shellItem)) diff --git a/cfd/vtblCommonFunc.go b/cfd/vtblCommonFunc.go index 39b1143..27b85ef 100644 --- a/cfd/vtblCommonFunc.go +++ b/cfd/vtblCommonFunc.go @@ -178,7 +178,7 @@ func (vtbl *iFileDialogVtbl) getResultString(objPtr unsafe.Pointer) (string, err return "", err } if shellItem == nil { - return "", fmt.Errorf("cancelled by user") + return "", ErrorCancelled } defer shellItem.vtbl.release(unsafe.Pointer(shellItem)) return shellItem.vtbl.getDisplayName(unsafe.Pointer(shellItem))