Skip to content

Commit

Permalink
fix: avoid runtime panic when the errors field of the response is `…
Browse files Browse the repository at this point in the history
…nil`

Signed-off-by: Norbert Biczo <[email protected]>
  • Loading branch information
pyrooka authored and padamstx committed Jan 7, 2025
1 parent cac0b03 commit 9c104b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/base_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,11 @@ func getErrorMessage(responseMap map[string]interface{}, statusCode int) string
var errors Errors
responseBuffer, _ := json.Marshal(responseMap)
if err := json.Unmarshal(responseBuffer, &errors); err == nil {
return errors.Errors[0].Message
if len(errors.Errors) > 0 {
return errors.Errors[0].Message
}

return ""
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/base_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,8 @@ func TestErrorMessage(t *testing.T) {
testGetErrorMessage(t, http.StatusInternalServerError,
`{"errorMessage":{"statusCode":500,"message":"Internal Server Error"}}`,
"Internal Server Error")

testGetErrorMessage(t, http.StatusInternalServerError, `{"errors": null}`, "")
}

func getTLSVersion(service *BaseService) int {
Expand Down

0 comments on commit 9c104b3

Please sign in to comment.