11package kinesisfake
22
33import (
4+ "errors"
45 "fmt"
56 "net/http"
7+
8+ "github.com/aws/smithy-go"
69)
710
11+ var errCodeToStatusCode = map [string ]int {
12+ "ResourceNotFoundException" : http .StatusNotFound ,
13+ "ExpiredIteratorException" : http .StatusBadRequest ,
14+ "ProvisionedThroughputExceededException" : http .StatusBadRequest ,
15+ "AccessDeniedException" : http .StatusForbidden ,
16+ "InvalidArgumentException" : http .StatusBadRequest ,
17+ }
18+
819func handleError (w http.ResponseWriter , err error ) {
9- if kerr , ok := err .(KinesisError ); ok {
10- w .WriteHeader (kerr .StatusCode ())
11- fmt .Fprintf (w , "{ \" __type\" : \" %s\" , \" message\" : \" %s\" }" , kerr .AWSExceptionCode (), kerr .Error ())
12- return
20+ var apiErr smithy.APIError
21+ if errors .As (err , & apiErr ) {
22+ statusCode := errCodeToStatusCode [apiErr .ErrorCode ()]
23+ if statusCode == 0 {
24+ statusCode = http .StatusInternalServerError
25+ }
26+ w .WriteHeader (statusCode )
27+ fmt .Fprintf (w , "{ \" __type\" : \" %s\" , \" message\" : \" %s\" }" , apiErr .ErrorCode (), apiErr .ErrorMessage ())
1328 }
29+
1430 w .WriteHeader (http .StatusInternalServerError )
1531 fmt .Fprintf (w , "{ \" error\" : \" Internal Server Error: %v\" }" , err )
1632}
@@ -20,59 +36,3 @@ type KinesisError interface {
2036 StatusCode () int
2137 AWSExceptionCode () string
2238}
23-
24- /* UnsupportedOperationError */
25-
26- type UnsupportedOperationError struct {
27- Operation string
28- }
29-
30- func (e * UnsupportedOperationError ) Error () string {
31- return fmt .Sprintf ("Operation '%s' not supported" , e .Operation )
32- }
33-
34- func (e * UnsupportedOperationError ) StatusCode () int {
35- return http .StatusNotImplemented
36- }
37-
38- /* ResourceNotFoundException */
39-
40- type ResourceNotFoundException struct {
41- message string
42- }
43-
44- func (e * ResourceNotFoundException ) Error () string {
45- if e .message != "" {
46- return e .message
47- }
48- return "Resource not found"
49- }
50-
51- func (e * ResourceNotFoundException ) StatusCode () int {
52- return http .StatusBadRequest
53- }
54-
55- func (e * ResourceNotFoundException ) AWSExceptionCode () string {
56- return "ResourceNotFoundException"
57- }
58-
59- /* ExpiredIteratorException */
60-
61- type ExpiredIteratorException struct {
62- message string
63- }
64-
65- func (e * ExpiredIteratorException ) Error () string {
66- if e .message != "" {
67- return e .message
68- }
69- return "Iterator expired"
70- }
71-
72- func (e * ExpiredIteratorException ) StatusCode () int {
73- return http .StatusBadRequest
74- }
75-
76- func (e * ExpiredIteratorException ) AWSExceptionCode () string {
77- return "ExpiredIteratorException"
78- }
0 commit comments