Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Define a pattern for messaging errors.
Browse files Browse the repository at this point in the history
Removed duplicated errors messages and defined in only one function.
  • Loading branch information
faabiosr committed May 12, 2019
1 parent adbf13b commit 98590fe
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions assert.go
Expand Up @@ -33,8 +33,8 @@ var (
ErrJson = "unable to marshal"
)

func fail(msg string) error {
return fmt.Errorf(FailMessage, msg)
func failf(format string, a ...interface{}) error {
return fmt.Errorf(FailMessage, fmt.Sprintf(format, a...))
}

// RequestMediaType asserts request media type against a list.
Expand All @@ -51,7 +51,7 @@ func RequestMediaType(mediaType string, doc Document, path, method string) error
}
}

return fail(fmt.Sprintf(ErrMediaType, mediaType, strings.Join(types, ", ")))
return failf(ErrMediaType, mediaType, strings.Join(types, ", "))
}

// ResponseMediaType asserts response media type against a list.
Expand All @@ -68,7 +68,7 @@ func ResponseMediaType(mediaType string, doc Document, path, method string) erro
}
}

return fail(fmt.Sprintf(ErrMediaType, mediaType, strings.Join(types, ", ")))
return failf(ErrMediaType, mediaType, strings.Join(types, ", "))
}

// RequestHeaders asserts rquest headers againt a schema header list.
Expand Down Expand Up @@ -110,9 +110,7 @@ func RequestHeaders(header http.Header, doc Document, path, method string) error
errorMessages = append(errorMessages, v.Description())
}

return fail(
fmt.Sprintf(ErrRequestHeaders, string(data), strings.Join(errorMessages, ", ")),
)
return failf(ErrRequestHeaders, string(data), strings.Join(errorMessages, ", "))
}

// ResponseHeaders asserts response headers againt a schema header list.
Expand Down Expand Up @@ -154,9 +152,7 @@ func ResponseHeaders(header http.Header, doc Document, path, method string, stat
errorMessages = append(errorMessages, v.Description())
}

return fail(
fmt.Sprintf(ErrResponseHeaders, string(data), strings.Join(errorMessages, ", ")),
)
return failf(ErrResponseHeaders, string(data), strings.Join(errorMessages, ", "))
}

// RequestQuery asserts request query againt a schema.
Expand Down Expand Up @@ -192,7 +188,5 @@ func RequestQuery(query url.Values, doc Document, path, method string) error {
errorMessages = append(errorMessages, v.Description())
}

return fail(
fmt.Sprintf(ErrRequestQuery, string(data), strings.Join(errorMessages, ", ")),
)
return failf(ErrRequestQuery, string(data), strings.Join(errorMessages, ", "))
}

0 comments on commit 98590fe

Please sign in to comment.