Use correct has error check for internal responses (#28796) (#28798)

Backport #28796 by @wxiaoguang

`resp != nil` doesn't mean the request really succeeded. Add a comment
for requestJSONResp to clarify the behavior.

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
(cherry picked from commit cbf366643b)
This commit is contained in:
Giteabot 2024-01-15 20:13:35 +08:00 committed by Earl Warren
parent 367561fff2
commit 215c96e646
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
4 changed files with 4 additions and 3 deletions

View file

@ -22,7 +22,7 @@ func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, Resp
}) })
resp, extra := requestJSONResp(req, &responseText{}) resp, extra := requestJSONResp(req, &responseText{})
if resp == nil { if extra.HasError() {
return "", extra return "", extra
} }
return resp.Text, extra return resp.Text, extra

View file

@ -27,7 +27,7 @@ func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string,
req := newInternalRequest(ctx, reqURL, "POST") req := newInternalRequest(ctx, reqURL, "POST")
req.Param("content", content) req.Param("content", content)
resp, extra := requestJSONResp(req, &responseText{}) resp, extra := requestJSONResp(req, &responseText{})
if resp == nil { if extra.HasError() {
return "", extra return "", extra
} }
return resp.Text, extra return resp.Text, extra

View file

@ -30,7 +30,7 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
}) })
resp, extra := requestJSONResp(req, &responseText{}) resp, extra := requestJSONResp(req, &responseText{})
if resp == nil { if extra.HasError() {
return "", extra return "", extra
} }
return resp.Text, extra return resp.Text, extra

View file

@ -47,6 +47,7 @@ func (re responseError) Error() string {
// requestJSONResp sends a request to the gitea server and then parses the response. // requestJSONResp sends a request to the gitea server and then parses the response.
// If the status code is not 2xx, or any error occurs, the ResponseExtra.Error field is guaranteed to be non-nil, // If the status code is not 2xx, or any error occurs, the ResponseExtra.Error field is guaranteed to be non-nil,
// and the ResponseExtra.UserMsg field will be set to a message for the end user. // and the ResponseExtra.UserMsg field will be set to a message for the end user.
// Caller should check the ResponseExtra.HasError() first to see whether the request fails.
// //
// * If the "res" is a struct pointer, the response will be parsed as JSON // * If the "res" is a struct pointer, the response will be parsed as JSON
// * If the "res" is responseText pointer, the response will be stored as text in it // * If the "res" is responseText pointer, the response will be stored as text in it