Skip to content

Commit

Permalink
[GH-336] - Fix Unmarshal JSON error as string
Browse files Browse the repository at this point in the history
  • Loading branch information
developerkunal committed Apr 25, 2024
1 parent f3560ac commit fa97b3f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions authentication/authentication_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func (a *Error) UnmarshalJSON(b []byte) error {
type authError Error
type authErrorWrapper struct {
*authError
Code string `json:"code"`
Description string `json:"description"`
Code string `json:"code"`
Description json.RawMessage `json:"description"` // Can be string or object
}

alias := &authErrorWrapper{(*authError)(a), "", ""}
alias := &authErrorWrapper{(*authError)(a), "", nil}

err := json.Unmarshal(b, alias)
if err != nil {
Expand All @@ -69,8 +69,14 @@ func (a *Error) UnmarshalJSON(b []byte) error {
a.Err = alias.Code
}

if alias.Description != "" {
a.Message = alias.Description
if len(alias.Description) > 0 {
var descText string
err := json.Unmarshal(alias.Description, &descText)
if err == nil {
a.Message = descText
} else {
a.Message = string(alias.Description)
}
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions authentication/authentication_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ func Test_newError(t *testing.T) {
Message: "Invalid sign up",
},
},
{
name: "it will handle invalid password response",
givenResponse: http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser((strings.NewReader(`{"name":"PasswordStrengthError","message":"Password is too weak","code":"invalid_password","description":{"rules":[{"message":"At least %d characters in length","format":[8],"code":"lengthAtLeast","verified":true},{"message":"Contain at least %d of the following %d types of characters:","code":"containsAtLeast","format":[3,4],"items":[{"message":"lower case letters (a-z)","code":"lowerCase","verified":true},{"message":"upper case letters (A-Z)","code":"upperCase","verified":false},{"message":"numbers (i.e. 0-9)","code":"numbers","verified":false},{"message":"special characters (e.g. !@#$%^&*)","code":"specialCharacters","verified":true}],"verified":false}],"verified":false},"policy":"* At least 8 characters in length\n* Contain at least 3 of the following 4 types of characters:\n * lower case letters (a-z)\n * upper case letters (A-Z)\n * numbers (i.e. 0-9)\n * special characters (e.g. !@#$%^&*)","statusCode":400}`))),
},
expectedError: Error{
StatusCode: 400,
Err: "invalid_password",
Message: `{"rules":[{"message":"At least %d characters in length","format":[8],"code":"lengthAtLeast","verified":true},{"message":"Contain at least %d of the following %d types of characters:","code":"containsAtLeast","format":[3,4],"items":[{"message":"lower case letters (a-z)","code":"lowerCase","verified":true},{"message":"upper case letters (A-Z)","code":"upperCase","verified":false},{"message":"numbers (i.e. 0-9)","code":"numbers","verified":false},{"message":"special characters (e.g. !@#$%^&*)","code":"specialCharacters","verified":true}],"verified":false}],"verified":false}`,
},
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit fa97b3f

Please sign in to comment.