Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'workhorse/internal/helper/fail/fail.go')
-rw-r--r--workhorse/internal/helper/fail/fail.go45
1 files changed, 45 insertions, 0 deletions
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()
+}