Skip to content

Commit

Permalink
#215 Validate fuctions in target against real Graphite installation i… (
Browse files Browse the repository at this point in the history
#416)

* #215 Validate fuctions in target against real Graphite installation in remote triggers
* Remove Target in ErrRemoteTriggerResponse
* Fix function ErrRemoteTriggerResponse.Error()
* Update tests
* Added loggining remote ErrRemoteTriggerResponse.Target

* #215 Validate fuctions in target against real Graphite installation in remote triggers
* Added point in text for function ErrorRemoteServerUnavailable in struct ErrorResponse.StatusText

* #215 Validate fuctions in target against real Graphite installation in remote triggers
* New get type err

* #215 Validate fuctions in target against real Graphite installation in remote triggers
* Create logging function for error remote server

* #215 Validate fuctions in target against real Graphite installation in remote triggers
* Rename function writeToLogErrorRemoteServer to loggingErrorRemoteServer

* #416 * Remove function loggingErrorRemoteServer
* Update ErrorResponse.StatusText in function ErrorRemoteServerUnavailable

* #416 Remove check type cast t, ok := err.(remote.ErrRemoteTriggerResponse)

* #416 Remove comment

* #416 Update switch in updateTrigger

* #416 Rename type of error

* #416 Rename type of error

* #416 Rename switch err
  • Loading branch information
Nixolay authored and beevee committed Sep 3, 2019
1 parent 8890e9e commit a879f8a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ErrorRemoteServerUnavailable(err error) *ErrorResponse {
return &ErrorResponse{
Err: err,
HTTPStatusCode: 503,
StatusText: "Remote server unavailable, error: %s",
StatusText: "Remote server unavailable.",
ErrorText: fmt.Sprintf("Remote server error, please contact administrator. Raw error: %s", err.Error()),
}
}
Expand Down
6 changes: 4 additions & 2 deletions api/handler/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ func updateTrigger(writer http.ResponseWriter, request *http.Request) {
triggerID := middleware.GetTriggerID(request)
trigger := &dto.Trigger{}
if err := render.Bind(request, trigger); err != nil {
switch err.(type) {
switch err := err.(type) {
case local.ErrParseExpr, local.ErrEvalExpr, local.ErrUnknownFunction:
render.Render(writer, request, api.ErrorInvalidRequest(fmt.Errorf("invalid graphite targets: %s", err.Error())))
case expression.ErrInvalidExpression:
render.Render(writer, request, api.ErrorInvalidRequest(fmt.Errorf("invalid expression: %s", err.Error())))
case api.ErrInvalidRequestContent:
render.Render(writer, request, api.ErrorInvalidRequest(err))
case remote.ErrRemoteTriggerResponse:
render.Render(writer, request, api.ErrorRemoteServerUnavailable(err))
response := api.ErrorRemoteServerUnavailable(err)
middleware.GetLoggerEntry(request).Error("%s : %s : %s", response.StatusText, response.ErrorText, err.Target)
render.Render(writer, request, response)
default:
render.Render(writer, request, api.ErrorInternalServer(err))
}
Expand Down
4 changes: 2 additions & 2 deletions checker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ func TestHandleTriggerCheck(t *testing.T) {
EventTimestamp: time.Now().Add(-1 * time.Hour).Unix(),
LastSuccessfulCheckTimestamp: now.Add(-1 * time.Minute).Unix(),
}
actual, err := triggerChecker.handleCheckResult(checkData, remote.ErrRemoteTriggerResponse{InternalError: fmt.Errorf("pain"), Target: "pain.target"})
actual, err := triggerChecker.handleCheckResult(checkData, remote.ErrRemoteTriggerResponse{InternalError: fmt.Errorf("pain")})
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Expand All @@ -1250,7 +1250,7 @@ func TestHandleTriggerCheck(t *testing.T) {
LastSuccessfulCheckTimestamp: now.Add(-10 * time.Minute).Unix(),
}
dataBase.EXPECT().PushNotificationEvent(gomock.Any(), true).Return(nil)
actual, err := triggerChecker.handleCheckResult(checkData, remote.ErrRemoteTriggerResponse{InternalError: fmt.Errorf("pain"), Target: "pain.target"})
actual, err := triggerChecker.handleCheckResult(checkData, remote.ErrRemoteTriggerResponse{InternalError: fmt.Errorf("pain")})
So(err, ShouldBeNil)
So(actual, ShouldResemble, expected)
})
Expand Down
2 changes: 1 addition & 1 deletion metric_source/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ErrRemoteTriggerResponse struct {

// Error is a representation of Error interface method
func (err ErrRemoteTriggerResponse) Error() string {
return fmt.Sprintf("failed to get remote target '%s': %s", err.Target, err.InternalError.Error())
return err.InternalError.Error()
}

// Remote is implementation of MetricSource interface, which implements fetch metrics method from remote graphite installation
Expand Down
6 changes: 3 additions & 3 deletions metric_source/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ func TestFetch(t *testing.T) {
remote := Remote{client: server.Client(), config: &Config{URL: server.URL}}
result, err := remote.Fetch(target, from, until, false)
So(result, ShouldBeEmpty)
So(err.Error(), ShouldResemble, "failed to get remote target 'foo.bar': invalid character 'S' looking for beginning of value")
So(err.Error(), ShouldResemble, "invalid character 'S' looking for beginning of value")
})

Convey("Fail request with InternalServerError", t, func() {
server := createServer([]byte("Some string"), http.StatusInternalServerError)
remote := Remote{client: server.Client(), config: &Config{URL: server.URL}}
result, err := remote.Fetch(target, from, until, false)
So(result, ShouldBeEmpty)
So(err.Error(), ShouldResemble, fmt.Sprintf("failed to get remote target 'foo.bar': bad response status %d: %s", http.StatusInternalServerError, "Some string"))
So(err.Error(), ShouldResemble, fmt.Sprintf("bad response status %d: %s", http.StatusInternalServerError, "Some string"))
})

Convey("Fail make request", t, func() {
url := "💩%$&TR"
remote := Remote{config: &Config{URL: url}}
result, err := remote.Fetch(target, from, until, false)
So(result, ShouldBeEmpty)
So(err.Error(), ShouldResemble, "failed to get remote target 'foo.bar': parse 💩%$&TR: invalid URL escape \"%$&\"")
So(err.Error(), ShouldResemble, "parse 💩%$&TR: invalid URL escape \"%$&\"")
})
}

0 comments on commit a879f8a

Please sign in to comment.