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

fix: error messages in OpenAPI/Swagger / improve error messages from failed webhooks and client timeouts #3218

Merged
merged 4 commits into from Apr 14, 2023

Conversation

alnr
Copy link
Contributor

@alnr alnr commented Apr 6, 2023

Partial fix for ory-corp/cloud#4354.

ory/herodot@v0.9.13...v0.10.2
ory/x@v0.0.549...v0.0.551

Previously, the Swagger and OpenAPI specs would incorrectly describe
specific errors returned from Kratos' API. Instead of having a nested
"error" key in the returned JSON payload, the API specs would incorrectly
say the contents of the generic error were flattened into the payload.

Example before (incorrect):

{
  "id": "browser_location_change_required",
  "code": 422,
  "status": "Unprocessable Entity",
  "reason": "In order to complete this flow please redirect the browser to: /ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083",
  "message": "browser location change required",
  "redirect_browser_to": "/ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083"
}

Actual Kratos response (correct, unchanged):

{
  "error": {
    "id": "browser_location_change_required",
    "code": 422,
    "status": "Unprocessable Entity",
    "reason": "In order to complete this flow please redirect the browser to: /ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083",
    "message": "browser location change required"
  },
  "redirect_browser_to": "/ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083"
}

This issue occurs because go-swagger does not interpret embedded structs with json tags correctly, i.e., it flattens the embedded struct's fields into outer type instead of nesting it under the key specified in the json tag.

The least bad fix is to use modified copies of the affected error structs just for Swagger API spec generation.

selfservice/hook/web_hook.go Outdated Show resolved Hide resolved
@alnr alnr requested a review from Benehiko April 12, 2023 15:57
@alnr alnr changed the title fix: improve error messages from failed webhooks and client timeouts fix: error messages in OpenAPI/Swagger / improve error messages from failed webhooks and client timeouts Apr 12, 2023
zepatrik
zepatrik previously approved these changes Apr 12, 2023
Copy link
Member

@zepatrik zepatrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice 👍

x/pointer.go Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Apr 12, 2023

Codecov Report

Merging #3218 (12ba299) into master (c15de85) will increase coverage by 0.00%.
The diff coverage is 74.50%.

❗ Current head 12ba299 differs from pull request most recent head df95327. Consider uploading reports for the commit df95327 to get more accurate results

@@           Coverage Diff           @@
##           master    #3218   +/-   ##
=======================================
  Coverage   77.89%   77.89%           
=======================================
  Files         319      319           
  Lines       20431    20443   +12     
=======================================
+ Hits        15914    15925   +11     
- Misses       3317     3320    +3     
+ Partials     1200     1198    -2     
Impacted Files Coverage Δ
continuity/container.go 90.00% <0.00%> (ø)
selfservice/flow/error.go 96.61% <ø> (ø)
selfservice/flow/settings/error.go 77.47% <ø> (ø)
selfservice/strategy/webauthn/registration.go 64.94% <0.00%> (-1.37%) ⬇️
session/manager.go 100.00% <ø> (ø)
x/pointer.go 100.00% <ø> (+25.00%) ⬆️
selfservice/errorx/manager.go 60.86% <33.33%> (-2.77%) ⬇️
selfservice/hook/web_hook.go 78.96% <55.55%> (-1.04%) ⬇️
persistence/sql/persister_errorx.go 74.50% <100.00%> (-0.97%) ⬇️
selfservice/errorx/handler.go 92.30% <100.00%> (ø)
... and 2 more

... and 2 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@jonas-jonas
Copy link
Contributor

E2e tests fail, with 502 in webhooks, so it looks like this might related to these changes. If you haven't looked into it yet, I can try to repro this locally tomorrow (e2e test setup in Kratos is a bit finicky). We can also do a call, if you want. Let me know on slack :)

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you for your contribution! This looks pretty good and I have some ideas how to improve it further :)

spec/api.json Outdated Show resolved Hide resolved
x/swagger/swagger_types_global.go Show resolved Hide resolved
Previously, the Swagger and OpenAPI specs would incorrectly describe
specific errors returned from Kratos' API. Instead of having a nested
"error" key in the returned JSON payload, the API specs would incorrectly
say the contents of the generic error were flattened into the payload.

Example before (incorrect):
{
  "id": "browser_location_change_required",
  "code": 422,
  "status": "Unprocessable Entity",
  "reason": "In order to complete this flow please redirect the browser to: /ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083",
  "message": "browser location change required",
  "redirect_browser_to": "/ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083"
}

Actual Kratos response (correct, unchanged):
{
  "error": {
    "id": "browser_location_change_required",
    "code": 422,
    "status": "Unprocessable Entity",
    "reason": "In order to complete this flow please redirect the browser to: /ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083",
    "message": "browser location change required"
  },
  "redirect_browser_to": "/ui/settings?flow=22b3ad6f-c50a-4c2f-8c94-a16e9dc20083"
}

This issue occurs because go-swagger does not interpret embedded structs
with `json` structs correctly, i.e., it flattens the embedded struct's
fields into outer type instead of nesting it under the key specified in
the `json` tag.

The least bad fix is to use modified copies of the affected error structs
just for Swagger API spec generation.
Copy link
Contributor

@CaptainStandby CaptainStandby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@alnr
Copy link
Contributor Author

alnr commented Apr 14, 2023

CLA check is stuck 🤷 ?

@alnr alnr enabled auto-merge (rebase) April 14, 2023 08:08
@aeneasr aeneasr self-requested a review April 14, 2023 10:13
@zepatrik zepatrik disabled auto-merge April 14, 2023 13:06
@zepatrik zepatrik merged commit b1bdcd3 into master Apr 14, 2023
25 checks passed
@zepatrik zepatrik deleted the webhook-errors branch April 14, 2023 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants