checkpoint: see body

- moved InvalidListRepliesPolicyError to the model package
- cleaned up some code there in regards to the list replies policy enum type
- cleaned up error messages sent back to the user
- print error messages to standard error os.Stderr
This commit is contained in:
Dan Anglin 2024-06-02 08:50:47 +01:00
parent c05cce9154
commit 40df5e53fe
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 43 additions and 30 deletions

View file

@ -39,7 +39,7 @@ var (
func main() { func main() {
if err := run(); err != nil { if err := run(); err != nil {
fmt.Printf("ERROR: %v.\n", err) fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1) 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 { flag.BoolFunc("no-color", "disable ANSI colour output when displaying text on screen", func(value string) error {
boolVal, err := strconv.ParseBool(value) boolVal, err := strconv.ParseBool(value)
if err != nil { 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) topLevelFlags.NoColor = new(bool)
@ -200,7 +200,7 @@ func run() error {
} }
if err != nil { if err != nil {
return fmt.Errorf("received an error executing the command; %w", err) return fmt.Errorf("(%s) %w", command, err)
} }
return nil return nil

View file

@ -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 { createExe.BoolFunc(flagSensitive, "specify if the status should be marked as sensitive", func(value string) error {
boolVal, err := strconv.ParseBool(value) boolVal, err := strconv.ParseBool(value)
if err != nil { 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) createExe.sensitive = new(bool)
@ -79,7 +79,7 @@ func (c *CreateExecutor) Execute() error {
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir) gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir)
if err != nil { 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{ funcMap := map[string]func(*client.Client) error{
@ -100,9 +100,9 @@ func (c *CreateExecutor) createList(gtsClient *client.Client) error {
return FlagNotSetError{flagText: flagListTitle} return FlagNotSetError{flagText: flagListTitle}
} }
parsedListRepliesPolicy := model.ParseListRepliesPolicy(c.listRepliesPolicy) parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy)
if parsedListRepliesPolicy == model.ListRepliesPolicyUnknown { if err != nil {
return InvalidListRepliesPolicyError{Policy: c.listRepliesPolicy} return err
} }
form := client.CreateListForm{ form := client.CreateListForm{
@ -136,7 +136,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
case c.fromFile != "": case c.fromFile != "":
content, err = utilities.ReadFile(c.fromFile) content, err = utilities.ReadFile(c.fromFile)
if err != nil { 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: default:
return EmptyContentError{ return EmptyContentError{
@ -147,7 +147,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
preferences, err := gtsClient.GetUserPreferences() preferences, err := gtsClient.GetUserPreferences()
if err != nil { 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 != "" { if c.language != "" {
@ -193,7 +193,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
status, err := gtsClient.CreateStatus(form) status, err := gtsClient.CreateStatus(form)
if err != nil { 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:") fmt.Println("Successfully created the following status:")

View file

@ -76,9 +76,9 @@ func (e *EditExecutor) editList(gtsClient *client.Client) error {
} }
if e.listRepliesPolicy != "" { if e.listRepliesPolicy != "" {
parsedListRepliesPolicy := model.ParseListRepliesPolicy(e.listRepliesPolicy) parsedListRepliesPolicy, err := model.ParseListRepliesPolicy(e.listRepliesPolicy)
if parsedListRepliesPolicy == model.ListRepliesPolicyUnknown { if err != nil {
return InvalidListRepliesPolicyError{Policy: e.listRepliesPolicy} return err
} }
list.RepliesPolicy = parsedListRepliesPolicy list.RepliesPolicy = parsedListRepliesPolicy

View file

@ -82,11 +82,3 @@ type InvalidStatusContentTypeError struct {
func (e InvalidStatusContentTypeError) Error() string { func (e InvalidStatusContentTypeError) Error() string {
return "'" + e.ContentType + "' is an invalid status content type (valid values are plain and markdown)" 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"
}

View file

@ -14,11 +14,11 @@ type Executor interface {
func Execute(executor Executor, args []string) error { func Execute(executor Executor, args []string) error {
if err := executor.Parse(args); err != nil { 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 { 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 return nil

View file

@ -41,7 +41,7 @@ func (l ListRepliesPolicy) String() string {
return output return output
} }
func ParseListRepliesPolicy(value string) ListRepliesPolicy { func ParseListRepliesPolicy(value string) (ListRepliesPolicy, error) {
mapped := map[string]ListRepliesPolicy{ mapped := map[string]ListRepliesPolicy{
listRepliesPolicyFollowedValue: ListRepliesPolicyFollowed, listRepliesPolicyFollowedValue: ListRepliesPolicyFollowed,
listRepliesPolicyListValue: ListRepliesPolicyList, listRepliesPolicyListValue: ListRepliesPolicyList,
@ -50,19 +50,24 @@ func ParseListRepliesPolicy(value string) ListRepliesPolicy {
output, ok := mapped[value] output, ok := mapped[value]
if !ok { if !ok {
return ListRepliesPolicyUnknown return ListRepliesPolicyUnknown, InvalidListRepliesPolicyError{value}
} }
return output return output, nil
} }
func (l ListRepliesPolicy) MarshalJSON() ([]byte, error) { func (l ListRepliesPolicy) MarshalJSON() ([]byte, error) {
value := l.String() value := l.String()
if value == unknownValue { 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 { 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) return fmt.Errorf("unable to unmarshal the data; %w", err)
} }
*l = ParseListRepliesPolicy(value) *l, err = ParseListRepliesPolicy(value)
if err != nil {
return err
}
return nil 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 { type List struct {
ID string `json:"id"` ID string `json:"id"`
RepliesPolicy ListRepliesPolicy `json:"replies_policy"` RepliesPolicy ListRepliesPolicy `json:"replies_policy"`