@@ -11,6 +11,8 @@ import (
1111 "regexp"
1212 "runtime"
1313 "strings"
14+
15+ "github.com/larksuite/cli/errs"
1416)
1517
1618// readClipboardImageBytes reads the current clipboard image and returns the
@@ -35,13 +37,13 @@ func readClipboardImageBytes() ([]byte, error) {
3537 case "linux" :
3638 data , err = readClipboardLinux ()
3739 default :
38- return nil , fmt . Errorf ( "clipboard image upload is not supported on %s" , runtime .GOOS )
40+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard image upload is not supported on %s" , runtime .GOOS )
3941 }
4042 if err != nil {
4143 return nil , err
4244 }
4345 if len (data ) == 0 {
44- return nil , fmt . Errorf ( "clipboard contains no image data" )
46+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard contains no image data" )
4547 }
4648 return data , nil
4749}
@@ -91,9 +93,9 @@ func readClipboardDarwin() ([]byte, error) {
9193 }
9294
9395 if stderrText != "" {
94- return nil , fmt . Errorf ( "clipboard contains no image data (osascript: %s)" , stderrText )
96+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard contains no image data (osascript: %s)" , stderrText )
9597 }
96- return nil , fmt . Errorf ( "clipboard contains no image data" )
98+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard contains no image data" )
9799}
98100
99101// runOsascript invokes osascript with a single AppleScript expression and
@@ -188,14 +190,14 @@ func decodeOsascriptData(s string) ([]byte, error) {
188190// decodeHex decodes an uppercase hex string (as produced by osascript) to bytes.
189191func decodeHex (h string ) ([]byte , error ) {
190192 if len (h )% 2 != 0 {
191- return nil , fmt .Errorf ("odd hex length" )
193+ return nil , fmt .Errorf ("odd hex length" ) //nolint:forbidigo // intermediate decode helper; result discarded by caller on error
192194 }
193195 b := make ([]byte , len (h )/ 2 )
194196 for i := 0 ; i < len (h ); i += 2 {
195197 hi := hexVal (h [i ])
196198 lo := hexVal (h [i + 1 ])
197199 if hi < 0 || lo < 0 {
198- return nil , fmt .Errorf ("invalid hex char at %d" , i )
200+ return nil , fmt .Errorf ("invalid hex char at %d" , i ) //nolint:forbidigo // intermediate decode helper; result discarded by caller on error
199201 }
200202 b [i / 2 ] = byte (hi << 4 | lo )
201203 }
@@ -237,12 +239,12 @@ $img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
237239 if msg == "" {
238240 msg = err .Error ()
239241 }
240- return nil , fmt . Errorf ( "clipboard read failed (%s)" , msg )
242+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard read failed (%s)" , msg )
241243 }
242244 b64 := strings .TrimSpace (string (out ))
243245 data , decErr := base64 .StdEncoding .DecodeString (b64 )
244246 if decErr != nil {
245- return nil , fmt . Errorf ( "clipboard image decode failed: %w " , decErr )
247+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard image decode failed: %s " , decErr ). WithCause ( decErr )
246248 }
247249 return data , nil
248250}
@@ -325,15 +327,15 @@ func readClipboardLinux() ([]byte, error) {
325327 foundTool = true
326328 out , err := exec .Command (t .name , t .args ... ).Output ()
327329 if err != nil {
328- lastErr = fmt . Errorf ( "clipboard image read failed via %s: %w " , t .name , err )
330+ lastErr = errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard image read failed via %s: %s " , t .name , err ). WithCause ( err )
329331 continue
330332 }
331333 if len (out ) == 0 {
332- lastErr = fmt . Errorf ( "clipboard contains no image data (%s returned empty output)" , t .name )
334+ lastErr = errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard contains no image data (%s returned empty output)" , t .name )
333335 continue
334336 }
335337 if t .validatePNG && ! hasPNGMagic (out ) {
336- lastErr = fmt . Errorf ( "clipboard contains no PNG image data (%s output is not a PNG)" , t .name )
338+ lastErr = errs . NewValidationError ( errs . SubtypeFailedPrecondition , "clipboard contains no PNG image data (%s output is not a PNG)" , t .name )
337339 continue
338340 }
339341 return out , nil
@@ -342,8 +344,8 @@ func readClipboardLinux() ([]byte, error) {
342344 if foundTool && lastErr != nil {
343345 return nil , lastErr
344346 }
345- return nil , fmt . Errorf (
346- "clipboard image read failed: no supported tool found. " +
347- "Install one of xclip, wl-clipboard, or xsel via your distro's package manager " +
347+ return nil , errs . NewValidationError ( errs . SubtypeFailedPrecondition ,
348+ "clipboard image read failed: no supported tool found. " +
349+ "Install one of xclip, wl-clipboard, or xsel via your distro's package manager " +
348350 "(apt, dnf, pacman, apk, brew, etc.)." )
349351}
0 commit comments