Simplify error handling

This commit is contained in:
Lena Fuhrimann 2018-06-23 12:49:44 +02:00
parent 2f45b53b6a
commit bd4b6c1b33
6 changed files with 31 additions and 30 deletions

24
Gopkg.lock generated
View file

@ -5,19 +5,19 @@
branch = "master" branch = "master"
name = "github.com/dustin/go-humanize" name = "github.com/dustin/go-humanize"
packages = ["."] packages = ["."]
revision = "02af3965c54e8cacf948b97fef38925c4120652c" revision = "9f541cc9db5d55bce703bd99987c9d5cb8eea45e"
[[projects]] [[projects]]
name = "github.com/go-ini/ini" name = "github.com/go-ini/ini"
packages = ["."] packages = ["."]
revision = "06f5f3d67269ccec1fe5fe4134ba6e982984f7f5" revision = "358ee7663966325963d4e8b2e1fbd570c5195153"
version = "v1.37.0" version = "v1.38.1"
[[projects]] [[projects]]
branch = "master" branch = "master"
name = "github.com/mastertinner/adapters" name = "github.com/mastertinner/adapters"
packages = ["logging"] packages = ["logging"]
revision = "9704a6fb93c903ec6e38a8420cc82d3a95668966" revision = "13d009731b3accb5b099171fb329a6d6b4e82a87"
[[projects]] [[projects]]
name = "github.com/matryer/is" name = "github.com/matryer/is"
@ -41,8 +41,8 @@
"pkg/s3utils", "pkg/s3utils",
"pkg/set" "pkg/set"
] ]
revision = "3adf3e30ef96aea8469c7aff8ef19ed38901817b" revision = "70799fe8dae6ecfb6c7d7e9e048fce27f23a1992"
version = "v6.0.4" version = "v6.0.5"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -59,8 +59,8 @@
[[projects]] [[projects]]
name = "github.com/sirupsen/logrus" name = "github.com/sirupsen/logrus"
packages = ["."] packages = ["."]
revision = "c155da19408a8799da419ed3eeb0cb5db0ad5dbc" revision = "3e01752db0189b9157070a0e1668a620f9a85da2"
version = "v1.0.5" version = "v1.0.6"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -70,7 +70,7 @@
"blake2b", "blake2b",
"ssh/terminal" "ssh/terminal"
] ]
revision = "a49355c7e3f8fe157a85be2f77e6e269a0f89602" revision = "c126467f60eb25f8f27e5a981f32a87e3965053f"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -79,7 +79,7 @@
"http/httpguts", "http/httpguts",
"idna" "idna"
] ]
revision = "afe8f62b1d6bbd81f31868121a50b06d8188e1f9" revision = "a680a1efc54dd51c040b3b5ce4939ea3cf2ea0d1"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -89,7 +89,7 @@
"unix", "unix",
"windows" "windows"
] ]
revision = "63fc586f45fe72d95d5240a5d5eb95e6503907d3" revision = "ac767d655b305d4e9612f5f6e33120b9176c4ad4"
[[projects]] [[projects]]
name = "golang.org/x/text" name = "golang.org/x/text"
@ -115,6 +115,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "f3d6f693907c972aed6e0bca37e46ec1714fb631b4de4cf5dab7ba20ddf06e19" inputs-digest = "36142ff417913b235566aeb04f404c879ab02b36fbeffc45235cc058ee223141"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View file

@ -39,7 +39,7 @@
[[constraint]] [[constraint]]
name = "github.com/minio/minio-go" name = "github.com/minio/minio-go"
version = "6.0.4" version = "6.0.5"
[[constraint]] [[constraint]]
name = "github.com/pkg/errors" name = "github.com/pkg/errors"

View file

@ -22,6 +22,7 @@ func HandleBucketView(s3 S3, tmplDir string) http.HandlerFunc {
BucketName string BucketName string
Objects []objectWithIcon Objects []objectWithIcon
} }
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bucketName := way.Param(r.Context(), "bucketName") bucketName := way.Param(r.Context(), "bucketName")

View file

@ -89,7 +89,7 @@ func TestHandleBucketView(t *testing.T) {
listObjectsV2Func: func(string, string, bool, <-chan struct{}) <-chan minio.ObjectInfo { listObjectsV2Func: func(string, string, bool, <-chan struct{}) <-chan minio.ObjectInfo {
objCh := make(chan minio.ObjectInfo) objCh := make(chan minio.ObjectInfo)
go func() { go func() {
objCh <- minio.ObjectInfo{Err: s3manager.ErrBucketDoesNotExist} objCh <- minio.ObjectInfo{Err: errors.New("The specified bucket does not exist")}
close(objCh) close(objCh)
}() }()
return objCh return objCh

View file

@ -25,7 +25,8 @@ func HandleCreateObject(s3 S3) http.HandlerFunc {
} }
defer file.Close() defer file.Close()
_, err = s3.PutObject(bucketName, handler.Filename, file, 1, minio.PutObjectOptions{ContentType: "application/octet-stream"}) opts := minio.PutObjectOptions{ContentType: "application/octet-stream"}
_, err = s3.PutObject(bucketName, handler.Filename, file, 1, opts)
if err != nil { if err != nil {
handleHTTPError(w, errors.Wrap(err, "error putting object")) handleHTTPError(w, errors.Wrap(err, "error putting object"))
return return

View file

@ -9,27 +9,26 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Errors that may be returned from an S3 client. // Error codes that may be returned from an S3 client.
var ( const (
ErrBucketDoesNotExist = errors.New("The specified bucket does not exist.") // nolint: golint ErrBucketDoesNotExist = "The specified bucket does not exist"
ErrKeyDoesNotExist = errors.New("The specified key does not exist.") // nolint: golint ErrKeyDoesNotExist = "The specified key does not exist"
) )
// handleHTTPError handles HTTP errors. // handleHTTPError handles HTTP errors.
func handleHTTPError(w http.ResponseWriter, err error) { func handleHTTPError(w http.ResponseWriter, err error) {
var code int cause := errors.Cause(err)
code := http.StatusInternalServerError
switch err := errors.Cause(err).(type) { _, ok := cause.(*json.SyntaxError)
case *json.SyntaxError: if ok {
code = http.StatusUnprocessableEntity code = http.StatusUnprocessableEntity
default: }
if err == io.EOF || err == io.ErrUnexpectedEOF { switch {
code = http.StatusUnprocessableEntity case cause == io.EOF || cause == io.ErrUnexpectedEOF:
} else if err == ErrBucketDoesNotExist || err == ErrKeyDoesNotExist { code = http.StatusUnprocessableEntity
code = http.StatusNotFound case cause.Error() == ErrBucketDoesNotExist || cause.Error() == ErrKeyDoesNotExist:
} else { code = http.StatusNotFound
code = http.StatusInternalServerError
}
} }
http.Error(w, http.StatusText(code), code) http.Error(w, http.StatusText(code), code)