Fixed returned API status code on error (#7435)
* Fixed returned API status code on error Signed-off-by: Marco Pracucci <marco@pracucci.com> * Fixed linter Signed-off-by: Marco Pracucci <marco@pracucci.com> * Simplified code Signed-off-by: Marco Pracucci <marco@pracucci.com>
This commit is contained in:
		
							parent
							
								
									08780a9ec9
								
							
						
					
					
						commit
						153f859b74
					
				| 
						 | 
					@ -469,7 +469,7 @@ func returnAPIError(err error) *apiError {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch err.(type) {
 | 
						switch errors.Cause(err).(type) {
 | 
				
			||||||
	case promql.ErrQueryCanceled:
 | 
						case promql.ErrQueryCanceled:
 | 
				
			||||||
		return &apiError{errorCanceled, err}
 | 
							return &apiError{errorCanceled, err}
 | 
				
			||||||
	case promql.ErrQueryTimeout:
 | 
						case promql.ErrQueryTimeout:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1193,7 +1193,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, testLabelAPI
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			response: map[string][]metadata{
 | 
								response: map[string][]metadata{
 | 
				
			||||||
				"go_threads": []metadata{
 | 
									"go_threads": {
 | 
				
			||||||
					{textparse.MetricTypeGauge, "Number of OS threads created", ""},
 | 
										{textparse.MetricTypeGauge, "Number of OS threads created", ""},
 | 
				
			||||||
					{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
 | 
										{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
| 
						 | 
					@ -1279,7 +1279,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, testLabelAPI
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			response: map[string][]metadata{
 | 
								response: map[string][]metadata{
 | 
				
			||||||
				"go_threads": []metadata{
 | 
									"go_threads": {
 | 
				
			||||||
					{textparse.MetricTypeGauge, "Number of OS threads created", ""},
 | 
										{textparse.MetricTypeGauge, "Number of OS threads created", ""},
 | 
				
			||||||
					{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
 | 
										{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
| 
						 | 
					@ -2719,6 +2719,42 @@ func TestTSDBStatus(t *testing.T) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestReturnAPIError(t *testing.T) {
 | 
				
			||||||
 | 
						cases := []struct {
 | 
				
			||||||
 | 
							err      error
 | 
				
			||||||
 | 
							expected errorType
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								err:      promql.ErrStorage{Err: errors.New("storage error")},
 | 
				
			||||||
 | 
								expected: errorInternal,
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								err:      errors.Wrap(promql.ErrStorage{Err: errors.New("storage error")}, "wrapped"),
 | 
				
			||||||
 | 
								expected: errorInternal,
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								err:      promql.ErrQueryTimeout("timeout error"),
 | 
				
			||||||
 | 
								expected: errorTimeout,
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								err:      errors.Wrap(promql.ErrQueryTimeout("timeout error"), "wrapped"),
 | 
				
			||||||
 | 
								expected: errorTimeout,
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								err:      promql.ErrQueryCanceled("canceled error"),
 | 
				
			||||||
 | 
								expected: errorCanceled,
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								err:      errors.Wrap(promql.ErrQueryCanceled("canceled error"), "wrapped"),
 | 
				
			||||||
 | 
								expected: errorCanceled,
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								err:      errors.New("exec error"),
 | 
				
			||||||
 | 
								expected: errorExec,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, c := range cases {
 | 
				
			||||||
 | 
							actual := returnAPIError(c.err)
 | 
				
			||||||
 | 
							testutil.NotOk(t, actual)
 | 
				
			||||||
 | 
							testutil.Equals(t, c.expected, actual.typ)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This is a global to avoid the benchmark being optimized away.
 | 
					// This is a global to avoid the benchmark being optimized away.
 | 
				
			||||||
var testResponseWriter = httptest.ResponseRecorder{}
 | 
					var testResponseWriter = httptest.ResponseRecorder{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue