forked from TrueCloudLab/certificates
Remove all references to old apiError.
This commit is contained in:
parent
1cb8bb3ae1
commit
df60fe3f0d
2 changed files with 7 additions and 77 deletions
|
@ -1,67 +0,0 @@
|
||||||
package authority
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type apiCtx map[string]interface{}
|
|
||||||
|
|
||||||
// Error implements the api.Error interface and adds context to error messages.
|
|
||||||
type apiError struct {
|
|
||||||
err error
|
|
||||||
code int
|
|
||||||
context apiCtx
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cause implements the errors.Causer interface and returns the original error.
|
|
||||||
func (e *apiError) Cause() error {
|
|
||||||
return e.err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error returns an error message with additional context.
|
|
||||||
func (e *apiError) Error() string {
|
|
||||||
ret := e.err.Error()
|
|
||||||
|
|
||||||
/*
|
|
||||||
if len(e.context) > 0 {
|
|
||||||
ret += "\n\nContext:"
|
|
||||||
for k, v := range e.context {
|
|
||||||
ret += fmt.Sprintf("\n %s: %v", k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrorResponse represents an error in JSON format.
|
|
||||||
type ErrorResponse struct {
|
|
||||||
Status int `json:"status"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// StatusCode returns an http status code indicating the type and severity of
|
|
||||||
// the error.
|
|
||||||
func (e *apiError) StatusCode() int {
|
|
||||||
if e.code == 0 {
|
|
||||||
return http.StatusInternalServerError
|
|
||||||
}
|
|
||||||
return e.code
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaller interface for the Error struct.
|
|
||||||
func (e *apiError) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(&ErrorResponse{Status: e.code, Message: http.StatusText(e.code)})
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON implements json.Unmarshaler interface for the Error struct.
|
|
||||||
func (e *apiError) UnmarshalJSON(data []byte) error {
|
|
||||||
var er ErrorResponse
|
|
||||||
if err := json.Unmarshal(data, &er); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
e.code = er.Status
|
|
||||||
e.err = fmt.Errorf(er.Message)
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -69,8 +69,9 @@ func TestGetEncryptedKey(t *testing.T) {
|
||||||
|
|
||||||
func TestGetProvisioners(t *testing.T) {
|
func TestGetProvisioners(t *testing.T) {
|
||||||
type gp struct {
|
type gp struct {
|
||||||
a *Authority
|
a *Authority
|
||||||
err *apiError
|
err error
|
||||||
|
code int
|
||||||
}
|
}
|
||||||
tests := map[string]func(t *testing.T) *gp{
|
tests := map[string]func(t *testing.T) *gp{
|
||||||
"ok": func(t *testing.T) *gp {
|
"ok": func(t *testing.T) *gp {
|
||||||
|
@ -89,14 +90,10 @@ func TestGetProvisioners(t *testing.T) {
|
||||||
ps, next, err := tc.a.GetProvisioners("", 0)
|
ps, next, err := tc.a.GetProvisioners("", 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if assert.NotNil(t, tc.err) {
|
if assert.NotNil(t, tc.err) {
|
||||||
switch v := err.(type) {
|
sc, ok := err.(errs.StatusCoder)
|
||||||
case *apiError:
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
||||||
assert.HasPrefix(t, v.err.Error(), tc.err.Error())
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
||||||
assert.Equals(t, v.code, tc.err.code)
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
||||||
assert.Equals(t, v.context, tc.err.context)
|
|
||||||
default:
|
|
||||||
t.Errorf("unexpected error type: %T", v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if assert.Nil(t, tc.err) {
|
if assert.Nil(t, tc.err) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue