cephfs admin: add support for returning not-implemented errors

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-09-30 15:58:07 -04:00 committed by John Mulligan
parent 462372c178
commit 5f64c52223
2 changed files with 47 additions and 0 deletions

View File

@ -20,8 +20,26 @@ var (
const (
deprecatedSuffix = "call is deprecated and will be removed in a future release"
missingPrefix = "No handler found"
einval = -22
)
type cephError interface {
ErrorCode() int
}
// NotImplementedError error values will be returned in the case that an API
// call is not available in the version of Ceph that is running in the target
// cluster.
type NotImplementedError struct {
response
}
// Error implements the error interface.
func (e NotImplementedError) Error() string {
return fmt.Sprintf("API call not implemented server-side: %s", e.status)
}
// response encapsulates the data returned by ceph and supports easy processing
// pipelines.
type response struct {
@ -57,6 +75,11 @@ func (r response) Status() string {
// that response is no longer needed for processing.
func (r response) End() error {
if !r.Ok() {
if ce, ok := r.err.(cephError); ok {
if ce.ErrorCode() == einval && strings.HasPrefix(r.status, missingPrefix) {
return NotImplementedError{response: r}
}
}
return r
}
return nil

View File

@ -125,4 +125,28 @@ func TestResponse(t *testing.T) {
assert.False(t, rtemp.Ok())
assert.Equal(t, "x", rtemp.Status())
})
t.Run("notImplemented", func(t *testing.T) {
rtemp := response{
status: "No handler found for this function",
err: myCephError(-22),
}
if assert.False(t, rtemp.Ok()) {
err := rtemp.End()
assert.Error(t, err)
var n NotImplementedError
assert.True(t, errors.As(err, &n))
assert.Contains(t, err.Error(), "not implemented")
}
})
}
type myCephError int
func (myCephError) Error() string {
return "oops"
}
func (e myCephError) ErrorCode() int {
return int(e)
}