1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-18 16:14:26 +00:00
gitea/modules/private/request.go
wxiaoguang f4538791f5
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes

At first, I'd like to help users like #23636 (there are a lot)

The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.

So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).

When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.

So I think it's good to make everything clear.

# Tech Backgrounds

Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.

Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.

* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.

In the old design, it assumes that:

* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.

The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.

# This PR

To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.

* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.

This PR saves more than 300 lines, while makes the git client messages
more clear.

Many gitea-serv/git-hook related essential functions are covered by
tests.

---------

Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 14:32:26 +08:00

136 lines
4.7 KiB
Go

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package private
import (
"fmt"
"io"
"net/http"
"unicode"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/json"
)
// responseText is used to get the response as text, instead of parsing it as JSON.
type responseText struct {
Text string
}
// ResponseExtra contains extra information about the response, especially for error responses.
type ResponseExtra struct {
StatusCode int
UserMsg string
Error error
}
type responseCallback func(resp *http.Response, extra *ResponseExtra)
func (re *ResponseExtra) HasError() bool {
return re.Error != nil
}
type responseError struct {
statusCode int
errorString string
}
func (re responseError) Error() string {
if re.errorString == "" {
return fmt.Sprintf("internal API error response, status=%d", re.statusCode)
}
return fmt.Sprintf("internal API error response, status=%d, err=%s", re.statusCode, re.errorString)
}
// requestJSONUserMsg 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.
//
// * 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 responseCallback pointer, the callback function should set the ResponseExtra fields accordingly
func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra ResponseExtra) {
resp, err := req.Response()
if err != nil {
extra.UserMsg = "Internal Server Connection Error"
extra.Error = fmt.Errorf("unable to contact gitea %q: %w", req.GoString(), err)
return nil, extra
}
defer resp.Body.Close()
extra.StatusCode = resp.StatusCode
// if the status code is not 2xx, try to parse the error response
if resp.StatusCode/100 != 2 {
var respErr Response
if err := json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
extra.UserMsg = "Internal Server Error Decoding Failed"
extra.Error = fmt.Errorf("unable to decode error response %q: %w", req.GoString(), err)
return nil, extra
}
extra.UserMsg = respErr.UserMsg
if extra.UserMsg == "" {
extra.UserMsg = "Internal Server Error (no message for end users)"
}
extra.Error = responseError{statusCode: resp.StatusCode, errorString: respErr.Err}
return res, extra
}
// now, the StatusCode must be 2xx
var v any = res
if respText, ok := v.(*responseText); ok {
// get the whole response as a text string
bs, err := io.ReadAll(resp.Body)
if err != nil {
extra.UserMsg = "Internal Server Response Reading Failed"
extra.Error = fmt.Errorf("unable to read response %q: %w", req.GoString(), err)
return nil, extra
}
respText.Text = string(bs)
return res, extra
} else if callback, ok := v.(*responseCallback); ok {
// pass the response to callback, and let the callback update the ResponseExtra
extra.StatusCode = resp.StatusCode
(*callback)(resp, &extra)
return nil, extra
} else if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
// decode the response into the given struct
extra.UserMsg = "Internal Server Response Decoding Failed"
extra.Error = fmt.Errorf("unable to decode response %q: %w", req.GoString(), err)
return nil, extra
}
if respMsg, ok := v.(*Response); ok {
// if the "res" is Response structure, try to get the UserMsg from it and update the ResponseExtra
extra.UserMsg = respMsg.UserMsg
if respMsg.Err != "" {
// usually this shouldn't happen, because the StatusCode is 2xx, there should be no error.
// but we still handle the "err" response, in case some people return error messages by status code 200.
extra.Error = responseError{statusCode: resp.StatusCode, errorString: respMsg.Err}
}
}
return res, extra
}
// requestJSONUserMsg sends a request to the gitea server and then parses the response as private.Response
// If the request succeeds, the successMsg will be used as part of ResponseExtra.UserMsg.
func requestJSONUserMsg(req *httplib.Request, successMsg string) ResponseExtra {
resp, extra := requestJSONResp(req, &Response{})
if extra.HasError() {
return extra
}
if resp.UserMsg == "" {
extra.UserMsg = successMsg // if UserMsg is empty, then use successMsg as userMsg
} else if successMsg != "" {
// else, now UserMsg is not empty, if successMsg is not empty, then append successMsg to UserMsg
if unicode.IsPunct(rune(extra.UserMsg[len(extra.UserMsg)-1])) {
extra.UserMsg = extra.UserMsg + " " + successMsg
} else {
extra.UserMsg = extra.UserMsg + ". " + successMsg
}
}
return extra
}