Commit 3aaadb1b authored by Jacob Vosmaer's avatar Jacob Vosmaer

Make fancy Sentry/Raven errors

parent 834ab5e2
......@@ -11,7 +11,6 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/badgateway"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"github.com/dgrijalva/jwt-go"
)
......@@ -148,13 +147,13 @@ func (api *API) PreAuthorizeHandler(h HandleFunc, suffix string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authReq, err := api.newRequest(r, nil, suffix)
if err != nil {
helper.Fail500(w, requesterror.New("preAuthorizeHandler", r, "newUpstreamRequest: %v", err))
helper.Fail500(w, r, fmt.Errorf("preAuthorizeHandler newUpstreamRequest: %v", err))
return
}
authResponse, err := api.Client.Do(authReq)
if err != nil {
helper.Fail500(w, requesterror.New("preAuthorizeHandler", r, "do request: %v", err))
helper.Fail500(w, r, fmt.Errorf("preAuthorizeHandler: do request: %v", err))
return
}
defer authResponse.Body.Close()
......@@ -174,7 +173,7 @@ func (api *API) PreAuthorizeHandler(h HandleFunc, suffix string) http.Handler {
}
if contentType := authResponse.Header.Get("Content-Type"); contentType != ResponseContentType {
helper.Fail500(w, requesterror.New("preAuthorizeHandler", r, "API responded with wrong content type: %v", contentType))
helper.Fail500(w, r, fmt.Errorf("preAuthorizeHandler: API responded with wrong content type: %v", contentType))
return
}
......@@ -183,7 +182,7 @@ func (api *API) PreAuthorizeHandler(h HandleFunc, suffix string) http.Handler {
// request metadata. We must extract this information from the auth
// response body.
if err := json.NewDecoder(authResponse.Body).Decode(a); err != nil {
helper.Fail500(w, requesterror.New("preAuthorizeHandler", r, "decode authorization response: %v", err))
helper.Fail500(w, r, fmt.Errorf("preAuthorizeHandler: decode authorization response: %v", err))
return
}
// Don't hog a TCP connection in CLOSE_WAIT, we can already close it now
......
package api
import (
"fmt"
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
)
// Prevent internal API responses intended for gitlab-workhorse from
......@@ -48,7 +48,7 @@ func (b *blocker) WriteHeader(status int) {
b.status = 500
b.Header().Del("Content-Length")
b.hijacked = true
helper.Fail500(b.rw, requesterror.New("api.blocker", b.r, "forbidden content-type: %q", ResponseContentType))
helper.Fail500(b.rw, b.r, fmt.Errorf("api.blocker: forbidden content-type: %q", ResponseContentType))
return
}
......
......@@ -11,7 +11,6 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/upload"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/zipartifacts"
)
......@@ -75,7 +74,7 @@ func (a *artifactsUploadProcessor) Cleanup() {
func UploadArtifacts(myAPI *api.API, h http.Handler) http.Handler {
return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
if a.TempPath == "" {
helper.Fail500(w, requesterror.New("UploadArtifacts", r, "TempPath is empty"))
helper.Fail500(w, r, fmt.Errorf("UploadArtifacts: TempPath is empty"))
return
}
......
......@@ -14,7 +14,6 @@ import (
"syscall"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/senddata"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/zipartifacts"
)
......@@ -28,14 +27,14 @@ 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, requesterror.New("SendEntry", r, "unpack sendData: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendEntry: unpack sendData: %v", err))
return
}
log.Printf("SendEntry: sending %q from %q for %q", params.Entry, params.Archive, r.URL.Path)
if params.Archive == "" || params.Entry == "" {
helper.Fail500(w, requesterror.New("SendEntry", r, "Archive or Entry is empty"))
helper.Fail500(w, r, fmt.Errorf("SendEntry: Archive or Entry is empty"))
return
}
......@@ -44,7 +43,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, requesterror.New("SendEntry", r, "%v", err))
helper.Fail500(w, r, fmt.Errorf("SendEntry: %v", err))
}
}
......
......@@ -10,7 +10,6 @@ import (
"time"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
)
// Values from http.DefaultTransport
......@@ -82,9 +81,10 @@ func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error
// instead of 500s we catch the RoundTrip error here and inject a
// 502 response.
if err != nil {
helper.LogError(&Error{
requesterror.New("badgateway", r, "failed after %.3fs: %v", time.Since(start).Seconds(), err),
})
helper.LogError(
r,
&Error{fmt.Errorf("badgateway: failed after %.3fs: %v", time.Since(start).Seconds(), err)},
)
res = &http.Response{
StatusCode: http.StatusBadGateway,
......
......@@ -18,7 +18,6 @@ import (
"time"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/senddata"
)
......@@ -35,7 +34,7 @@ var SendArchive = &archive{"git-archive:"}
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, requesterror.New("SendArchive", r, "unpack sendData: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendArchive: unpack sendData: %v", err))
return
}
......@@ -43,7 +42,7 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
urlPath := r.URL.Path
format, ok := parseBasename(filepath.Base(urlPath))
if !ok {
helper.Fail500(w, requesterror.New("SendArchive", r, "invalid format: %s", urlPath))
helper.Fail500(w, r, fmt.Errorf("SendArchive: invalid format: %s", urlPath))
return
}
......@@ -66,7 +65,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, requesterror.New("SendArchive", r, "create tempfile: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendArchive: create tempfile: %v", err))
return
}
defer tempFile.Close()
......@@ -77,12 +76,12 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
archiveCmd := gitCommand("", "git", "--git-dir="+params.RepoPath, "archive", "--format="+archiveFormat, "--prefix="+params.ArchivePrefix+"/", params.CommitId)
archiveStdout, err := archiveCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("SendArchive", r, "archive stdout: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendArchive: archive stdout: %v", err))
return
}
defer archiveStdout.Close()
if err := archiveCmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("SendArchive", r, "start %v: %v", archiveCmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("SendArchive: start %v: %v", archiveCmd.Args, err))
return
}
defer helper.CleanUpProcessGroup(archiveCmd) // Ensure brute force subprocess clean-up
......@@ -96,13 +95,13 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
stdout, err = compressCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("SendArchive", r, "compress stdout: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendArchive: compress stdout: %v", err))
return
}
defer stdout.Close()
if err := compressCmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("SendArchive", r, "start %v: %v", compressCmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("SendArchive: start %v: %v", compressCmd.Args, err))
return
}
defer helper.CleanUpProcessGroup(compressCmd)
......@@ -117,24 +116,22 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
setArchiveHeaders(w, format, archiveFilename)
w.WriteHeader(200) // Don't bother with HTTP 500 from this point on, just return
if _, err := io.Copy(w, archiveReader); err != nil {
helper.LogError(&copyError{
requesterror.New("SendArchive", r, "copy 'git archive' output: %v", err),
})
helper.LogError(r, &copyError{fmt.Errorf("SendArchive: copy 'git archive' output: %v", err)})
return
}
if err := archiveCmd.Wait(); err != nil {
helper.LogError(requesterror.New("SendArchive", r, "archiveCmd: %v", err))
helper.LogError(r, fmt.Errorf("SendArchive: archiveCmd: %v", err))
return
}
if compressCmd != nil {
if err := compressCmd.Wait(); err != nil {
helper.LogError(requesterror.New("SendArchive", r, "compressCmd: %v", err))
helper.LogError(r, fmt.Errorf("SendArchive: compressCmd: %v", err))
return
}
}
if err := finalizeCachedArchive(tempFile, params.ArchivePath); err != nil {
helper.LogError(requesterror.New("SendArchive", r, "finalize cached archive: %v", err))
helper.LogError(r, fmt.Errorf("SendArchive: finalize cached archive: %v", err))
return
}
}
......
package git
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/senddata"
)
......@@ -19,7 +19,7 @@ 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, requesterror.New("SendBlob", r, "unpack sendData: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendBlob: unpack sendData: %v", err))
return
}
......@@ -27,29 +27,29 @@ func (b *blob) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
sizeOutput, err := gitCommand("", "git", "--git-dir="+params.RepoPath, "cat-file", "-s", params.BlobId).Output()
if err != nil {
helper.Fail500(w, requesterror.New("SendBlob", r, "get blob size: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendBlob: get blob size: %v", err))
return
}
gitShowCmd := gitCommand("", "git", "--git-dir="+params.RepoPath, "cat-file", "blob", params.BlobId)
stdout, err := gitShowCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("SendBlob", r, "git cat-file stdout: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendBlob: git cat-file stdout: %v", err))
return
}
if err := gitShowCmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("SendBlob", r, "start %v: %v", gitShowCmd, err))
helper.Fail500(w, r, fmt.Errorf("SendBlob: start %v: %v", gitShowCmd, err))
return
}
defer helper.CleanUpProcessGroup(gitShowCmd)
w.Header().Set("Content-Length", strings.TrimSpace(string(sizeOutput)))
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(&copyError{requesterror.New("SendBlob", r, "copy git cat-file stdout: %v", err)})
helper.LogError(r, &copyError{fmt.Errorf("SendBlob: copy git cat-file stdout: %v", err)})
return
}
if err := gitShowCmd.Wait(); err != nil {
helper.LogError(requesterror.New("SendBlob", r, "wait for git cat-file: %v", err))
helper.LogError(r, fmt.Errorf("SendBlob: wait for git cat-file: %v", err))
return
}
}
package git
import (
"fmt"
"io"
"log"
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/senddata"
)
......@@ -22,7 +22,7 @@ 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, requesterror.New("SendDiff", r, "unpack sendData: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendDiff: unpack sendData: %v", err))
return
}
......@@ -31,25 +31,26 @@ func (d *diff) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
gitDiffCmd := gitCommand("", "git", "--git-dir="+params.RepoPath, "diff", params.ShaFrom, params.ShaTo)
stdout, err := gitDiffCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("SendDiff", r, "create stdout pipe: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendDiff: create stdout pipe: %v", err))
return
}
if err := gitDiffCmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("SendDiff", r, "start %v: %v", gitDiffCmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("SendDiff: start %v: %v", gitDiffCmd.Args, err))
return
}
defer helper.CleanUpProcessGroup(gitDiffCmd)
w.Header().Del("Content-Length")
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(&copyError{
requesterror.New("SendDiff", r, "copy %v stdout: %v", gitDiffCmd.Args, err),
})
helper.LogError(
r,
&copyError{fmt.Errorf("SendDiff: copy %v stdout: %v", gitDiffCmd.Args, err)},
)
return
}
if err := gitDiffCmd.Wait(); err != nil {
helper.LogError(requesterror.New("SendDiff", r, "wait for %v: %v", gitDiffCmd.Args, err))
helper.LogError(r, fmt.Errorf("SendDiff: wait for %v: %v", gitDiffCmd.Args, err))
return
}
}
......@@ -7,7 +7,6 @@ import (
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/senddata"
)
......@@ -23,7 +22,7 @@ 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, requesterror.New("SendPatch", r, "unpack sendData: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendPatch: unpack sendData: %v", err))
return
}
......@@ -34,23 +33,23 @@ func (p *patch) Inject(w http.ResponseWriter, r *http.Request, sendData string)
stdout, err := gitPatchCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("SendPatch", r, "create stdout pipe: %v", err))
helper.Fail500(w, r, fmt.Errorf("SendPatch: create stdout pipe: %v", err))
return
}
if err := gitPatchCmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("SendPatch", r, "start %v: %v", gitPatchCmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("SendPatch: start %v: %v", gitPatchCmd.Args, err))
return
}
defer helper.CleanUpProcessGroup(gitPatchCmd)
w.Header().Del("Content-Length")
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(&copyError{requesterror.New("SendPatch", r, "copy %v stdout: %v", gitPatchCmd.Args, err)})
helper.LogError(r, &copyError{fmt.Errorf("SendPatch: copy %v stdout: %v", gitPatchCmd.Args, err)})
return
}
if err := gitPatchCmd.Wait(); err != nil {
helper.LogError(requesterror.New("SendPatch", r, "wait for %v: %v", gitPatchCmd.Args, err))
helper.LogError(r, fmt.Errorf("SendPatch: wait for %v: %v", gitPatchCmd.Args, err))
return
}
}
......@@ -16,7 +16,6 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
)
func GetInfoRefs(a *api.API) http.Handler {
......@@ -40,7 +39,7 @@ func looksLikeRepo(p string) bool {
func repoPreAuthorizeHandler(myAPI *api.API, handleFunc api.HandleFunc) http.Handler {
return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
if a.RepoPath == "" {
helper.Fail500(w, requesterror.New("repoPreAuthorizeHandler", r, "RepoPath empty"))
helper.Fail500(w, r, fmt.Errorf("repoPreAuthorizeHandler: RepoPath empty"))
return
}
......@@ -65,12 +64,12 @@ func handleGetInfoRefs(w http.ResponseWriter, r *http.Request, a *api.Response)
cmd := gitCommand(a.GL_ID, "git", subCommand(rpc), "--stateless-rpc", "--advertise-refs", a.RepoPath)
stdout, err := cmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("handleGetInfoRefs", r, "stdout: %v", err))
helper.Fail500(w, r, fmt.Errorf("handleGetInfoRefs: stdout: %v", err))
return
}
defer stdout.Close()
if err := cmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("handleGetInfoRefs", r, "start %v: %v", cmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("handleGetInfoRefs: start %v: %v", cmd.Args, err))
return
}
defer helper.CleanUpProcessGroup(cmd) // Ensure brute force subprocess clean-up
......@@ -80,21 +79,22 @@ func handleGetInfoRefs(w http.ResponseWriter, r *http.Request, a *api.Response)
w.Header().Add("Cache-Control", "no-cache")
w.WriteHeader(200) // Don't bother with HTTP 500 from this point on, just return
if err := pktLine(w, fmt.Sprintf("# service=%s\n", rpc)); err != nil {
helper.LogError(requesterror.New("handleGetInfoRefs", r, "pktLine: %v", err))
helper.LogError(r, fmt.Errorf("handleGetInfoRefs: pktLine: %v", err))
return
}
if err := pktFlush(w); err != nil {
helper.LogError(requesterror.New("handleGetInfoRefs", r, "pktFlush: %v", err))
helper.LogError(r, fmt.Errorf("handleGetInfoRefs: pktFlush: %v", err))
return
}
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(&copyError{
requesterror.New("handleGetInfoRefs", r, "copy output of %v: %v", cmd.Args, err),
})
helper.LogError(
r,
&copyError{fmt.Errorf("handleGetInfoRefs: copy output of %v: %v", cmd.Args, err)},
)
return
}
if err := cmd.Wait(); err != nil {
helper.LogError(requesterror.New("handleGetInfoRefs", r, "wait for %v: %v", cmd.Args, err))
helper.LogError(r, fmt.Errorf("handleGetInfoRefs: wait for %v: %v", cmd.Args, err))
return
}
}
......@@ -106,7 +106,7 @@ func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
action := filepath.Base(r.URL.Path)
if !(action == "git-upload-pack" || action == "git-receive-pack") {
// The 'dumb' Git HTTP protocol is not supported
helper.Fail500(w, requesterror.New("handlePostRPC", r, "unsupported action: %s", r.URL.Path))
helper.Fail500(w, r, fmt.Errorf("handlePostRPC: unsupported action: %s", r.URL.Path))
return
}
......@@ -114,25 +114,25 @@ func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
cmd := gitCommand(a.GL_ID, "git", subCommand(action), "--stateless-rpc", a.RepoPath)
stdout, err := cmd.StdoutPipe()
if err != nil {
helper.Fail500(w, requesterror.New("handlePostRPC", r, "stdout: %v", err))
helper.Fail500(w, r, fmt.Errorf("handlePostRPC: stdout: %v", err))
return
}
defer stdout.Close()
stdin, err := cmd.StdinPipe()
if err != nil {
helper.Fail500(w, requesterror.New("handlePostRPC", r, "stdin: %v", err))
helper.Fail500(w, r, fmt.Errorf("handlePostRPC: stdin: %v", err))
return
}
defer stdin.Close()
if err := cmd.Start(); err != nil {
helper.Fail500(w, requesterror.New("handlePostRPC", r, "start %v: %v", cmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("handlePostRPC: start %v: %v", cmd.Args, err))
return
}
defer helper.CleanUpProcessGroup(cmd) // Ensure brute force subprocess clean-up
// Write the client request body to Git's standard input
if _, err := io.Copy(stdin, r.Body); err != nil {
helper.Fail500(w, requesterror.New("handlePostRPC", r, "write to %v: %v", cmd.Args, err))
helper.Fail500(w, r, fmt.Errorf("handlePostRPC: write to %v: %v", cmd.Args, err))
return
}
// Signal to the Git subprocess that no more data is coming
......@@ -149,13 +149,14 @@ func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
// This io.Copy may take a long time, both for Git push and pull.
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(&copyError{
requesterror.New("handlePostRPC", r, "copy output of %v: %v", cmd.Args, err),
})
helper.LogError(
r,
&copyError{fmt.Errorf("handlePostRPC: copy output of %v: %v", cmd.Args, err)},
)
return
}
if err := cmd.Wait(); err != nil {
helper.LogError(requesterror.New("handlePostRPC", r, "wait for %v: %v", cmd.Args, err))
helper.LogError(r, fmt.Errorf("handlePostRPC: wait for %v: %v", cmd.Args, err))
return
}
}
......
......@@ -8,18 +8,25 @@ import (
"os"
"os/exec"
"syscall"
"github.com/getsentry/raven-go"
)
func Fail500(w http.ResponseWriter, err error) {
func Fail500(w http.ResponseWriter, r *http.Request, err error) {
http.Error(w, "Internal server error", 500)
LogError(err)
captureRavenError(r, err)
printError(r, err)
}
func LogError(r *http.Request, err error) {
captureRavenError(r, err)
printError(r, err)
}
func LogError(err error) {
raven.CaptureError(err, nil)
log.Printf("error: %v", err)
func printError(r *http.Request, err error) {
if r != nil {
log.Printf("error: %s %q: %v", r.Method, r.RequestURI, err)
} else {
log.Printf("error: %v", err)
}
}
func SetNoCacheHeaders(header http.Header) {
......
package helper
import (
"net/http"
"reflect"
"strings"
"github.com/getsentry/raven-go"
)
func captureRavenError(r *http.Request, err error) {
client := raven.DefaultClient
interfaces := []raven.Interface{}
if r != nil {
CleanHeadersForRaven(r)
interfaces = append(interfaces, raven.NewHttp(r))
}
exception := &raven.Exception{
Stacktrace: raven.NewStacktrace(2, 3, nil),
Value: err.Error(),
Type: reflect.TypeOf(err).String(),
}
interfaces = append(interfaces, exception)
packet := raven.NewPacket(err.Error(), interfaces...)
client.Capture(packet, nil)
}
func CleanHeadersForRaven(r *http.Request) {
if auth := r.Header.Get("Authorization"); auth != "" {
if authSplit := strings.Split(auth, " "); authSplit != nil {
r.Header.Set("Authorization", authSplit[0]+" [redacted]")
}
}
}
......@@ -8,6 +8,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
......@@ -16,7 +17,6 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
)
func PutStore(a *api.API, h http.Handler) http.Handler {
......@@ -27,17 +27,17 @@ func lfsAuthorizeHandler(myAPI *api.API, handleFunc api.HandleFunc) http.Handler
return myAPI.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
if a.StoreLFSPath == "" {
helper.Fail500(w, requesterror.New("lfsAuthorizeHandler", r, "StoreLFSPath empty"))
helper.Fail500(w, r, fmt.Errorf("lfsAuthorizeHandler: StoreLFSPath empty"))
return
}
if a.LfsOid == "" {
helper.Fail500(w, requesterror.New("lfsAuthorizeHandler", r, "LfsOid empty"))
helper.Fail500(w, r, fmt.Errorf("lfsAuthorizeHandler: LfsOid empty"))
return
}
if err := os.MkdirAll(a.StoreLFSPath, 0700); err != nil {
helper.Fail500(w, requesterror.New("lfsAuthorizeHandler", r, "mkdir StoreLFSPath: %v", err))
helper.Fail500(w, r, fmt.Errorf("lfsAuthorizeHandler: mkdir StoreLFSPath: %v", err))
return
}
......@@ -49,7 +49,7 @@ func handleStoreLfsObject(h http.Handler) api.HandleFunc {
return func(w http.ResponseWriter, r *http.Request, a *api.Response) {
file, err := ioutil.TempFile(a.StoreLFSPath, a.LfsOid)
if err != nil {
helper.Fail500(w, requesterror.New("handleStoreLfsObject", r, "create tempfile: %v", err))
helper.Fail500(w, r, fmt.Errorf("handleStoreLfsObject: create tempfile: %v", err))
return
}
defer os.Remove(file.Name())
......@@ -60,19 +60,19 @@ func handleStoreLfsObject(h http.Handler) api.HandleFunc {
written, err := io.Copy(hw, r.Body)
if err != nil {
helper.Fail500(w, requesterror.New("handleStoreLfsObject", r, "copy body to tempfile: %v", err))
helper.Fail500(w, r, fmt.Errorf("handleStoreLfsObject: copy body to tempfile: %v", err))
return
}
file.Close()
if written != a.LfsSize {
helper.Fail500(w, requesterror.New("handleStoreLfsObject", r, "expected size %d, wrote %d", a.LfsSize, written))
helper.Fail500(w, r, fmt.Errorf("handleStoreLfsObject: expected size %d, wrote %d", a.LfsSize, written))
return
}
shaStr := hex.EncodeToString(hash.Sum(nil))
if shaStr != a.LfsOid {
helper.Fail500(w, requesterror.New("handleStoreLfsObject", r, "expected sha256 %s, got %s", a.LfsOid, shaStr))
helper.Fail500(w, r, fmt.Errorf("handleStoreLfsObject: expected sha256 %s, got %s", a.LfsOid, shaStr))
return
}
......
......@@ -9,7 +9,6 @@ import (
"time"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/urlprefix"
)
......@@ -29,11 +28,11 @@ func (s *Static) ServeExisting(prefix urlprefix.Prefix, cache CacheMode, notFoun
// The filepath.Join does Clean traversing directories up
if !strings.HasPrefix(file, s.DocumentRoot) {
helper.Fail500(w, requesterror.New("ServeExisting", r, "%v", &os.PathError{
helper.Fail500(w, r, &os.PathError{
Op: "open",
Path: file,
Err: os.ErrInvalid,
}))
})
return
}
......
......@@ -10,7 +10,6 @@ import (
"os"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
)
type MultipartFormProcessor interface {
......@@ -105,7 +104,7 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
func HandleFileUploads(w http.ResponseWriter, r *http.Request, h http.Handler, tempPath string, filter MultipartFormProcessor) {
if tempPath == "" {
helper.Fail500(w, requesterror.New("handleFileUploads", r, "tempPath empty"))
helper.Fail500(w, r, fmt.Errorf("handleFileUploads: tempPath empty"))
return
}
......@@ -119,7 +118,7 @@ func HandleFileUploads(w http.ResponseWriter, r *http.Request, h http.Handler, t
if err == http.ErrNotMultipart {
h.ServeHTTP(w, r)
} else {
helper.Fail500(w, requesterror.New("handleFileUploads", r, "extract files from multipart: %v", err))
helper.Fail500(w, r, fmt.Errorf("handleFileUploads: extract files from multipart: %v", err))
}
return
}
......
......@@ -41,7 +41,10 @@ func (a *testFormProcessor) ProcessField(formName string, writer *multipart.Writ
func TestUploadTempPathRequirement(t *testing.T) {
response := httptest.NewRecorder()
request := &http.Request{}
request, err := http.NewRequest("", "", nil)
if err != nil {
t.Fatal(err)
}
HandleFileUploads(response, request, nilHandler, "", nil)
testhelper.AssertResponseCode(t, response, 500)
}
......
......@@ -7,7 +7,6 @@ import (
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/requesterror"
)
func contentEncodingHandler(h http.Handler) http.Handler {
......@@ -27,7 +26,7 @@ func contentEncodingHandler(h http.Handler) http.Handler {
}
if err != nil {
helper.Fail500(w, requesterror.New("contentEncodingHandler", r, "%v", err))
helper.Fail500(w, r, fmt.Errorf("contentEncodingHandler: %v", err))
return
}
defer body.Close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment