enbas/internal/model/status_content_type.go
Dan Anglin c6c711c29b
fix: update error handling
Changes:

- Move InvalidListRepliesPolicyError, InvalidTimelineCategory,
  InvalidStatusVisibility and InvalidStatusContentTypeError type to the
  model package.
- Clean up some code in regards to the parsing of the Enum types.
- Clean up the error messages sent back to the user.
- Use colons instead of semicolons when unwrapping error messages.
- Print errors to Standard Error (os.Stderr)
2024-06-02 11:35:43 +01:00

74 lines
1.9 KiB
Go

// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package model
import (
"encoding/json"
"fmt"
)
type StatusContentType int
const (
StatusContentTypePlainText StatusContentType = iota
StatusContentTypeMarkdown
StatusContentTypeUnknown
)
const (
statusContentTypeTextPlainValue = "text/plain"
statusContentTypePlainValue = "plain"
statusContentTypeTextMarkdownValue = "text/markdown"
statusContentTypeMarkdownValue = "markdown"
)
func (s StatusContentType) String() string {
mapped := map[StatusContentType]string{
StatusContentTypeMarkdown: statusContentTypeTextMarkdownValue,
StatusContentTypePlainText: statusContentTypeTextPlainValue,
}
output, ok := mapped[s]
if !ok {
return unknownValue
}
return output
}
func ParseStatusContentType(value string) (StatusContentType, error) {
switch value {
case statusContentTypePlainValue, statusContentTypeTextPlainValue:
return StatusContentTypePlainText, nil
case statusContentTypeMarkdownValue, statusContentTypeTextMarkdownValue:
return StatusContentTypeMarkdown, nil
}
return StatusContentTypeUnknown, InvalidStatusContentTypeError{Value: value}
}
func (s StatusContentType) MarshalJSON() ([]byte, error) {
value := s.String()
if value == unknownValue {
return nil, InvalidStatusContentTypeError{Value: 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"
}