From 834a380d803ae9ce59ce5ee233fa3a78a984fe68 Mon Sep 17 00:00:00 2001 From: Kang Ming Date: Wed, 4 Dec 2024 23:35:39 +0800 Subject: [PATCH] fix: return the error code instead of status code (#1855) ## What kind of change does this PR introduce? * Return the error code for in the redirect URL instead of the status code --- internal/api/external.go | 3 +-- internal/api/verify.go | 4 ++-- internal/api/verify_test.go | 6 +++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/api/external.go b/internal/api/external.go index 2eff891eff..768343d225 100644 --- a/internal/api/external.go +++ b/internal/api/external.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "net/url" - "strconv" "strings" "time" @@ -635,7 +634,7 @@ func getErrorQueryString(err error, errorID string, log logrus.FieldLogger, q ur log.WithError(e.Cause()).Info(e.Error()) } q.Set("error_description", e.Message) - q.Set("error_code", strconv.Itoa(e.HTTPStatus)) + q.Set("error_code", e.ErrorCode) case *OAuthError: q.Set("error", e.Err) q.Set("error_description", e.Description) diff --git a/internal/api/verify.go b/internal/api/verify.go index ad5a7d096e..c4e8fa2523 100644 --- a/internal/api/verify.go +++ b/internal/api/verify.go @@ -439,10 +439,10 @@ func (a *API) prepErrorRedirectURL(err *HTTPError, r *http.Request, rurl string, hq.Set("error", str) q.Set("error", str) } - hq.Set("error_code", strconv.Itoa(err.HTTPStatus)) + hq.Set("error_code", err.ErrorCode) hq.Set("error_description", err.Message) - q.Set("error_code", strconv.Itoa(err.HTTPStatus)) + q.Set("error_code", err.ErrorCode) q.Set("error_description", err.Message) if flowType == models.PKCEFlow { // Additionally, may override existing error query param if set to PKCE. diff --git a/internal/api/verify_test.go b/internal/api/verify_test.go index ea0892098a..59111ef068 100644 --- a/internal/api/verify_test.go +++ b/internal/api/verify_test.go @@ -305,7 +305,7 @@ func (ts *VerifyTestSuite) TestExpiredConfirmationToken() { f, err := url.ParseQuery(rurl.Fragment) require.NoError(ts.T(), err) - assert.Equal(ts.T(), "403", f.Get("error_code")) + assert.Equal(ts.T(), ErrorCodeOTPExpired, f.Get("error_code")) assert.Equal(ts.T(), "Email link is invalid or has expired", f.Get("error_description")) assert.Equal(ts.T(), "access_denied", f.Get("error")) } @@ -824,7 +824,7 @@ func (ts *VerifyTestSuite) TestVerifyBannedUser() { f, err := url.ParseQuery(rurl.Fragment) require.NoError(ts.T(), err) - assert.Equal(ts.T(), "403", f.Get("error_code")) + assert.Equal(ts.T(), ErrorCodeUserBanned, f.Get("error_code")) }) } } @@ -1145,7 +1145,7 @@ func (ts *VerifyTestSuite) TestPrepRedirectURL() { func (ts *VerifyTestSuite) TestPrepErrorRedirectURL() { const DefaultError = "Invalid redirect URL" - redirectError := fmt.Sprintf("error=invalid_request&error_code=400&error_description=%s", url.QueryEscape(DefaultError)) + redirectError := fmt.Sprintf("error=invalid_request&error_code=validation_failed&error_description=%s", url.QueryEscape(DefaultError)) cases := []struct { desc string