Browse Source

in case of error, parse it and include in the message only the errorMessage part

master
bsoniam 7 years ago
committed by Francis PEROT
parent
commit
1c7676253d
  1. 33
      keycloak_client.go

33
keycloak_client.go

@ -2,6 +2,7 @@ package keycloak
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -165,6 +166,14 @@ func (c *Client) get(accessToken string, data interface{}, plugins ...plugin.Plu
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: 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{ return HTTPError{
HTTPStatus: resp.StatusCode, HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
@ -210,6 +219,14 @@ func (c *Client) post(accessToken string, data interface{}, plugins ...plugin.Pl
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: 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{ return "", HTTPError{
HTTPStatus: resp.StatusCode, HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
@ -257,6 +274,14 @@ func (c *Client) delete(accessToken string, plugins ...plugin.Plugin) error {
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: 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{ return HTTPError{
HTTPStatus: resp.StatusCode, HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
@ -297,6 +322,14 @@ func (c *Client) put(accessToken string, plugins ...plugin.Plugin) error {
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: 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{ return HTTPError{
HTTPStatus: resp.StatusCode, HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()), Message: string(resp.Bytes()),

Loading…
Cancel
Save