checkpoint: moved the invalid status content type error to model

This commit is contained in:
Dan Anglin 2024-06-02 10:38:30 +01:00
parent d660081f3e
commit e75468b6fa
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
5 changed files with 28 additions and 21 deletions

View file

@ -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{

View file

@ -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)"
}

View file

@ -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 {

View file

@ -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"
}

View file

@ -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
}