Improve HTTP Client error messages for Geo requests
Context
After !739 (merged), when Geo proxy a request from a secondary site, in gitlab-shell
all httpClient
requests that receive status codes in the range of >= 400
output an standard message: Remote repository is unavailable
for failed Git operations.
This standard error message shallows dedicated errors defined in gitlab-rails
:
In lib/gitlab/git_access.rb
:
ERROR_MESSAGES = {
upload: 'You are not allowed to upload code for this project.',
download: 'You are not allowed to download code from this project.',
auth_upload: 'You are not allowed to upload code.',
auth_download: 'You are not allowed to download code.',
deploy_key_upload: 'This deploy key does not have write access to this project.',
no_repo: 'A repository for this project does not exist yet.',
project_not_found: "The project you were looking for could not be found or you don't have permission to view it.",
command_not_allowed: "The command you're trying to execute is not allowed.",
upload_pack_disabled_over_http: 'Pulling over HTTP is not allowed.',
receive_pack_disabled_over_http: 'Pushing over HTTP is not allowed.',
read_only: 'The repository is temporarily read-only. Please try again later.',
cannot_push_to_read_only: "You can't push code to a read-only GitLab instance.",
push_code: 'You are not allowed to push code to this project.'
}.freeze
In app/controllers/repositories/git_http_controller.rb
:
rescue_from Gitlab::GitAccess::ForbiddenError, with: :render_403_with_exception
rescue_from JWT::DecodeError, with: :render_403_with_exception
rescue_from Gitlab::GitAccess::NotFoundError, with: :render_404_with_exception
rescue_from Gitlab::GitAccessProject::CreationError, with: :render_422_with_exception
All the above are in the range of >=400
and rendered as exception messages:
def render_403_with_exception(exception)
render plain: exception.message, status: :forbidden
end
def render_404_with_exception(exception)
render plain: exception.message, status: :not_found
end
def render_422_with_exception(exception)
render plain: exception.message, status: :unprocessable_entity
end
def render_503_with_exception(exception, message: nil)
render plain: message || exception.message, status: :service_unavailable
end
Screenshots
gitlab-shell logs | Console output |
---|---|
gitlab-shell logs | Console output |
---|---|
Possible fix
From my limited Golang knowledge, in internal/gitlabnet/git/client.go
we could:
func (c *Client) do(request *http.Request) (*http.Response, error) {
for k, v := range c.Headers {
request.Header.Add(k, v)
}
response, err := httpClient.Do(request)
if err != nil || response.StatusCode >= 400 {
bodyBytes, _ := io.ReadAll(response.Body)
return nil, &client.ApiError{string(bodyBytes)}
}
return response, nil
}
I tested the above code and I could see the expected messages. I attached the result in the screenshots.