Skip to content

Commit

Permalink
Fix item merging bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Silcott committed Sep 4, 2021
1 parent 0e41655 commit 962d83a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 7 additions & 10 deletions replier.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewReplier(manifests []ErrorManifest, options ...Option) *Replier {
activeTransferObject := &defaultReplyTransferObject{}

replier := Replier{
errorManifest: mergeManifestCollections(manifests...),
errorManifest: mergeManifestCollections(manifests),
transferObject: activeTransferObject,
}

Expand Down Expand Up @@ -220,27 +220,24 @@ func sendHTTPResponse(writer http.ResponseWriter, transferObject TransferObject)

// mergeManifestCollections handles merges the passed manifests into a singular
// map
func mergeManifestCollections(manifests ...ErrorManifest) ErrorManifest {
func mergeManifestCollections(manifests []ErrorManifest) ErrorManifest {

mergedManifests := make(ErrorManifest)

for _, manifest := range manifests {
key, value := getManifestAttributes(manifest)
mergedManifests[key] = *value
getManifestItems(manifest, mergedManifests)
}

return mergedManifests
}

// getManifestAttributes returns key and value for pass manifest
func getManifestAttributes(manifest ErrorManifest) (key string, value *ErrorManifestItem) {
// getManifestItems pulls the key and items from the manifest and inserts into final manifest
func getManifestItems(manifest ErrorManifest, finalManifest ErrorManifest) {

for k, v := range manifest {
key = k
value = &v
for key, item := range manifest {
finalManifest[key] = item
}

return key, value
}

// getInternalServertErrorManifestItem returns typical 500 error with text and message
Expand Down
6 changes: 5 additions & 1 deletion replier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,13 @@ func TestReplier_NewHTTPResponseForError(t *testing.T) {
}{
{
name: "Success - Resource not found",
manifests: []reply.ErrorManifest{
manifests: append([]reply.ErrorManifest{
{"test-404-error": reply.ErrorManifestItem{Message: "resource not found", StatusCode: http.StatusNotFound}},
},
reply.ErrorManifest{
"test-401-error": reply.ErrorManifestItem{Message: "unauthorized", StatusCode: http.StatusUnauthorized},
},
),
err: errors.New("test-404-error"),
expectedStatusCode: http.StatusNotFound,
assertResponse: func(w *httptest.ResponseRecorder, t *testing.T) {
Expand Down

0 comments on commit 962d83a

Please sign in to comment.