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

fail.go « fail « helper « internal « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32c2940a0cccf52cd0bf15b52c862a1dafe6fb78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()
}