summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-01-15 20:13:35 +0800
committerGitHub <noreply@github.com>2024-01-15 13:13:35 +0100
commitcbf366643bfbc89a1fbc8a149e31abf19c60d6a9 (patch)
treeb85a6b740c945c11b788985539ec8c39de8a3d7d
parentdf694f6a7d72141e11f12a9cba6f0850a7e8e186 (diff)
downloadgitea-cbf366643bfbc89a1fbc8a149e31abf19c60d6a9.tar.gz
gitea-cbf366643bfbc89a1fbc8a149e31abf19c60d6a9.zip
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>
-rw-r--r--modules/private/actions.go2
-rw-r--r--modules/private/key.go2
-rw-r--r--modules/private/mail.go2
-rw-r--r--modules/private/request.go1
4 files changed, 4 insertions, 3 deletions
diff --git a/modules/private/actions.go b/modules/private/actions.go
index 4ec77dc936..a22833632e 100644
--- a/modules/private/actions.go
+++ b/modules/private/actions.go
@@ -22,7 +22,7 @@ func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, Resp
})
resp, extra := requestJSONResp(req, &responseText{})
- if resp == nil {
+ if extra.HasError() {
return "", extra
}
return resp.Text, extra
diff --git a/modules/private/key.go b/modules/private/key.go
index aa1e8aa56f..08762bd401 100644
--- a/modules/private/key.go
+++ b/modules/private/key.go
@@ -27,7 +27,7 @@ func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string,
req := newInternalRequest(ctx, reqURL, "POST")
req.Param("content", content)
resp, extra := requestJSONResp(req, &responseText{})
- if resp == nil {
+ if extra.HasError() {
return "", extra
}
return resp.Text, extra
diff --git a/modules/private/mail.go b/modules/private/mail.go
index 699f5e5f42..ac55d6fe4d 100644
--- a/modules/private/mail.go
+++ b/modules/private/mail.go
@@ -30,7 +30,7 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
})
resp, extra := requestJSONResp(req, &responseText{})
- if resp == nil {
+ if extra.HasError() {
return "", extra
}
return resp.Text, extra
diff --git a/modules/private/request.go b/modules/private/request.go
index d3f99381a6..2bc43b972d 100644
--- a/modules/private/request.go
+++ b/modules/private/request.go
@@ -47,6 +47,7 @@ func (re responseError) Error() string {
// 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,
// 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 responseText pointer, the response will be stored as text in it