new error types

This commit is contained in:
Dan Anglin 2024-08-17 00:43:41 +01:00
parent fb3453f5a0
commit e49da231d4
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 43 additions and 25 deletions

View file

@ -78,9 +78,9 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
descriptionsExists = true
if !c.mediaDescriptions.ExpectedLength(numMediaFiles) {
return MismatchedNumMediaArgs{
argType: "media descriptions",
numArgs: len(c.mediaDescriptions),
return MismatchedNumMediaValuesError{
valueType: "media descriptions",
numValues: len(c.mediaDescriptions),
numMediaFiles: numMediaFiles,
}
}
@ -90,9 +90,9 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
focusValuesExists = true
if !c.mediaFocusValues.ExpectedLength(numMediaFiles) {
return MismatchedNumMediaArgs{
argType: "media focus values",
numArgs: len(c.mediaFocusValues),
return MismatchedNumMediaValuesError{
valueType: "media focus values",
numValues: len(c.mediaFocusValues),
numMediaFiles: numMediaFiles,
}
}
@ -235,19 +235,21 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
func (c *CreateExecutor) createMediaAttachment(gtsClient *client.Client) error {
expectedNumValues := 1
if !c.mediaFiles.ExpectedLength(expectedNumValues) {
return fmt.Errorf(
"received an unexpected number of media files: want %d",
expectedNumValues,
)
return UnexpectedNumValuesError{
name: "media files",
expected: expectedNumValues,
actual: len(c.mediaFiles),
}
}
description := ""
if !c.mediaDescriptions.Empty() {
if !c.mediaDescriptions.ExpectedLength(expectedNumValues) {
return fmt.Errorf(
"received an unexpected number of media descriptions: want %d",
expectedNumValues,
)
return UnexpectedNumValuesError{
name: "media descriptions",
expected: expectedNumValues,
actual: len(c.mediaDescriptions),
}
}
var err error
@ -265,10 +267,11 @@ func (c *CreateExecutor) createMediaAttachment(gtsClient *client.Client) error {
focus := ""
if !c.mediaFocusValues.Empty() {
if !c.mediaFocusValues.ExpectedLength(expectedNumValues) {
return fmt.Errorf(
"received an unexpected number of media focus values: want %d",
expectedNumValues,
)
return UnexpectedNumValuesError{
name: "media focus values",
expected: expectedNumValues,
actual: len(c.mediaFocusValues),
}
}
focus = c.mediaFocusValues[0]

View file

@ -87,18 +87,33 @@ func (e NotFollowingError) Error() string {
return "you are not following " + e.account
}
type MismatchedNumMediaArgs struct {
argType string
numArgs int
type MismatchedNumMediaValuesError struct {
valueType string
numValues int
numMediaFiles int
}
func (e MismatchedNumMediaArgs) Error() string {
func (e MismatchedNumMediaValuesError) Error() string {
return fmt.Sprintf(
"unexpected number of %s: received %d media files but got %d %s",
e.argType,
e.valueType,
e.numMediaFiles,
e.numArgs,
e.argType,
e.numValues,
e.valueType,
)
}
type UnexpectedNumValuesError struct {
name string
actual int
expected int
}
func (e UnexpectedNumValuesError) Error() string {
return fmt.Sprintf(
"received an unexpected number of %s: received %d, expected %d",
e.name,
e.actual,
e.expected,
)
}