66 "reflect"
77 "strings"
88
9- "github.com/dropbox/dbxcli/internal/output"
9+ "github.com/dropbox/dbxcli/v3/ internal/output"
1010 "github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
1111 dropboxauth "github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/auth"
1212 "github.com/spf13/cobra"
@@ -25,6 +25,7 @@ const (
2525 jsonErrorCodeEnvTokenStillActive = "env_token_still_active"
2626 jsonErrorCodeInvalidArguments = "invalid_arguments"
2727 jsonErrorCodeNotFound = "not_found"
28+ jsonErrorCodePartialTransfer = "partial_transfer"
2829 jsonErrorCodePathConflict = "path_conflict"
2930 jsonErrorCodePermissionDenied = "permission_denied"
3031 jsonErrorCodeRateLimited = "rate_limited"
@@ -33,6 +34,18 @@ const (
3334 jsonErrorCodeUnknownFlag = "unknown_flag"
3435)
3536
37+ const (
38+ exitCodeSuccess = 0
39+ exitCodeGenericError = 1
40+ exitCodeAuthFailure = 2
41+ exitCodePermissionDenied = 3
42+ exitCodeNotFound = 4
43+ exitCodeConflict = 5
44+ exitCodeRateLimited = 6
45+ exitCodeValidationError = 7
46+ exitCodePartialTransfer = 8
47+ )
48+
3649type jsonCodedError interface {
3750 error
3851 JSONErrorCode () string
@@ -202,7 +215,11 @@ func parseOutputFormat(value string) (output.Format, error) {
202215 case output .FormatJSON :
203216 return output .FormatJSON , nil
204217 default :
205- return "" , fmt .Errorf ("unsupported output format %q: use text or json" , value )
218+ return "" , invalidArgumentsErrorfWithDetails (
219+ "unsupported output format %q: use text or json" ,
220+ flagValueErrorDetails (outputFlag , value ),
221+ value ,
222+ )
206223 }
207224}
208225
@@ -276,6 +293,38 @@ func renderCommandErrorWithJSON(cmd *cobra.Command, err error, forceJSON bool) {
276293 }
277294}
278295
296+ func exitCodeForError (err error ) int {
297+ if err == nil {
298+ return exitCodeSuccess
299+ }
300+
301+ switch jsonErrorCode (err ) {
302+ case jsonErrorCodeAppKeyRequired ,
303+ jsonErrorCodeAuthExchangeFailed ,
304+ jsonErrorCodeAuthRefreshFailed ,
305+ jsonErrorCodeAuthRequired ,
306+ jsonErrorCodeEnvTokenStillActive :
307+ return exitCodeAuthFailure
308+ case jsonErrorCodePermissionDenied :
309+ return exitCodePermissionDenied
310+ case jsonErrorCodeNotFound :
311+ return exitCodeNotFound
312+ case jsonErrorCodePathConflict :
313+ return exitCodeConflict
314+ case jsonErrorCodeRateLimited :
315+ return exitCodeRateLimited
316+ case jsonErrorCodeInvalidArguments ,
317+ jsonErrorCodeStructuredOutputUnsupported ,
318+ jsonErrorCodeUnknownCommand ,
319+ jsonErrorCodeUnknownFlag :
320+ return exitCodeValidationError
321+ case jsonErrorCodePartialTransfer :
322+ return exitCodePartialTransfer
323+ default :
324+ return exitCodeGenericError
325+ }
326+ }
327+
279328// outputJSONRequested is a narrow pre-parse fallback for errors that happen
280329// before Cobra has resolved a command/flag context, such as unknown commands.
281330func outputJSONRequested (args []string ) bool {
0 commit comments