Skip to content

Commit

Permalink
go-concourse: configs: JSON parsing: improve error message given to t…
Browse files Browse the repository at this point in the history
…he end user

Signed-off-by: aliculPix4D <[email protected]>
  • Loading branch information
aliculPix4D committed Oct 4, 2024
1 parent a8c6204 commit 3cf91e9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
39 changes: 28 additions & 11 deletions go-concourse/concourse/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package concourse
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -85,20 +86,36 @@ func (team *team) CreateOrUpdatePipelineConfig(pipelineRef atc.PipelineRef, conf

switch response.StatusCode {
case http.StatusOK, http.StatusCreated:
configResponse := setConfigResponse{}
err = json.Unmarshal(body, &configResponse)
if err != nil {
return false, false, []ConfigWarning{}, err
if response.Header.Get("Content-Type") == "application/json" {
configResponse := setConfigResponse{}
err = json.Unmarshal(body, &configResponse)
if err != nil {
return false, false, []ConfigWarning{}, fmt.Errorf("client: unable to JSON parse the response body: %w", err)
}
created := response.StatusCode == http.StatusCreated
return created, !created, configResponse.Warnings, nil
} else {
return false, false, []ConfigWarning{}, internal.UnexpectedResponseError{
StatusCode: response.StatusCode,
Status: response.Status,
Body: string(body),
}
}
created := response.StatusCode == http.StatusCreated
return created, !created, configResponse.Warnings, nil
case http.StatusBadRequest:
var validationErr atc.SaveConfigResponse
err = json.Unmarshal(body, &validationErr)
if err != nil {
return false, false, []ConfigWarning{}, err
if response.Header.Get("Content-Type") == "application/json" {
var validationErr atc.SaveConfigResponse
err = json.Unmarshal(body, &validationErr)
if err != nil {
return false, false, []ConfigWarning{}, fmt.Errorf("client: unable to JSON parse the response body: %w", err)
}
return false, false, []ConfigWarning{}, InvalidConfigError{Errors: validationErr.Errors}
} else {
return false, false, []ConfigWarning{}, internal.UnexpectedResponseError{
StatusCode: response.StatusCode,
Status: response.Status,
Body: string(body),
}
}
return false, false, []ConfigWarning{}, InvalidConfigError{Errors: validationErr.Errors}
case http.StatusForbidden:
return false, false, []ConfigWarning{}, internal.ForbiddenError{
Reason: string(body),
Expand Down
37 changes: 37 additions & 0 deletions go-concourse/concourse/configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ var _ = Describe("ATC Handler Configs", func() {
expectedVersion string
expectedConfig []byte

contentType string
returnHeader int
returnBody []byte

Expand Down Expand Up @@ -204,6 +205,10 @@ var _ = Describe("ATC Handler Configs", func() {

Expect(receivedConfig).To(Equal(expectedConfig))

if contentType != "" {
w.Header().Set("Content-Type", contentType)
}

w.WriteHeader(returnHeader)
w.Write(returnBody)
},
Expand All @@ -213,6 +218,7 @@ var _ = Describe("ATC Handler Configs", func() {

Context("when creating a new config", func() {
BeforeEach(func() {
contentType = "application/json"
returnHeader = http.StatusCreated
returnBody = []byte(`{"warnings":[
{"type": "warning-1-type", "message": "fake-warning1"},
Expand Down Expand Up @@ -245,6 +251,20 @@ var _ = Describe("ATC Handler Configs", func() {
It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("client: unable to JSON parse the response body"))
})
})

Context("when response does not contain application/json header", func() {
BeforeEach(func() {
contentType = "text/plain"
returnBody = []byte(`server error`)
})

It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("server error"))
})
})

Expand Down Expand Up @@ -286,6 +306,7 @@ var _ = Describe("ATC Handler Configs", func() {

Context("when updating a config", func() {
BeforeEach(func() {
contentType = "application/json"
returnHeader = http.StatusOK
returnBody = []byte(`{"warnings":[
{"type": "warning-1-type", "message": "fake-warning1"},
Expand Down Expand Up @@ -318,6 +339,7 @@ var _ = Describe("ATC Handler Configs", func() {
It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("client: unable to JSON parse the response body"))
})
})

Expand Down Expand Up @@ -359,6 +381,7 @@ var _ = Describe("ATC Handler Configs", func() {

Context("when setting config returns bad request", func() {
BeforeEach(func() {
contentType = "application/json"
returnHeader = http.StatusBadRequest
returnBody = []byte(`{"errors":["fake-error1","fake-error2"]}`)
})
Expand All @@ -378,6 +401,20 @@ var _ = Describe("ATC Handler Configs", func() {
It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("client: unable to JSON parse the response body"))
})
})

Context("when response does not contain application/json header", func() {
BeforeEach(func() {
contentType = "text/plain"
returnBody = []byte(`server error`)
})

It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("server error"))
})
})
})
Expand Down

0 comments on commit 3cf91e9

Please sign in to comment.