Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[management] improve zitadel idp error response detail by decoding errors #2634

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
bcmmbaga marked this conversation as resolved.
Show resolved Hide resolved
}

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)
bcmmbaga marked this conversation as resolved.
Show resolved Hide resolved
}

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)
}

bcmmbaga marked this conversation as resolved.
Show resolved Hide resolved
// userData construct user data from zitadel profile.
func (zp zitadelProfile) userData() *UserData {
var (
Expand Down
Loading