summaryrefslogtreecommitdiff
path: root/workhorse
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-12-19 12:08:27 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-12-19 12:08:27 +0000
commit4a44059e43feeef9e03fe99096de5035806c9f8f (patch)
tree4af11effe8e487422fe4dd1c815bd7635324415f /workhorse
parent570f4d7ae78b453d9816f4f328bcd3941fbd0f37 (diff)
downloadgitlab-ce-4a44059e43feeef9e03fe99096de5035806c9f8f.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'workhorse')
-rw-r--r--workhorse/internal/api/api.go5
-rw-r--r--workhorse/internal/api/block.go3
-rw-r--r--workhorse/internal/api/block_test.go2
-rw-r--r--workhorse/internal/artifacts/entry.go8
-rw-r--r--workhorse/internal/builds/register.go4
-rw-r--r--workhorse/internal/channel/channel.go6
-rw-r--r--workhorse/internal/dependencyproxy/dependencyproxy.go8
-rw-r--r--workhorse/internal/dependencyproxy/dependencyproxy_test.go4
-rw-r--r--workhorse/internal/git/archive.go10
-rw-r--r--workhorse/internal/git/blob.go8
-rw-r--r--workhorse/internal/git/diff.go8
-rw-r--r--workhorse/internal/git/format-patch.go8
-rw-r--r--workhorse/internal/git/info-refs.go7
-rw-r--r--workhorse/internal/git/snapshot.go10
-rw-r--r--workhorse/internal/helper/fail/fail.go45
-rw-r--r--workhorse/internal/helper/fail/fail_test.go (renamed from workhorse/internal/helper/helpers_test.go)8
-rw-r--r--workhorse/internal/helper/helpers.go23
-rw-r--r--workhorse/internal/imageresizer/image_resizer.go4
-rw-r--r--workhorse/internal/queueing/requests.go4
-rw-r--r--workhorse/internal/sendfile/sendfile.go3
-rw-r--r--workhorse/internal/sendurl/sendurl.go10
-rw-r--r--workhorse/internal/upload/body_uploader.go10
-rw-r--r--workhorse/internal/upload/uploads.go26
-rw-r--r--workhorse/internal/upstream/handlers.go4
24 files changed, 128 insertions, 100 deletions
diff --git a/workhorse/internal/api/api.go b/workhorse/internal/api/api.go
index 1f8266bf609..1758bb5a6a8 100644
--- a/workhorse/internal/api/api.go
+++ b/workhorse/internal/api/api.go
@@ -18,6 +18,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/config"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/secret"
)
@@ -335,7 +336,7 @@ func (api *API) PreAuthorizeHandler(next HandleFunc, suffix string) http.Handler
}
if err != nil {
- helper.Fail500(w, r, err)
+ fail.Request(w, r, err)
return
}
@@ -390,7 +391,7 @@ func passResponseBack(httpResponse *http.Response, w http.ResponseWriter, r *htt
// the entire response body in memory before sending it on.
responseBody, err := bufferResponse(httpResponse.Body)
if err != nil {
- helper.Fail500(w, r, err)
+ fail.Request(w, r, err)
return
}
httpResponse.Body.Close() // Free up the Puma thread
diff --git a/workhorse/internal/api/block.go b/workhorse/internal/api/block.go
index 43763fc2b13..aac43f8cf77 100644
--- a/workhorse/internal/api/block.go
+++ b/workhorse/internal/api/block.go
@@ -5,6 +5,7 @@ import (
"net/http"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
)
// Prevent internal API responses intended for gitlab-workhorse from
@@ -48,7 +49,7 @@ func (b *blocker) WriteHeader(status int) {
b.status = 500
b.Header().Del("Content-Length")
b.hijacked = true
- helper.Fail500(b.rw, b.r, fmt.Errorf("api.blocker: forbidden content-type: %q", ResponseContentType))
+ fail.Request(b.rw, b.r, fmt.Errorf("api.blocker: forbidden content-type: %q", ResponseContentType))
return
}
diff --git a/workhorse/internal/api/block_test.go b/workhorse/internal/api/block_test.go
index 0beb401d2f5..c1ffe93dfb8 100644
--- a/workhorse/internal/api/block_test.go
+++ b/workhorse/internal/api/block_test.go
@@ -20,7 +20,7 @@ func TestBlocker(t *testing.T) {
{
desc: "blocked",
contentType: ResponseContentType,
- out: "Internal server error\n",
+ out: "Internal Server Error\n",
},
{
desc: "pass",
diff --git a/workhorse/internal/artifacts/entry.go b/workhorse/internal/artifacts/entry.go
index 3ec3a559100..e2eef174989 100644
--- a/workhorse/internal/artifacts/entry.go
+++ b/workhorse/internal/artifacts/entry.go
@@ -16,8 +16,8 @@ import (
"gitlab.com/gitlab-org/labkit/log"
"gitlab.com/gitlab-org/labkit/mask"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/command"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/zipartifacts"
)
@@ -31,7 +31,7 @@ var SendEntry = &entry{"artifacts-entry:"}
func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
var params entryParams
if err := e.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendEntry: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendEntry: unpack sendData: %v", err))
return
}
@@ -42,7 +42,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
}).Print("SendEntry: sending")
if params.Archive == "" || params.Entry == "" {
- helper.Fail500(w, r, fmt.Errorf("SendEntry: Archive or Entry is empty"))
+ fail.Request(w, r, fmt.Errorf("SendEntry: Archive or Entry is empty"))
return
}
@@ -51,7 +51,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
if os.IsNotExist(err) {
http.NotFound(w, r)
} else if err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendEntry: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendEntry: %v", err))
}
}
diff --git a/workhorse/internal/builds/register.go b/workhorse/internal/builds/register.go
index 6f47f0f99b7..0a2fe47ed7e 100644
--- a/workhorse/internal/builds/register.go
+++ b/workhorse/internal/builds/register.go
@@ -12,6 +12,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/redis"
)
@@ -119,7 +120,8 @@ func RegisterHandler(h http.Handler, watchHandler WatchKeyHandler, pollingDurati
requestBody, err := readRunnerBody(w, r)
if err != nil {
registerHandlerBodyReadErrors.Inc()
- helper.RequestEntityTooLarge(w, r, &largeBodyError{err})
+ fail.Request(w, r, &largeBodyError{err},
+ fail.WithStatus(http.StatusRequestEntityTooLarge))
return
}
diff --git a/workhorse/internal/channel/channel.go b/workhorse/internal/channel/channel.go
index deb4c32d661..f8228620a83 100644
--- a/workhorse/internal/channel/channel.go
+++ b/workhorse/internal/channel/channel.go
@@ -12,7 +12,7 @@ import (
"gitlab.com/gitlab-org/labkit/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
)
var (
@@ -26,7 +26,7 @@ var (
func Handler(myAPI *api.API) http.Handler {
return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
if err := a.Channel.Validate(); err != nil {
- helper.Fail500(w, r, err)
+ fail.Request(w, r, err)
return
}
@@ -47,7 +47,7 @@ func Handler(myAPI *api.API) http.Handler {
func ProxyChannel(w http.ResponseWriter, r *http.Request, settings *api.ChannelSettings, proxy *Proxy) {
server, err := connectToServer(settings, r)
if err != nil {
- helper.Fail500(w, r, err)
+ fail.Request(w, r, err)
log.ContextLogger(r.Context()).WithError(err).Print("Channel: connecting to server failed")
return
}
diff --git a/workhorse/internal/dependencyproxy/dependencyproxy.go b/workhorse/internal/dependencyproxy/dependencyproxy.go
index 491587b98b3..e170b001806 100644
--- a/workhorse/internal/dependencyproxy/dependencyproxy.go
+++ b/workhorse/internal/dependencyproxy/dependencyproxy.go
@@ -8,7 +8,7 @@ import (
"gitlab.com/gitlab-org/labkit/log"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
)
@@ -57,7 +57,7 @@ func (p *Injector) SetUploadHandler(uploadHandler http.Handler) {
func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
dependencyResponse, err := p.fetchUrl(r.Context(), sendData)
if err != nil {
- helper.Fail500(w, r, err)
+ fail.Request(w, r, err)
return
}
defer dependencyResponse.Body.Close()
@@ -72,7 +72,7 @@ func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData strin
teeReader := io.TeeReader(dependencyResponse.Body, w)
saveFileRequest, err := http.NewRequestWithContext(r.Context(), "POST", r.URL.String()+"/upload", teeReader)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("dependency proxy: failed to create request: %w", err))
+ fail.Request(w, r, fmt.Errorf("dependency proxy: failed to create request: %w", err))
}
saveFileRequest.Header = r.Header.Clone()
@@ -96,7 +96,7 @@ func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData strin
if nrw.status != http.StatusOK {
fields := log.Fields{"code": nrw.status}
- helper.Fail500WithFields(nrw, r, fmt.Errorf("dependency proxy: failed to upload file"), fields)
+ fail.Request(nrw, r, fmt.Errorf("dependency proxy: failed to upload file"), fail.WithFields(fields))
}
}
diff --git a/workhorse/internal/dependencyproxy/dependencyproxy_test.go b/workhorse/internal/dependencyproxy/dependencyproxy_test.go
index 6056433f3b1..d893ddc500f 100644
--- a/workhorse/internal/dependencyproxy/dependencyproxy_test.go
+++ b/workhorse/internal/dependencyproxy/dependencyproxy_test.go
@@ -153,14 +153,14 @@ func TestIncorrectSendData(t *testing.T) {
response := makeRequest(NewInjector(), "")
require.Equal(t, 500, response.Code)
- require.Equal(t, "Internal server error\n", response.Body.String())
+ require.Equal(t, "Internal Server Error\n", response.Body.String())
}
func TestIncorrectSendDataUrl(t *testing.T) {
response := makeRequest(NewInjector(), `{"Token": "token", "Url": "url"}`)
require.Equal(t, 500, response.Code)
- require.Equal(t, "Internal server error\n", response.Body.String())
+ require.Equal(t, "Internal Server Error\n", response.Body.String())
}
func TestFailedOriginServer(t *testing.T) {
diff --git a/workhorse/internal/git/archive.go b/workhorse/internal/git/archive.go
index 5c6bc2e2266..3361a8bed44 100644
--- a/workhorse/internal/git/archive.go
+++ b/workhorse/internal/git/archive.go
@@ -23,7 +23,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
)
@@ -53,14 +53,14 @@ var (
func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
var params archiveParams
if err := a.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendArchive: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendArchive: unpack sendData: %v", err))
return
}
urlPath := r.URL.Path
format, ok := parseBasename(filepath.Base(urlPath))
if !ok {
- helper.Fail500(w, r, fmt.Errorf("SendArchive: invalid format: %s", urlPath))
+ fail.Request(w, r, fmt.Errorf("SendArchive: invalid format: %s", urlPath))
return
}
@@ -93,7 +93,7 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
// to finalize the cached archive.
tempFile, err = prepareArchiveTempfile(path.Dir(params.ArchivePath), archiveFilename)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendArchive: create tempfile: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendArchive: create tempfile: %v", err))
return
}
defer tempFile.Close()
@@ -104,7 +104,7 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
archiveReader, err = handleArchiveWithGitaly(r, &params, format)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("operations.GetArchive: %v", err))
+ fail.Request(w, r, fmt.Errorf("operations.GetArchive: %v", err))
return
}
diff --git a/workhorse/internal/git/blob.go b/workhorse/internal/git/blob.go
index a6d9cd8b1da..06b0eb08228 100644
--- a/workhorse/internal/git/blob.go
+++ b/workhorse/internal/git/blob.go
@@ -8,7 +8,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
)
@@ -23,20 +23,20 @@ var SendBlob = &blob{"git-blob:"}
func (b *blob) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
var params blobParams
if err := b.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendBlob: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendBlob: unpack sendData: %v", err))
return
}
ctx, blobClient, err := gitaly.NewBlobClient(r.Context(), params.GitalyServer)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("blob.GetBlob: %v", err))
+ fail.Request(w, r, fmt.Errorf("blob.GetBlob: %v", err))
return
}
setBlobHeaders(w)
if err := blobClient.SendBlob(ctx, w, &params.GetBlobRequest); err != nil {
- helper.Fail500(w, r, fmt.Errorf("blob.GetBlob: %v", err))
+ fail.Request(w, r, fmt.Errorf("blob.GetBlob: %v", err))
return
}
}
diff --git a/workhorse/internal/git/diff.go b/workhorse/internal/git/diff.go
index 9ae95bb6680..d450d1b9034 100644
--- a/workhorse/internal/git/diff.go
+++ b/workhorse/internal/git/diff.go
@@ -8,7 +8,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
)
@@ -24,19 +24,19 @@ var SendDiff = &diff{"git-diff:"}
func (d *diff) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
var params diffParams
if err := d.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendDiff: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendDiff: unpack sendData: %v", err))
return
}
request := &gitalypb.RawDiffRequest{}
if err := gitaly.UnmarshalJSON(params.RawDiffRequest, request); err != nil {
- helper.Fail500(w, r, fmt.Errorf("diff.RawDiff: %v", err))
+ fail.Request(w, r, fmt.Errorf("diff.RawDiff: %v", err))
return
}
ctx, diffClient, err := gitaly.NewDiffClient(r.Context(), params.GitalyServer)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("diff.RawDiff: %v", err))
+ fail.Request(w, r, fmt.Errorf("diff.RawDiff: %v", err))
return
}
diff --git a/workhorse/internal/git/format-patch.go b/workhorse/internal/git/format-patch.go
index d0b2e875c95..a4306474aa5 100644
--- a/workhorse/internal/git/format-patch.go
+++ b/workhorse/internal/git/format-patch.go
@@ -8,7 +8,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
)
@@ -24,20 +24,20 @@ var SendPatch = &patch{"git-format-patch:"}
func (p *patch) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
var params patchParams
if err := p.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendPatch: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendPatch: unpack sendData: %v", err))
return
}
request := &gitalypb.RawPatchRequest{}
if err := gitaly.UnmarshalJSON(params.RawPatchRequest, request); err != nil {
- helper.Fail500(w, r, fmt.Errorf("diff.RawPatch: %v", err))
+ fail.Request(w, r, fmt.Errorf("diff.RawPatch: %v", err))
return
}
ctx, diffClient, err := gitaly.NewDiffClient(r.Context(), params.GitalyServer)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("diff.RawPatch: %v", err))
+ fail.Request(w, r, fmt.Errorf("diff.RawPatch: %v", err))
return
}
diff --git a/workhorse/internal/git/info-refs.go b/workhorse/internal/git/info-refs.go
index b7f825839f8..3e0e4dcb3e5 100644
--- a/workhorse/internal/git/info-refs.go
+++ b/workhorse/internal/git/info-refs.go
@@ -14,7 +14,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
)
func GetInfoRefsHandler(a *api.API) http.Handler {
@@ -47,9 +47,10 @@ func handleGetInfoRefs(rw http.ResponseWriter, r *http.Request, a *api.Response)
err = fmt.Errorf("handleGetInfoRefs: %v", err)
if status != nil && status.Code() == grpccodes.Unavailable {
- helper.CaptureAndFail(responseWriter, r, err, "The git server, Gitaly, is not available at this time. Please contact your administrator.", http.StatusServiceUnavailable)
+ fail.Request(responseWriter, r, err, fail.WithStatus(http.StatusServiceUnavailable),
+ fail.WithBody("The git server, Gitaly, is not available at this time. Please contact your administrator."))
} else {
- helper.Fail500(responseWriter, r, err)
+ fail.Request(responseWriter, r, err)
}
}
}
diff --git a/workhorse/internal/git/snapshot.go b/workhorse/internal/git/snapshot.go
index aadb35a5189..777ecd144a8 100644
--- a/workhorse/internal/git/snapshot.go
+++ b/workhorse/internal/git/snapshot.go
@@ -9,7 +9,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
)
@@ -31,26 +31,26 @@ func (s *snapshot) Inject(w http.ResponseWriter, r *http.Request, sendData strin
var params snapshotParams
if err := s.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendSnapshot: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendSnapshot: unpack sendData: %v", err))
return
}
request := &gitalypb.GetSnapshotRequest{}
if err := gitaly.UnmarshalJSON(params.GetSnapshotRequest, request); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendSnapshot: unmarshal GetSnapshotRequest: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendSnapshot: unmarshal GetSnapshotRequest: %v", err))
return
}
ctx, c, err := gitaly.NewRepositoryClient(r.Context(), params.GitalyServer)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendSnapshot: gitaly.NewRepositoryClient: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendSnapshot: gitaly.NewRepositoryClient: %v", err))
return
}
reader, err := c.SnapshotReader(ctx, request)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendSnapshot: client.SnapshotReader: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendSnapshot: client.SnapshotReader: %v", err))
return
}
diff --git a/workhorse/internal/helper/fail/fail.go b/workhorse/internal/helper/fail/fail.go
new file mode 100644
index 00000000000..32c2940a0cc
--- /dev/null
+++ b/workhorse/internal/helper/fail/fail.go
@@ -0,0 +1,45 @@
+package fail
+
+import (
+ "net/http"
+
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
+)
+
+type failure struct {
+ status int
+ body string
+ fields log.Fields
+}
+
+type Option func(*failure)
+
+// WithStatus sets the HTTP status and body text of the failure response.
+func WithStatus(status int) Option {
+ return func(f *failure) {
+ f.status = status
+ f.body = http.StatusText(status)
+ }
+}
+
+// WithBody sets the body text of the failure response. Note that
+// subsequent applications of WithStatus will override the response body.
+func WithBody(body string) Option { return func(f *failure) { f.body = body } }
+
+// WithFields adds log fields to the failure log message.
+func WithFields(fields log.Fields) Option { return func(f *failure) { f.fields = fields } }
+
+// Request combines error handling actions for a failed HTTP request. By
+// default it writes a generic HTTP 500 response to w. The status code
+// and response body can be modified by passing options. The value of
+// err, if non nil, is logged and reported to Sentry.
+func Request(w http.ResponseWriter, r *http.Request, err error, options ...Option) {
+ f := &failure{}
+ WithStatus(http.StatusInternalServerError)(f)
+ for _, opt := range options {
+ opt(f)
+ }
+
+ http.Error(w, f.body, f.status)
+ log.WithRequest(r).WithFields(f.fields).WithError(err).Error()
+}
diff --git a/workhorse/internal/helper/helpers_test.go b/workhorse/internal/helper/fail/fail_test.go
index f303b22d424..ceb037d2da7 100644
--- a/workhorse/internal/helper/helpers_test.go
+++ b/workhorse/internal/helper/fail/fail_test.go
@@ -1,4 +1,4 @@
-package helper
+package fail
import (
"bytes"
@@ -9,13 +9,13 @@ import (
"github.com/stretchr/testify/require"
)
-func TestFail500WorksWithNils(t *testing.T) {
+func TestRequestWorksWithNils(t *testing.T) {
body := bytes.NewBuffer(nil)
w := httptest.NewRecorder()
w.Body = body
- Fail500(w, nil, nil)
+ Request(w, nil, nil)
require.Equal(t, http.StatusInternalServerError, w.Code)
- require.Equal(t, "Internal server error\n", body.String())
+ require.Equal(t, "Internal Server Error\n", body.String())
}
diff --git a/workhorse/internal/helper/helpers.go b/workhorse/internal/helper/helpers.go
index 2234cd8a9f3..a4a91901ea9 100644
--- a/workhorse/internal/helper/helpers.go
+++ b/workhorse/internal/helper/helpers.go
@@ -3,35 +3,12 @@ package helper
import (
"errors"
"mime"
- "net/http"
"net/url"
"os"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
)
-func CaptureAndFail(w http.ResponseWriter, r *http.Request, err error, msg string, code int) {
- http.Error(w, msg, code)
- printError(r, err, nil)
-}
-
-func Fail500(w http.ResponseWriter, r *http.Request, err error) {
- CaptureAndFail(w, r, err, "Internal server error", http.StatusInternalServerError)
-}
-
-func Fail500WithFields(w http.ResponseWriter, r *http.Request, err error, fields log.Fields) {
- http.Error(w, "Internal server error", http.StatusInternalServerError)
- printError(r, err, fields)
-}
-
-func RequestEntityTooLarge(w http.ResponseWriter, r *http.Request, err error) {
- CaptureAndFail(w, r, err, "Request Entity Too Large", http.StatusRequestEntityTooLarge)
-}
-
-func printError(r *http.Request, err error, fields log.Fields) {
- log.WithRequest(r).WithFields(fields).WithError(err).Error()
-}
-
func OpenFile(path string) (file *os.File, fi os.FileInfo, err error) {
file, err = os.Open(path)
if err != nil {
diff --git a/workhorse/internal/imageresizer/image_resizer.go b/workhorse/internal/imageresizer/image_resizer.go
index 4a7e52d92d9..72f345239a6 100644
--- a/workhorse/internal/imageresizer/image_resizer.go
+++ b/workhorse/internal/imageresizer/image_resizer.go
@@ -20,8 +20,8 @@ import (
"gitlab.com/gitlab-org/labkit/tracing"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/config"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/command"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
@@ -420,7 +420,7 @@ func handleOutcome(w http.ResponseWriter, req *http.Request, startTime time.Time
switch outcome.status {
case statusRequestFailure:
if outcome.bytesWritten <= 0 {
- helper.Fail500WithFields(w, req, outcome.err, fields)
+ fail.Request(w, req, outcome.err, fail.WithFields(fields))
} else {
log.WithError(outcome.err).Error(outcome.status)
}
diff --git a/workhorse/internal/queueing/requests.go b/workhorse/internal/queueing/requests.go
index 34d4c985f53..c3df614de41 100644
--- a/workhorse/internal/queueing/requests.go
+++ b/workhorse/internal/queueing/requests.go
@@ -6,7 +6,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
)
const (
@@ -46,7 +46,7 @@ func QueueRequests(name string, h http.Handler, limit, queueLimit uint, queueTim
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
default:
- helper.Fail500(w, r, err)
+ fail.Request(w, r, err)
}
})
diff --git a/workhorse/internal/sendfile/sendfile.go b/workhorse/internal/sendfile/sendfile.go
index e9d6d763b88..70d93f1109c 100644
--- a/workhorse/internal/sendfile/sendfile.go
+++ b/workhorse/internal/sendfile/sendfile.go
@@ -20,6 +20,7 @@ import (
"gitlab.com/gitlab-org/gitlab/workhorse/internal/headers"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/nginx"
)
@@ -130,7 +131,7 @@ func sendFileFromDisk(w http.ResponseWriter, r *http.Request, file string) {
if contentTypeHeaderPresent {
data, err := io.ReadAll(io.LimitReader(content, headers.MaxDetectSize))
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("content type detection: %v", err))
+ fail.Request(w, r, fmt.Errorf("content type detection: %v", err))
return
}
diff --git a/workhorse/internal/sendurl/sendurl.go b/workhorse/internal/sendurl/sendurl.go
index 8e679c6b475..e689fc84a0f 100644
--- a/workhorse/internal/sendurl/sendurl.go
+++ b/workhorse/internal/sendurl/sendurl.go
@@ -10,7 +10,7 @@ import (
"gitlab.com/gitlab-org/labkit/mask"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/transport"
@@ -83,7 +83,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
defer sendURLOpenRequests.Dec()
if err := e.Unpack(&params, sendData); err != nil {
- helper.Fail500(w, r, fmt.Errorf("SendURL: unpack sendData: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendURL: unpack sendData: %v", err))
return
}
@@ -94,7 +94,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
if params.URL == "" {
sendURLRequestsInvalidData.Inc()
- helper.Fail500(w, r, fmt.Errorf("SendURL: URL is empty"))
+ fail.Request(w, r, fmt.Errorf("SendURL: URL is empty"))
return
}
@@ -102,7 +102,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
newReq, err := http.NewRequest("GET", params.URL, nil)
if err != nil {
sendURLRequestsInvalidData.Inc()
- helper.Fail500(w, r, fmt.Errorf("SendURL: NewRequest: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendURL: NewRequest: %v", err))
return
}
newReq = newReq.WithContext(r.Context())
@@ -120,7 +120,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
}
if err != nil {
sendURLRequestsRequestFailed.Inc()
- helper.Fail500(w, r, fmt.Errorf("SendURL: Do request: %v", err))
+ fail.Request(w, r, fmt.Errorf("SendURL: Do request: %v", err))
return
}
diff --git a/workhorse/internal/upload/body_uploader.go b/workhorse/internal/upload/body_uploader.go
index 4b5152c283c..4733415c2c1 100644
--- a/workhorse/internal/upload/body_uploader.go
+++ b/workhorse/internal/upload/body_uploader.go
@@ -8,7 +8,7 @@ import (
"strings"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upload/destination"
)
@@ -19,20 +19,20 @@ func RequestBody(rails PreAuthorizer, h http.Handler, p Preparer) http.Handler {
return rails.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
opts, err := p.Prepare(a)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("RequestBody: preparation failed: %v", err))
+ fail.Request(w, r, fmt.Errorf("RequestBody: preparation failed: %v", err))
return
}
fh, err := destination.Upload(r.Context(), r.Body, r.ContentLength, "upload", opts)
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("RequestBody: upload failed: %v", err))
+ fail.Request(w, r, fmt.Errorf("RequestBody: upload failed: %v", err))
return
}
data := url.Values{}
fields, err := fh.GitLabFinalizeFields("file")
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("RequestBody: finalize fields failed: %v", err))
+ fail.Request(w, r, fmt.Errorf("RequestBody: finalize fields failed: %v", err))
return
}
@@ -49,7 +49,7 @@ func RequestBody(rails PreAuthorizer, h http.Handler, p Preparer) http.Handler {
sft := SavedFileTracker{Request: r}
sft.Track("file", fh.LocalPath)
if err := sft.Finalize(r.Context()); err != nil {
- helper.Fail500(w, r, fmt.Errorf("RequestBody: finalize failed: %v", err))
+ fail.Request(w, r, fmt.Errorf("RequestBody: finalize failed: %v", err))
return
}
diff --git a/workhorse/internal/upload/uploads.go b/workhorse/internal/upload/uploads.go
index 32e51fea9e5..a3072aa5d00 100644
--- a/workhorse/internal/upload/uploads.go
+++ b/workhorse/internal/upload/uploads.go
@@ -12,7 +12,7 @@ import (
"github.com/golang-jwt/jwt/v4"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upload/destination"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upload/exif"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/zipartifacts"
@@ -51,23 +51,23 @@ func interceptMultipartFiles(w http.ResponseWriter, r *http.Request, h http.Hand
err := rewriteFormFilesFromMultipart(r, writer, filter, fa, p)
if err != nil {
switch err {
- case ErrInjectedClientParam, http.ErrMissingBoundary:
- helper.CaptureAndFail(w, r, err, "Bad Request", http.StatusBadRequest)
- case ErrTooManyFilesUploaded:
- helper.CaptureAndFail(w, r, err, err.Error(), http.StatusBadRequest)
case http.ErrNotMultipart:
h.ServeHTTP(w, r)
- case destination.ErrEntityTooLarge:
- helper.RequestEntityTooLarge(w, r, err)
- case zipartifacts.ErrBadMetadata:
- helper.RequestEntityTooLarge(w, r, err)
+ case ErrInjectedClientParam, http.ErrMissingBoundary:
+ fail.Request(w, r, err, fail.WithStatus(http.StatusBadRequest))
+ case ErrTooManyFilesUploaded:
+ fail.Request(w, r, err, fail.WithStatus(http.StatusBadRequest), fail.WithBody(err.Error()))
+ case destination.ErrEntityTooLarge, zipartifacts.ErrBadMetadata:
+ fail.Request(w, r, err, fail.WithStatus(http.StatusRequestEntityTooLarge))
case exif.ErrRemovingExif:
- helper.CaptureAndFail(w, r, err, "Failed to process image", http.StatusUnprocessableEntity)
+ fail.Request(w, r, err, fail.WithStatus(http.StatusUnprocessableEntity),
+ fail.WithBody("Failed to process image"))
default:
if errors.Is(err, context.DeadlineExceeded) {
- helper.CaptureAndFail(w, r, err, "deadline exceeded", http.StatusGatewayTimeout)
+ fail.Request(w, r, err, fail.WithStatus(http.StatusGatewayTimeout),
+ fail.WithBody("deadline exceeded"))
} else {
- helper.Fail500(w, r, fmt.Errorf("handleFileUploads: extract files from multipart: %v", err))
+ fail.Request(w, r, fmt.Errorf("handleFileUploads: extract files from multipart: %v", err))
}
}
return
@@ -82,7 +82,7 @@ func interceptMultipartFiles(w http.ResponseWriter, r *http.Request, h http.Hand
r.Header.Set("Content-Type", writer.FormDataContentType())
if err := filter.Finalize(r.Context()); err != nil {
- helper.Fail500(w, r, fmt.Errorf("handleFileUploads: Finalize: %v", err))
+ fail.Request(w, r, fmt.Errorf("handleFileUploads: Finalize: %v", err))
return
}
diff --git a/workhorse/internal/upstream/handlers.go b/workhorse/internal/upstream/handlers.go
index 5974170e172..85fee0bf7e2 100644
--- a/workhorse/internal/upstream/handlers.go
+++ b/workhorse/internal/upstream/handlers.go
@@ -6,7 +6,7 @@ import (
"io"
"net/http"
- "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
+ "gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
)
func contentEncodingHandler(h http.Handler) http.Handler {
@@ -26,7 +26,7 @@ func contentEncodingHandler(h http.Handler) http.Handler {
}
if err != nil {
- helper.Fail500(w, r, fmt.Errorf("contentEncodingHandler: %v", err))
+ fail.Request(w, r, fmt.Errorf("contentEncodingHandler: %v", err))
return
}
defer body.Close()