diff --git a/keycloak_client.go b/keycloak_client.go index ff35914..da6926b 100644 --- a/keycloak_client.go +++ b/keycloak_client.go @@ -2,6 +2,7 @@ package keycloak import ( "context" + "encoding/json" "fmt" "net/http" "net/url" @@ -165,6 +166,14 @@ func (c *Client) get(accessToken string, data interface{}, plugins ...plugin.Plu Message: string(resp.Bytes()), } case resp.StatusCode >= 400: + var response map[string]string + err := json.Unmarshal(resp.Bytes(), &response) + if message, ok := response["errorMessage"]; ok && err == nil { + return HTTPError{ + HTTPStatus: resp.StatusCode, + Message: message, + } + } return HTTPError{ HTTPStatus: resp.StatusCode, Message: string(resp.Bytes()), @@ -210,6 +219,14 @@ func (c *Client) post(accessToken string, data interface{}, plugins ...plugin.Pl Message: string(resp.Bytes()), } case resp.StatusCode >= 400: + var response map[string]string + err := json.Unmarshal(resp.Bytes(), &response) + if message, ok := response["errorMessage"]; ok && err == nil { + return "", HTTPError{ + HTTPStatus: resp.StatusCode, + Message: message, + } + } return "", HTTPError{ HTTPStatus: resp.StatusCode, Message: string(resp.Bytes()), @@ -257,6 +274,14 @@ func (c *Client) delete(accessToken string, plugins ...plugin.Plugin) error { Message: string(resp.Bytes()), } case resp.StatusCode >= 400: + var response map[string]string + err := json.Unmarshal(resp.Bytes(), &response) + if message, ok := response["errorMessage"]; ok && err == nil { + return HTTPError{ + HTTPStatus: resp.StatusCode, + Message: message, + } + } return HTTPError{ HTTPStatus: resp.StatusCode, Message: string(resp.Bytes()), @@ -297,6 +322,14 @@ func (c *Client) put(accessToken string, plugins ...plugin.Plugin) error { Message: string(resp.Bytes()), } case resp.StatusCode >= 400: + var response map[string]string + err := json.Unmarshal(resp.Bytes(), &response) + if message, ok := response["errorMessage"]; ok && err == nil { + return HTTPError{ + HTTPStatus: resp.StatusCode, + Message: message, + } + } return HTTPError{ HTTPStatus: resp.StatusCode, Message: string(resp.Bytes()),