Skip to content

Commit

Permalink
[management] improve zitadel idp error response detail by decoding er…
Browse files Browse the repository at this point in the history
…rors
  • Loading branch information
adasauce committed Sep 23, 2024
1 parent d47be15 commit 1176f2a
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions management/server/idp/zitadel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type ZitadelCredentials struct {
appMetrics telemetry.AppMetrics
}

type zitadelErrorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}

// zitadelEmail specifies details of a user email.
type zitadelEmail struct {
Email string `json:"email"`
Expand Down Expand Up @@ -489,7 +494,10 @@ func (zm *ZitadelManager) post(ctx context.Context, resource string, body string
zm.appMetrics.IDPMetrics().CountRequestStatusError()
}

return nil, fmt.Errorf("unable to post %s, statusCode %d", reqURL, resp.StatusCode)
bodyBytes, _ := io.ReadAll(resp.Body)
zErr := zm.readZitadelError(bodyBytes)

return bodyBytes, fmt.Errorf("unable to post %s, statusCode %d, zitadel: %w", reqURL, resp.StatusCode, zErr)
}

return io.ReadAll(resp.Body)
Expand Down Expand Up @@ -561,12 +569,25 @@ func (zm *ZitadelManager) get(ctx context.Context, resource string, q url.Values
zm.appMetrics.IDPMetrics().CountRequestStatusError()
}

return nil, fmt.Errorf("unable to get %s, statusCode %d", reqURL, resp.StatusCode)
bodyBytes, _ := io.ReadAll(resp.Body)
zErr := zm.readZitadelError(bodyBytes)

return bodyBytes, fmt.Errorf("unable to get %s, statusCode %d, zitadel: %w", reqURL, resp.StatusCode, zErr)
}

return io.ReadAll(resp.Body)
}

func (zm *ZitadelManager) readZitadelError(errorBody []byte) error {
var zitadelErr zitadelErrorResponse
err := zm.helper.Unmarshal(errorBody, &zitadelErr)
if err != nil {
return fmt.Errorf("error unparsable body: %s", errorBody)
}

return fmt.Errorf("error code: %d message: %s", zitadelErr.Code, zitadelErr.Message)
}

// userData construct user data from zitadel profile.
func (zp zitadelProfile) userData() *UserData {
var (
Expand Down

0 comments on commit 1176f2a

Please sign in to comment.