|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "github.com/weaveworks/common/httpgrpc" |
| 7 | + "google.golang.org/grpc/codes" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/go-kit/log" |
| 14 | + v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestRespondFromGRPCError(t *testing.T) { |
| 19 | + logger := log.NewNopLogger() |
| 20 | + for _, tc := range []struct { |
| 21 | + name string |
| 22 | + err error |
| 23 | + expectedResp *Response |
| 24 | + code int |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "non grpc error", |
| 28 | + err: errors.New("test"), |
| 29 | + expectedResp: &Response{ |
| 30 | + Status: "error", |
| 31 | + ErrorType: v1.ErrServer, |
| 32 | + Error: "test", |
| 33 | + }, |
| 34 | + code: 500, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "bad data", |
| 38 | + err: httpgrpc.Errorf(http.StatusBadRequest, "bad_data"), |
| 39 | + expectedResp: &Response{ |
| 40 | + Status: "error", |
| 41 | + ErrorType: v1.ErrBadData, |
| 42 | + Error: "bad_data", |
| 43 | + }, |
| 44 | + code: http.StatusBadRequest, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "413", |
| 48 | + err: httpgrpc.Errorf(http.StatusRequestEntityTooLarge, "bad_data"), |
| 49 | + expectedResp: &Response{ |
| 50 | + Status: "error", |
| 51 | + ErrorType: v1.ErrBadData, |
| 52 | + Error: "bad_data", |
| 53 | + }, |
| 54 | + code: http.StatusRequestEntityTooLarge, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "499", |
| 58 | + err: httpgrpc.Errorf(StatusClientClosedRequest, "bad_data"), |
| 59 | + expectedResp: &Response{ |
| 60 | + Status: "error", |
| 61 | + ErrorType: v1.ErrCanceled, |
| 62 | + Error: "bad_data", |
| 63 | + }, |
| 64 | + code: StatusClientClosedRequest, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "504", |
| 68 | + err: httpgrpc.Errorf(http.StatusGatewayTimeout, "bad_data"), |
| 69 | + expectedResp: &Response{ |
| 70 | + Status: "error", |
| 71 | + ErrorType: v1.ErrTimeout, |
| 72 | + Error: "bad_data", |
| 73 | + }, |
| 74 | + code: http.StatusGatewayTimeout, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "422", |
| 78 | + err: httpgrpc.Errorf(http.StatusUnprocessableEntity, "bad_data"), |
| 79 | + expectedResp: &Response{ |
| 80 | + Status: "error", |
| 81 | + ErrorType: v1.ErrExec, |
| 82 | + Error: "bad_data", |
| 83 | + }, |
| 84 | + code: http.StatusUnprocessableEntity, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "grpc status code", |
| 88 | + err: httpgrpc.Errorf(int(codes.PermissionDenied), "bad_data"), |
| 89 | + expectedResp: &Response{ |
| 90 | + Status: "error", |
| 91 | + ErrorType: v1.ErrBadData, |
| 92 | + Error: "bad_data", |
| 93 | + }, |
| 94 | + code: http.StatusUnprocessableEntity, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "other status code defaults to err server", |
| 98 | + err: httpgrpc.Errorf(http.StatusTooManyRequests, "bad_data"), |
| 99 | + expectedResp: &Response{ |
| 100 | + Status: "error", |
| 101 | + ErrorType: v1.ErrServer, |
| 102 | + Error: "bad_data", |
| 103 | + }, |
| 104 | + code: http.StatusTooManyRequests, |
| 105 | + }, |
| 106 | + } { |
| 107 | + t.Run(tc.name, func(t *testing.T) { |
| 108 | + writer := httptest.NewRecorder() |
| 109 | + RespondFromGRPCError(logger, writer, tc.err) |
| 110 | + output, err := io.ReadAll(writer.Body) |
| 111 | + require.NoError(t, err) |
| 112 | + var res Response |
| 113 | + err = json.Unmarshal(output, &res) |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + require.Equal(t, tc.expectedResp.Status, res.Status) |
| 117 | + require.Equal(t, tc.expectedResp.Error, res.Error) |
| 118 | + require.Equal(t, tc.expectedResp.ErrorType, res.ErrorType) |
| 119 | + |
| 120 | + require.Equal(t, tc.code, writer.Code) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestRespondError(t *testing.T) { |
| 126 | + logger := log.NewNopLogger() |
| 127 | + for _, tc := range []struct { |
| 128 | + name string |
| 129 | + errorType v1.ErrorType |
| 130 | + msg string |
| 131 | + status string |
| 132 | + code int |
| 133 | + expectedResp *Response |
| 134 | + }{ |
| 135 | + { |
| 136 | + name: "bad data", |
| 137 | + errorType: v1.ErrBadData, |
| 138 | + msg: "test_msg", |
| 139 | + status: "error", |
| 140 | + code: 400, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "server error", |
| 144 | + errorType: v1.ErrServer, |
| 145 | + msg: "test_msg", |
| 146 | + status: "error", |
| 147 | + code: 500, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "canceled", |
| 151 | + errorType: v1.ErrCanceled, |
| 152 | + msg: "test_msg", |
| 153 | + status: "error", |
| 154 | + code: 499, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "timeout", |
| 158 | + errorType: v1.ErrTimeout, |
| 159 | + msg: "test_msg", |
| 160 | + status: "error", |
| 161 | + code: 502, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "prometheus_format_error", |
| 165 | + expectedResp: &Response{ |
| 166 | + Status: "error", |
| 167 | + ErrorType: v1.ErrServer, |
| 168 | + Error: "server_error", |
| 169 | + }, |
| 170 | + code: 400, |
| 171 | + status: "error", |
| 172 | + errorType: v1.ErrBadData, |
| 173 | + }, |
| 174 | + { |
| 175 | + // If the input Prometheus error cannot be unmarshalled, |
| 176 | + // use the error type and message provided in the function. |
| 177 | + name: "bad_prometheus_format_error", |
| 178 | + msg: `"status":"error","data":null,"errorType":"bad_data","error":"bad_data"}`, |
| 179 | + code: 500, |
| 180 | + status: "error", |
| 181 | + errorType: v1.ErrServer, |
| 182 | + }, |
| 183 | + } { |
| 184 | + t.Run(tc.name, func(t *testing.T) { |
| 185 | + msg := tc.msg |
| 186 | + if tc.expectedResp != nil { |
| 187 | + output, err := json.Marshal(tc.expectedResp) |
| 188 | + require.NoError(t, err) |
| 189 | + msg = string(output) |
| 190 | + } |
| 191 | + writer := httptest.NewRecorder() |
| 192 | + RespondError(logger, writer, tc.errorType, msg, tc.code) |
| 193 | + output, err := io.ReadAll(writer.Body) |
| 194 | + require.NoError(t, err) |
| 195 | + var res Response |
| 196 | + err = json.Unmarshal(output, &res) |
| 197 | + require.NoError(t, err) |
| 198 | + |
| 199 | + if tc.expectedResp == nil { |
| 200 | + require.Equal(t, tc.status, res.Status) |
| 201 | + require.Equal(t, tc.msg, res.Error) |
| 202 | + require.Equal(t, tc.errorType, res.ErrorType) |
| 203 | + } else { |
| 204 | + require.Equal(t, tc.expectedResp.Status, res.Status) |
| 205 | + require.Equal(t, tc.expectedResp.Error, res.Error) |
| 206 | + require.Equal(t, tc.expectedResp.ErrorType, res.ErrorType) |
| 207 | + } |
| 208 | + |
| 209 | + require.Equal(t, tc.code, writer.Code) |
| 210 | + }) |
| 211 | + } |
| 212 | +} |
0 commit comments