/
errors.go
50 lines (41 loc) · 1.11 KB
/
errors.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
50
package ec2
import (
"github.com/YaleSpinup/apierror"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func ErrCode(msg string, err error) error {
if aerr, ok := errors.Cause(err).(awserr.Error); ok {
switch aerr.Code() {
case
// Access forbidden.
"Forbidden":
return apierror.New(apierror.ErrForbidden, msg, aerr)
case
// Conflict
"Conflict":
return apierror.New(apierror.ErrConflict, msg, aerr)
case
// Not found.
"NotFound":
return apierror.New(apierror.ErrNotFound, msg, aerr)
case
"Bad Request":
return apierror.New(apierror.ErrBadRequest, msg, aerr)
case
// Limit Exceeded
"LimitExceeded":
return apierror.New(apierror.ErrLimitExceeded, msg, aerr)
case
// Service Unavailable
"ServiceUnavailable":
return apierror.New(apierror.ErrServiceUnavailable, msg, aerr)
default:
m := msg + ": " + aerr.Message()
return apierror.New(apierror.ErrBadRequest, m, aerr)
}
}
log.Warnf("uncaught error: %s, returning Internal Server Error", err)
return apierror.New(apierror.ErrInternalError, msg, err)
}