2019-02-09 09:53:29 -05:00
|
|
|
package errdefs
|
2018-12-31 07:59:59 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFromStatusCode(t *testing.T) {
|
|
|
|
testErr := fmt.Errorf("some error occurred")
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
err error
|
|
|
|
status int
|
|
|
|
check func(error) bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusNotFound,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsNotFound,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusBadRequest,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsInvalidParameter,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusConflict,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsConflict,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusUnauthorized,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsUnauthorized,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusServiceUnavailable,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsUnavailable,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusForbidden,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsForbidden,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusNotModified,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsNotModified,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusNotImplemented,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsNotImplemented,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: testErr,
|
|
|
|
status: http.StatusInternalServerError,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsSystem,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-09 09:53:29 -05:00
|
|
|
err: Unknown(testErr),
|
2018-12-31 07:59:59 -05:00
|
|
|
status: http.StatusInternalServerError,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsUnknown,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-09 09:53:29 -05:00
|
|
|
err: DataLoss(testErr),
|
2018-12-31 07:59:59 -05:00
|
|
|
status: http.StatusInternalServerError,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsDataLoss,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-09 09:53:29 -05:00
|
|
|
err: Deadline(testErr),
|
2018-12-31 07:59:59 -05:00
|
|
|
status: http.StatusInternalServerError,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsDeadline,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
{
|
2019-02-09 09:53:29 -05:00
|
|
|
err: Cancelled(testErr),
|
2018-12-31 07:59:59 -05:00
|
|
|
status: http.StatusInternalServerError,
|
2019-02-09 09:53:29 -05:00
|
|
|
check: IsCancelled,
|
2018-12-31 07:59:59 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(http.StatusText(tc.status), func(t *testing.T) {
|
|
|
|
err := FromStatusCode(tc.err, tc.status)
|
|
|
|
assert.Check(t, tc.check(err), "unexpected error-type %T", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|