From d092780ae4eb369a15a063aa7990a833e82cf267 Mon Sep 17 00:00:00 2001 From: Aishwarya Kaneri Date: Wed, 23 Jan 2019 17:20:19 +0530 Subject: [PATCH] [Aishwarya|Jenson] handle authorization error in schedule job --- daemon/client.go | 21 +++++++++++++-------- daemon/client_test.go | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/daemon/client.go b/daemon/client.go index f03b0148..08e29a47 100644 --- a/daemon/client.go +++ b/daemon/client.go @@ -104,8 +104,7 @@ func (c *client) ScheduleJob(name, tags, time, notificationEmails, group string, defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { - err := getHttpResponseError(resp.Body) - return "", err + return "", buildHTTPError(c, resp) } var scheduledJob ScheduleJobPayload @@ -403,15 +402,21 @@ func buildHTTPError(c *client, resp *http.Response) error { return fmt.Errorf("%s\n%s", utility.UnauthorizedErrorHeader, utility.UnauthorizedErrorMissingConfig) } return fmt.Errorf("%s\n%s", utility.UnauthorizedErrorHeader, utility.UnauthorizedErrorInvalidConfig) - } else if resp.StatusCode == http.StatusBadRequest { + } + + if resp.StatusCode == http.StatusBadRequest { return getHttpResponseError(resp.Body) - } else if resp.StatusCode == http.StatusNoContent { + } + + if resp.StatusCode == http.StatusNoContent { return fmt.Errorf(utility.NoScheduledJobsError) - } else if resp.StatusCode == http.StatusNotFound { + } + + if resp.StatusCode == http.StatusNotFound { return fmt.Errorf(utility.JobNotFoundError) - } else { - return fmt.Errorf("%s\nStatus Code: %d, %s", utility.GenericResponseErrorHeader, resp.StatusCode, http.StatusText(resp.StatusCode)) - } + } + + return fmt.Errorf("%s\nStatus Code: %d, %s", utility.GenericResponseErrorHeader, resp.StatusCode, http.StatusText(resp.StatusCode)) } func getHttpResponseError(response io_reader.ReadCloser) error { diff --git a/daemon/client_test.go b/daemon/client_test.go index 9367653d..032c397b 100644 --- a/daemon/client_test.go +++ b/daemon/client_test.go @@ -357,7 +357,7 @@ func (s *ClientTestSuite) TestSchedulingAlreadyExistedScheduledJob() { "POST", "http://"+proctorConfig.Host+"/jobs/schedule", func(req *http.Request) (*http.Response, error) { - return httpmock.NewStringResponse(409, "provided duplicate combination of job name and args for scheduling"), nil + return httpmock.NewStringResponse(409, "Server Error!!!\nStatus Code: 409, Conflict"), nil }, ).WithHeader( &http.Header{ @@ -371,7 +371,7 @@ func (s *ClientTestSuite) TestSchedulingAlreadyExistedScheduledJob() { s.mockConfigLoader.On("Load").Return(proctorConfig, config.ConfigError{}).Once() _, err := s.testClient.ScheduleJob(procName, tags, time, notificationEmails, group, procArgs) - assert.Equal(t, "provided duplicate combination of job name and args for scheduling", err.Error()) + assert.Equal(t, "Server Error!!!\nStatus Code: 409, Conflict", err.Error()) s.mockConfigLoader.AssertExpectations(t) }