diff --git a/authentication/authentication_error.go b/authentication/authentication_error.go index 8c24446c..1b208214 100644 --- a/authentication/authentication_error.go +++ b/authentication/authentication_error.go @@ -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 { @@ -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 diff --git a/authentication/authentication_error_test.go b/authentication/authentication_error_test.go index 7b657f50..a454ce98 100644 --- a/authentication/authentication_error_test.go +++ b/authentication/authentication_error_test.go @@ -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 {