diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 7de554d..7739113 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -39,7 +39,7 @@ var ( func main() { if err := run(); err != nil { - fmt.Printf("ERROR: %v.\n", err) + fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) os.Exit(1) } } @@ -71,7 +71,7 @@ func run() error { flag.BoolFunc("no-color", "disable ANSI colour output when displaying text on screen", func(value string) error { boolVal, err := strconv.ParseBool(value) if err != nil { - return fmt.Errorf("unable to parse %q as a boolean; %w", value, err) + return fmt.Errorf("unable to parse %q as a boolean: %w", value, err) } topLevelFlags.NoColor = new(bool) @@ -200,7 +200,7 @@ func run() error { } if err != nil { - return fmt.Errorf("received an error executing the command; %w", err) + return fmt.Errorf("(%s) %w", command, err) } return nil diff --git a/internal/executor/create.go b/internal/executor/create.go index 06f7b29..9da207b 100644 --- a/internal/executor/create.go +++ b/internal/executor/create.go @@ -58,7 +58,7 @@ func NewCreateExecutor(tlf TopLevelFlags, name, summary string) *CreateExecutor createExe.BoolFunc(flagSensitive, "specify if the status should be marked as sensitive", func(value string) error { boolVal, err := strconv.ParseBool(value) if err != nil { - return fmt.Errorf("unable to parse %q as a boolean value; %w", value, err) + return fmt.Errorf("unable to parse %q as a boolean value: %w", value, err) } createExe.sensitive = new(bool) @@ -79,7 +79,7 @@ func (c *CreateExecutor) Execute() error { gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir) if err != nil { - return fmt.Errorf("unable to create the GoToSocial client; %w", err) + return fmt.Errorf("unable to create the GoToSocial client: %w", err) } funcMap := map[string]func(*client.Client) error{ @@ -100,9 +100,9 @@ func (c *CreateExecutor) createList(gtsClient *client.Client) error { return FlagNotSetError{flagText: flagListTitle} } - parsedListRepliesPolicy := model.ParseListRepliesPolicy(c.listRepliesPolicy) - if parsedListRepliesPolicy == model.ListRepliesPolicyUnknown { - return InvalidListRepliesPolicyError{Policy: c.listRepliesPolicy} + parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy) + if err != nil { + return err } form := client.CreateListForm{ @@ -136,7 +136,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { case c.fromFile != "": content, err = utilities.ReadFile(c.fromFile) if err != nil { - return fmt.Errorf("unable to get the status contents from %q; %w", c.fromFile, err) + return fmt.Errorf("unable to get the status contents from %q: %w", c.fromFile, err) } default: return EmptyContentError{ @@ -147,7 +147,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { preferences, err := gtsClient.GetUserPreferences() if err != nil { - fmt.Println("WARNING: Unable to get your posting preferences; %w", err) + fmt.Println("WARNING: Unable to get your posting preferences: %w", err) } if c.language != "" { @@ -193,7 +193,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { status, err := gtsClient.CreateStatus(form) if err != nil { - return fmt.Errorf("unable to create the status; %w", err) + return fmt.Errorf("unable to create the status: %w", err) } fmt.Println("Successfully created the following status:") diff --git a/internal/executor/edit.go b/internal/executor/edit.go index d7c6120..82959fa 100644 --- a/internal/executor/edit.go +++ b/internal/executor/edit.go @@ -76,9 +76,9 @@ func (e *EditExecutor) editList(gtsClient *client.Client) error { } if e.listRepliesPolicy != "" { - parsedListRepliesPolicy := model.ParseListRepliesPolicy(e.listRepliesPolicy) - if parsedListRepliesPolicy == model.ListRepliesPolicyUnknown { - return InvalidListRepliesPolicyError{Policy: e.listRepliesPolicy} + parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(e.listRepliesPolicy) + if err != nil { + return err } list.RepliesPolicy = parsedListRepliesPolicy diff --git a/internal/executor/errors.go b/internal/executor/errors.go index 1af7689..6383ef3 100644 --- a/internal/executor/errors.go +++ b/internal/executor/errors.go @@ -82,11 +82,3 @@ type InvalidStatusContentTypeError struct { func (e InvalidStatusContentTypeError) Error() string { return "'" + e.ContentType + "' is an invalid status content type (valid values are plain and markdown)" } - -type InvalidListRepliesPolicyError struct { - Policy string -} - -func (e InvalidListRepliesPolicyError) Error() string { - return "'" + e.Policy + "' is an invalid list replies policy" -} diff --git a/internal/executor/executor.go b/internal/executor/executor.go index d42fb94..88cd1e1 100644 --- a/internal/executor/executor.go +++ b/internal/executor/executor.go @@ -14,11 +14,11 @@ type Executor interface { func Execute(executor Executor, args []string) error { if err := executor.Parse(args); err != nil { - return fmt.Errorf("unable to parse the command line flags; %w", err) + return fmt.Errorf("parsing error: %w", err) } if err := executor.Execute(); err != nil { - return fmt.Errorf("unable to execute the command %q; %w", executor.Name(), err) + return fmt.Errorf("execution error: %w", err) } return nil diff --git a/internal/model/list.go b/internal/model/list.go index 1331b99..8820c03 100644 --- a/internal/model/list.go +++ b/internal/model/list.go @@ -41,7 +41,7 @@ func (l ListRepliesPolicy) String() string { return output } -func ParseListRepliesPolicy(value string) ListRepliesPolicy { +func ParseListRepliesPolicy(value string) (ListRepliesPolicy, error) { mapped := map[string]ListRepliesPolicy{ listRepliesPolicyFollowedValue: ListRepliesPolicyFollowed, listRepliesPolicyListValue: ListRepliesPolicyList, @@ -50,19 +50,24 @@ func ParseListRepliesPolicy(value string) ListRepliesPolicy { output, ok := mapped[value] if !ok { - return ListRepliesPolicyUnknown + return ListRepliesPolicyUnknown, InvalidListRepliesPolicyError{value} } - return output + return output, nil } func (l ListRepliesPolicy) MarshalJSON() ([]byte, error) { value := l.String() if value == unknownValue { - return nil, fmt.Errorf("%q is not a valid list replies policy") + return nil, InvalidListRepliesPolicyError{value} } - return json.Marshal(value) + data, err := json.Marshal(value) + if err != nil { + return nil, fmt.Errorf("unable to encode %s to JSON: %w", value, err) + } + + return data, nil } func (l *ListRepliesPolicy) UnmarshalJSON(data []byte) error { @@ -75,11 +80,27 @@ func (l *ListRepliesPolicy) UnmarshalJSON(data []byte) error { return fmt.Errorf("unable to unmarshal the data; %w", err) } - *l = ParseListRepliesPolicy(value) + *l, err = ParseListRepliesPolicy(value) + if err != nil { + return err + } return nil } +type InvalidListRepliesPolicyError struct { + Value string +} + +func (e InvalidListRepliesPolicyError) Error() string { + return "'" + + e.Value + + "' is not a valid list replies policy (valid values are " + + listRepliesPolicyFollowedValue + ", " + + listRepliesPolicyListValue + ", " + + listRepliesPolicyNoneValue + ")" +} + type List struct { ID string `json:"id"` RepliesPolicy ListRepliesPolicy `json:"replies_policy"`