diff --git a/internal/executor/create.go b/internal/executor/create.go index 7385d03..a9eec1c 100644 --- a/internal/executor/create.go +++ b/internal/executor/create.go @@ -173,9 +173,9 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { return err } - parsedContentType := model.ParseStatusContentType(c.contentType) - if parsedContentType == model.StatusContentTypeUnknown { - return InvalidStatusContentTypeError{ContentType: c.contentType} + parsedContentType, err := model.ParseStatusContentType(c.contentType) + if err != nil { + return err } form := client.CreateStatusForm{ diff --git a/internal/executor/errors.go b/internal/executor/errors.go index 54fd190..1a5e6f6 100644 --- a/internal/executor/errors.go +++ b/internal/executor/errors.go @@ -58,11 +58,3 @@ func (e EmptyContentError) Error() string { return message } - -type InvalidStatusContentTypeError struct { - ContentType string -} - -func (e InvalidStatusContentTypeError) Error() string { - return "'" + e.ContentType + "' is an invalid status content type (valid values are plain and markdown)" -} diff --git a/internal/model/list.go b/internal/model/list.go index c4c044e..30f3e48 100644 --- a/internal/model/list.go +++ b/internal/model/list.go @@ -95,10 +95,10 @@ type InvalidListRepliesPolicyError struct { func (e InvalidListRepliesPolicyError) Error() string { return "'" + e.Value + - "' is not a valid list replies policy (valid values are " + + "' is not a valid list replies policy: valid values are " + listRepliesPolicyFollowedValue + ", " + listRepliesPolicyListValue + ", " + - listRepliesPolicyNoneValue + ")" + listRepliesPolicyNoneValue } type List struct { diff --git a/internal/model/status_content_type.go b/internal/model/status_content_type.go index 7d33120..8b252a9 100644 --- a/internal/model/status_content_type.go +++ b/internal/model/status_content_type.go @@ -38,22 +38,37 @@ func (s StatusContentType) String() string { return output } -func ParseStatusContentType(value string) StatusContentType { +func ParseStatusContentType(value string) (StatusContentType, error) { switch value { case statusContentTypePlainValue, statusContentTypeTextPlainValue: - return StatusContentTypePlainText + return StatusContentTypePlainText, nil case statusContentTypeMarkdownValue, statusContentTypeTextMarkdownValue: - return StatusContentTypeMarkdown + return StatusContentTypeMarkdown, nil } - return StatusContentTypeUnknown + return StatusContentTypeUnknown, InvalidStatusContentTypeError{Value: value} } func (s StatusContentType) MarshalJSON() ([]byte, error) { value := s.String() if value == unknownValue { - return nil, fmt.Errorf("%q is not a valid status content type", value) + return nil, InvalidStatusContentTypeError{Value: 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 +} + +type InvalidStatusContentTypeError struct { + Value string +} + +func (e InvalidStatusContentTypeError) Error() string { + return "'" + e.Value + "' is an invalid status content type: valid values are " + + statusContentTypePlainValue + " or " + statusContentTypeTextPlainValue + " for plain text, or " + + statusContentTypeMarkdownValue + " or " + statusContentTypeTextMarkdownValue + " for Markdown" } diff --git a/internal/model/status_visibility.go b/internal/model/status_visibility.go index 84bd76a..48c9262 100644 --- a/internal/model/status_visibility.go +++ b/internal/model/status_visibility.go @@ -98,10 +98,10 @@ type InvalidStatusVisibilityError struct { } func (e InvalidStatusVisibilityError) Error() string { - return "'" + e.Value + "' is not a valid status visibility value (valid values are " + + return "'" + e.Value + "' is not a valid status visibility value: valid values are " + statusVisibilityPublicValue + ", " + statusVisibilityUnlistedValue + ", " + statusVisibilityPrivateValue + ", " + statusVisibilityMutualsOnlyValue + ", " + - statusVisibilityDirectValue + ")" + statusVisibilityDirectValue }