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

Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" #23687

Merged
merged 11 commits into from Mar 29, 2023

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Mar 24, 2023

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.

@wxiaoguang wxiaoguang marked this pull request as draft March 24, 2023 14:19
@yardenshoham yardenshoham added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Mar 24, 2023
@wxiaoguang wxiaoguang force-pushed the refactor-cmd branch 8 times, most recently from 528885c to 56914d4 Compare March 24, 2023 18:03
@codecov-commenter

This comment was marked as off-topic.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 24, 2023
@wxiaoguang wxiaoguang marked this pull request as ready for review March 24, 2023 18:38
@wxiaoguang
Copy link
Contributor Author

This PR is ready for review

@wxiaoguang wxiaoguang force-pushed the refactor-cmd branch 2 times, most recently from 760926f to 5c83c6b Compare March 24, 2023 19:16
@wxiaoguang wxiaoguang changed the title WIP: Refactor internal API for git commands Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" Mar 24, 2023
@wxiaoguang wxiaoguang force-pushed the refactor-cmd branch 2 times, most recently from a35f8ce to 949cb5d Compare March 25, 2023 05:34
@lunny
Copy link
Member

lunny commented Mar 25, 2023

I think something maybe wrong when push via ssh protocol but clone is OK.

ServCommand failed: internal API error response, status=403
fatal: protocol error: bad line length character:
Git

@wxiaoguang
Copy link
Contributor Author

I think something maybe wrong when push via ssh protocol but clone is OK.

ServCommand failed: internal API error response, status=403
fatal: protocol error: bad line length character:
Git

The "push" should have been covered by tests.

How do you reproduce it? According to "internal API error response, status=403", I guess it's related to some permission or protection problem during your test?

@lunny
Copy link
Member

lunny commented Mar 25, 2023

I think something maybe wrong when push via ssh protocol but clone is OK.

ServCommand failed: internal API error response, status=403
fatal: protocol error: bad line length character:
Git

The "push" should have been covered by tests.

How do you reproduce it? According to "internal API error response, status=403", I guess it's related to some permission or protection problem during your test?

Just clone and push. I'm the admin of this instance.
OK. My fault. It's a mirror repository. But I think the user message is not friendly like before.

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Mar 25, 2023

OK. My fault. It's a mirror repository. But I think the user message is not friendly like before.

My plan is to fine tune more messages in the future, and we have enough time to eat our own dogfood.

At least, it's much easier than before to provide more friendly messages to end users.


Made a fix for the log output, could you try if the new commit could output the friendly messages?

cmd/keys.go Outdated Show resolved Hide resolved
cmd/mailer.go Outdated Show resolved Hide resolved
cmd/serv.go Outdated Show resolved Hide resolved
modules/httplib/httplib.go Show resolved Hide resolved
routers/private/serv.go Show resolved Hide resolved
routers/private/serv.go Show resolved Hide resolved
routers/private/serv.go Show resolved Hide resolved
modules/private/internal.go Outdated Show resolved Hide resolved
modules/private/internal.go Outdated Show resolved Hide resolved
modules/private/request.go Show resolved Hide resolved
wxiaoguang and others added 3 commits March 26, 2023 08:47
Co-authored-by: delvh <dev.lh@web.de>
# Conflicts:
#	contrib/pr/checkout.go
@lunny
Copy link
Member

lunny commented Mar 26, 2023

$ git push origin main
Gitea: Mirror Repository lunny2/tea is read-only
ServCommand failed: internal API error response, status=403
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Maybe the line ServCommand failed: internal API error response, status=403 should not be displayed?

@wxiaoguang
Copy link
Contributor Author

$ git push origin main
Gitea: Mirror Repository lunny2/tea is read-only
ServCommand failed: internal API error response, status=403
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Maybe the line ServCommand failed: internal API error response, status=403 should not be displayed?

because you are using dev mode. In prod mode, there won't be the internal log

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 26, 2023
@wxiaoguang wxiaoguang force-pushed the refactor-cmd branch 2 times, most recently from bcf00a2 to fb87fad Compare March 26, 2023 06:42
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 28, 2023
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Mar 28, 2023
@lunny lunny added this to the 1.20.0 milestone Mar 28, 2023
@jolheiser
Copy link
Member

@delvh did you have any further reservations?

Copy link
Member

@delvh delvh left a comment

Choose a reason for hiding this comment

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

@jolheiser nope.


Ah, nice. With this approval, I've popped the top two priorities off my mental review queue.
Now I can focus on the lower priority PRs again…

res.Err = err.Error()
}
return &res
Err string `json:"err,omitempty"` // server-side error log message, it won't be exposed to end users
Copy link
Member

Choose a reason for hiding this comment

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

Regarding my previous comment here:
I've previously misunderstood something here, I thought this response would be returned to the user, not that it is the result of a Gitea internal request.
The user message is simply sent through fail.
That makes a lot more sense then.

@lunny lunny merged commit f453879 into go-gitea:main Mar 29, 2023
2 checks passed
@lunny lunny removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Mar 29, 2023
zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 29, 2023
* upstream/main:
  Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (go-gitea#23687)
  Add CSS rules for basic colored labels (go-gitea#23774)
  Add meilisearch support (go-gitea#23136)
  Add missing translation for `actions.runners.reset_registration_token_success` (go-gitea#23732)
  [skip ci] Updated translations via Crowdin
  Implement Issue Config (go-gitea#20956)
  Set repository link based on the url in package.json for npm packages (go-gitea#20379)
  Add API to manage issue dependencies (go-gitea#17935)
  Add creation time in tag list page (go-gitea#23693)
  Make minio package support legacy MD5 checksum (go-gitea#23768)
  Yarden Shoham has a new email address (go-gitea#23767)
  fix br display for packages curls (go-gitea#23737)
@wxiaoguang wxiaoguang deleted the refactor-cmd branch March 29, 2023 08:06
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants