Skip to content

Commit 6876fd6

Browse files
zeripathtechknowlogick
andcommittedOct 18, 2020
When handling errors in storageHandler check underlying error (go-gitea#13178)
Unfortunately there was a mistake in go-gitea#13164 which fails to handle os.PathError wrapping an os.ErrNotExist Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 1ba4a7e commit 6876fd6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
 

‎modules/storage/minio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type minioObject struct {
3030
func (m *minioObject) Stat() (os.FileInfo, error) {
3131
oi, err := m.Object.Stat()
3232
if err != nil {
33-
return nil, err
33+
return nil, convertMinioErr(err)
3434
}
3535

3636
return &minioFileInfo{oi}, nil

‎routers/routes/routes.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package routes
77
import (
88
"bytes"
99
"encoding/gob"
10+
"errors"
11+
"fmt"
1012
"io"
1113
"net/http"
1214
"path"
@@ -125,7 +127,13 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
125127
rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix)
126128
u, err := objStore.URL(rPath, path.Base(rPath))
127129
if err != nil {
128-
ctx.Error(500, err.Error())
130+
if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
131+
log.Warn("Unable to find %s %s", prefix, rPath)
132+
ctx.Error(404, "file not found")
133+
return
134+
}
135+
log.Error("Error whilst getting URL for %s %s. Error: %v", prefix, rPath, err)
136+
ctx.Error(500, fmt.Sprintf("Error whilst getting URL for %s %s", prefix, rPath))
129137
return
130138
}
131139
http.Redirect(
@@ -152,7 +160,13 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
152160
//If we have matched and access to release or issue
153161
fr, err := objStore.Open(rPath)
154162
if err != nil {
155-
ctx.Error(500, err.Error())
163+
if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
164+
log.Warn("Unable to find %s %s", prefix, rPath)
165+
ctx.Error(404, "file not found")
166+
return
167+
}
168+
log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
169+
ctx.Error(500, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath))
156170
return
157171
}
158172
defer fr.Close()

0 commit comments

Comments
 (0)