forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
49 lines (39 loc) · 937 Bytes
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package handler
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Response interface {
// Shorten attempts to return a response
// that can be serialized in a smaller size.
Shorten() Response
}
type valueResponse struct {
Value interface{} `json:"value"`
}
func NewValueResponse(value interface{}) Response {
return valueResponse{Value: value}
}
func (r valueResponse) Shorten() Response {
return r
}
type exceptionResponse struct {
Exception struct {
Message string `json:"message,omitempty"`
} `json:"exception"`
err error
}
func NewExceptionResponse(err error) (resp Response) {
r := exceptionResponse{}
r.Exception.Message = err.Error()
r.err = err
return r
}
func (r exceptionResponse) Shorten() Response {
if typedErr, ok := r.err.(bosherr.ShortenableError); ok {
sr := exceptionResponse{}
sr.Exception.Message = typedErr.ShortError()
sr.err = typedErr
return sr
}
return r
}