From 6c48c7faa9c16f826fc435505103e35039b8125e Mon Sep 17 00:00:00 2001 From: Scott Kipfer Date: Mon, 8 Mar 2021 10:17:43 -0500 Subject: [PATCH 1/2] use meaningful error message when clipboard format is unavailable --- clipboard_windows.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clipboard_windows.go b/clipboard_windows.go index 253bb93..e9f288d 100644 --- a/clipboard_windows.go +++ b/clipboard_windows.go @@ -7,6 +7,7 @@ package clipboard import ( + "errors" "runtime" "syscall" "time" @@ -56,8 +57,8 @@ func readAll() (string, error) { // Otherwise if the goroutine switch thread during execution (which is a common practice), the OpenClipboard and CloseClipboard will happen on two different threads, and it will result in a clipboard deadlock. runtime.LockOSThread() defer runtime.UnlockOSThread() - if formatAvailable, _, err := isClipboardFormatAvailable.Call(cfUnicodetext); formatAvailable == 0 { - return "", err + if formatAvailable, _, _ := isClipboardFormatAvailable.Call(cfUnicodetext); formatAvailable == 0 { + return "", errors.New("clipboard format is unavailable") } err := waitOpenClipboard() if err != nil { From c91ae685af2e15389fdd9306db664a7321f449e0 Mon Sep 17 00:00:00 2001 From: Scott Kipfer Date: Mon, 8 Mar 2021 10:26:24 -0500 Subject: [PATCH 2/2] export format unavailable error --- clipboard_windows.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clipboard_windows.go b/clipboard_windows.go index e9f288d..0ea5568 100644 --- a/clipboard_windows.go +++ b/clipboard_windows.go @@ -34,6 +34,8 @@ var ( globalLock = kernel32.NewProc("GlobalLock") globalUnlock = kernel32.NewProc("GlobalUnlock") lstrcpy = kernel32.NewProc("lstrcpyW") + + ErrFormatUnavailable = errors.New("clipboard format is unavailable") ) // waitOpenClipboard opens the clipboard, waiting for up to a second to do so. @@ -58,7 +60,7 @@ func readAll() (string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() if formatAvailable, _, _ := isClipboardFormatAvailable.Call(cfUnicodetext); formatAvailable == 0 { - return "", errors.New("clipboard format is unavailable") + return "", ErrFormatUnavailable } err := waitOpenClipboard() if err != nil {