@@ -6,7 +6,6 @@ package markdown
66import (
77 "bytes"
88 "context"
9- "errors"
109 "fmt"
1110 "io"
1211 "net/http"
@@ -19,7 +18,6 @@ import (
1918
2019 "github.com/larksuite/cli/errs"
2120 "github.com/larksuite/cli/internal/client"
22- "github.com/larksuite/cli/internal/output"
2321 "github.com/larksuite/cli/internal/validate"
2422 "github.com/larksuite/cli/shortcuts/common"
2523)
@@ -85,16 +83,18 @@ func (spec markdownUploadSpec) Target() markdownUploadTarget {
8583func validateMarkdownSpec (runtime * common.RuntimeContext , spec markdownUploadSpec , requireName bool ) error {
8684 switch {
8785 case spec .ContentSet && spec .FileSet :
88- return common .FlagErrorf ("--content and --file are mutually exclusive" )
86+ return markdownValidationError ("--content and --file are mutually exclusive" ).
87+ WithParams (markdownInvalidParam ("--content" , "mutually exclusive" ), markdownInvalidParam ("--file" , "mutually exclusive" ))
8988 case ! spec .ContentSet && ! spec .FileSet :
90- return common .FlagErrorf ("specify exactly one of --content or --file" )
89+ return markdownValidationError ("specify exactly one of --content or --file" ).
90+ WithParams (markdownInvalidParam ("--content" , "required; specify exactly one" ), markdownInvalidParam ("--file" , "required; specify exactly one" ))
9191 }
9292
9393 if markdownFlagExplicitlyEmpty (runtime , "folder-token" ) {
94- return common . FlagErrorf ( "--folder-token cannot be empty; omit it to upload into Drive root folder" )
94+ return markdownValidationParamError ( "--folder-token" , "--folder-token cannot be empty; omit it to upload into Drive root folder" )
9595 }
9696 if markdownFlagExplicitlyEmpty (runtime , "wiki-token" ) {
97- return common . FlagErrorf ( "--wiki-token cannot be empty; provide a valid wiki node token or omit the flag entirely" )
97+ return markdownValidationParamError ( "--wiki-token" , "--wiki-token cannot be empty; provide a valid wiki node token or omit the flag entirely" )
9898 }
9999 targets := 0
100100 if spec .FolderToken != "" {
@@ -104,22 +104,23 @@ func validateMarkdownSpec(runtime *common.RuntimeContext, spec markdownUploadSpe
104104 targets ++
105105 }
106106 if targets > 1 {
107- return common .FlagErrorf ("--folder-token and --wiki-token are mutually exclusive" )
107+ return markdownValidationError ("--folder-token and --wiki-token are mutually exclusive" ).
108+ WithParams (markdownInvalidParam ("--folder-token" , "mutually exclusive" ), markdownInvalidParam ("--wiki-token" , "mutually exclusive" ))
108109 }
109110 if spec .FolderToken != "" {
110111 if err := validate .ResourceName (spec .FolderToken , "--folder-token" ); err != nil {
111- return output . ErrValidation ( " %s" , err )
112+ return markdownValidationParamError ( "--folder-token" , " %s" , err ). WithCause ( err )
112113 }
113114 }
114115 if spec .WikiToken != "" {
115116 if err := validate .ResourceName (spec .WikiToken , "--wiki-token" ); err != nil {
116- return output . ErrValidation ( " %s" , err )
117+ return markdownValidationParamError ( "--wiki-token" , " %s" , err ). WithCause ( err )
117118 }
118119 }
119120
120121 if requireName && spec .ContentSet {
121122 if strings .TrimSpace (spec .FileName ) == "" {
122- return common . FlagErrorf ( "--name is required when using --content" )
123+ return markdownValidationParamError ( "--name" , "--name is required when using --content" )
123124 }
124125 if err := validateMarkdownFileName (spec .FileName , "--name" ); err != nil {
125126 return err
@@ -128,10 +129,10 @@ func validateMarkdownSpec(runtime *common.RuntimeContext, spec markdownUploadSpe
128129
129130 if spec .FileSet {
130131 if strings .TrimSpace (spec .FilePath ) == "" {
131- return common . FlagErrorf ( "--file cannot be empty" )
132+ return markdownValidationParamError ( "--file" , "--file cannot be empty" )
132133 }
133134 if _ , err := validate .SafeInputPath (spec .FilePath ); err != nil {
134- return output . ErrValidation ( " unsafe file path: %s" , err )
135+ return markdownValidationParamError ( "--file" , " unsafe file path: %s" , err ). WithCause ( err )
135136 }
136137 if err := validateMarkdownFileName (filepath .Base (spec .FilePath ), "--file" ); err != nil {
137138 return err
@@ -154,10 +155,10 @@ func markdownFlagExplicitlyEmpty(runtime *common.RuntimeContext, flagName string
154155func validateMarkdownFileName (name , flagName string ) error {
155156 trimmed := strings .TrimSpace (name )
156157 if trimmed == "" {
157- return common . FlagErrorf ( "%s cannot be empty" , flagName )
158+ return markdownValidationParamError ( flagName , "%s cannot be empty" , flagName )
158159 }
159160 if ! strings .HasSuffix (strings .ToLower (trimmed ), ".md" ) {
160- return common . FlagErrorf ( "%s must end with .md" , flagName )
161+ return markdownValidationParamError ( flagName , "%s must end with .md" , flagName )
161162 }
162163 return nil
163164}
@@ -201,22 +202,9 @@ func openMarkdownDownload(ctx context.Context, runtime *common.RuntimeContext, f
201202 return resp , nil
202203}
203204
204- func wrapMarkdownDownloadError (err error ) error {
205- // Preserve any already-classified error: legacy *output.ExitError or any
206- // typed errs.* error. Only un-classified errors get wrapped as network.
207- var exitErr * output.ExitError
208- if errors .As (err , & exitErr ) {
209- return err
210- }
211- if _ , ok := errs .ProblemOf (err ); ok {
212- return err
213- }
214- return output .ErrNetwork ("download failed: %s" , err )
215- }
216-
217205func validateNonEmptyMarkdownSize (size int64 ) error {
218206 if size == 0 {
219- return output . ErrValidation ("%s" , markdownEmptyContentError )
207+ return markdownValidationError ("%s" , markdownEmptyContentError )
220208 }
221209 return nil
222210}
@@ -227,12 +215,12 @@ func markdownSourceSize(runtime *common.RuntimeContext, spec markdownUploadSpec)
227215 size = int64 (len (spec .Content ))
228216 } else {
229217 if strings .TrimSpace (spec .FilePath ) == "" {
230- return 0 , common . FlagErrorf ( "--file cannot be empty" )
218+ return 0 , markdownValidationParamError ( "--file" , "--file cannot be empty" )
231219 }
232220
233221 info , err := runtime .FileIO ().Stat (spec .FilePath )
234222 if err != nil {
235- return 0 , common .WrapInputStatError (err )
223+ return 0 , withMarkdownFileParam ( common .WrapInputStatErrorTyped (err ), "--file" )
236224 }
237225 size = info .Size ()
238226 }
@@ -424,7 +412,7 @@ func uploadMarkdownFileAll(runtime *common.RuntimeContext, spec markdownUploadSp
424412 return withMarkdownUploadRetryResult (runtime , markdownUploadAllAction , func () (markdownUploadResult , error ) {
425413 fileReader , err := openReader ()
426414 if err != nil {
427- return markdownUploadResult {}, common .WrapInputStatErrorTyped (err )
415+ return markdownUploadResult {}, withMarkdownFileParam ( common .WrapInputStatErrorTyped (err ), "--file" )
428416 }
429417 defer fileReader .Close ()
430418
@@ -491,7 +479,7 @@ func uploadMarkdownFileMultipart(runtime *common.RuntimeContext, spec markdownUp
491479
492480 fileReader , err := openReader ()
493481 if err != nil {
494- return markdownUploadResult {}, common .WrapInputStatErrorTyped (err )
482+ return markdownUploadResult {}, withMarkdownFileParam ( common .WrapInputStatErrorTyped (err ), "--file" )
495483 }
496484 defer fileReader .Close ()
497485
@@ -563,7 +551,7 @@ func uploadMarkdownMultipartParts(runtime *common.RuntimeContext, fileReader io.
563551
564552 n , readErr := io .ReadFull (fileReader , buffer [:int (chunkSize )])
565553 if readErr != nil {
566- return output . ErrValidation ( " cannot read file: %s" , readErr )
554+ return markdownValidationParamError ( "--file" , " cannot read file: %s" , readErr ). WithCause ( readErr )
567555 }
568556
569557 fd := larkcore .NewFormdata ()
0 commit comments