filestore: Handle os.ErrNotExist not in core handler

This commit is contained in:
Marius 2021-04-25 23:17:42 +02:00
parent 8031aabb7e
commit 97602c3d62
5 changed files with 23 additions and 12 deletions

View File

@ -89,6 +89,10 @@ func (store FileStore) GetUpload(ctx context.Context, id string) (handler.Upload
info := handler.FileInfo{} info := handler.FileInfo{}
data, err := ioutil.ReadFile(store.infoPath(id)) data, err := ioutil.ReadFile(store.infoPath(id))
if err != nil { if err != nil {
if os.IsNotExist(err) {
// Interpret os.ErrNotExist as 404 Not Found
err = handler.ErrNotFound
}
return nil, err return nil, err
} }
if err := json.Unmarshal(data, &info); err != nil { if err := json.Unmarshal(data, &info); err != nil {
@ -99,6 +103,10 @@ func (store FileStore) GetUpload(ctx context.Context, id string) (handler.Upload
infoPath := store.infoPath(id) infoPath := store.infoPath(id)
stat, err := os.Stat(binPath) stat, err := os.Stat(binPath)
if err != nil { if err != nil {
if os.IsNotExist(err) {
// Interpret os.ErrNotExist as 404 Not Found
err = handler.ErrNotFound
}
return nil, err return nil, err
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"io" "io"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -74,7 +73,7 @@ func TestFilestore(t *testing.T) {
// Test if upload is deleted // Test if upload is deleted
upload, err = store.GetUpload(ctx, info.ID) upload, err = store.GetUpload(ctx, info.ID)
a.Equal(nil, upload) a.Equal(nil, upload)
a.True(os.IsNotExist(err)) a.Equal(handler.ErrNotFound, err)
} }
func TestMissingPath(t *testing.T) { func TestMissingPath(t *testing.T) {
@ -89,6 +88,18 @@ func TestMissingPath(t *testing.T) {
a.Equal(nil, upload) a.Equal(nil, upload)
} }
func TestNotFound(t *testing.T) {
a := assert.New(t)
store := FileStore{"./path"}
ctx := context.Background()
upload, err := store.GetUpload(ctx, "upload-that-does-not-exist")
a.Error(err)
a.Equal(handler.ErrNotFound, err)
a.Equal(nil, upload)
}
func TestConcatUploads(t *testing.T) { func TestConcatUploads(t *testing.T) {
a := assert.New(t) a := assert.New(t)

View File

@ -3,7 +3,6 @@ package handler_test
import ( import (
"context" "context"
"net/http" "net/http"
"os"
"testing" "testing"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
@ -64,7 +63,7 @@ func TestHead(t *testing.T) {
}) })
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetUpload(context.Background(), "no").Return(nil, os.ErrNotExist) store.EXPECT().GetUpload(context.Background(), "no").Return(nil, ErrNotFound)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -141,7 +140,7 @@ func TestPatch(t *testing.T) {
}) })
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) { SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetUpload(context.Background(), "no").Return(nil, os.ErrNotExist) store.EXPECT().GetUpload(context.Background(), "no").Return(nil, ErrNotFound)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: composer, StoreComposer: composer,

View File

@ -9,7 +9,6 @@ import (
"math" "math"
"net" "net"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -896,11 +895,6 @@ func (handler *UnroutedHandler) terminateUpload(ctx context.Context, upload Uplo
// Send the error in the response body. The status code will be looked up in // Send the error in the response body. The status code will be looked up in
// ErrStatusCodes. If none is found 500 Internal Error will be used. // ErrStatusCodes. If none is found 500 Internal Error will be used.
func (handler *UnroutedHandler) sendError(w http.ResponseWriter, r *http.Request, err error) { func (handler *UnroutedHandler) sendError(w http.ResponseWriter, r *http.Request, err error) {
// Interpret os.ErrNotExist as 404 Not Found
if os.IsNotExist(err) {
err = ErrNotFound
}
// Errors for read timeouts contain too much information which is not // Errors for read timeouts contain too much information which is not
// necessary for us and makes grouping for the metrics harder. The error // necessary for us and makes grouping for the metrics harder. The error
// message looks like: read tcp 127.0.0.1:1080->127.0.0.1:53673: i/o timeout // message looks like: read tcp 127.0.0.1:1080->127.0.0.1:53673: i/o timeout