Skip to content

Commit aca13f9

Browse files
When handling errors in storageHandler check underlying error (#13178) (#13193)
Unfortunately there was a mistake in #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> Co-authored-by: zeripath <art27@cantab.net>
1 parent 1ba4a7e commit aca13f9

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

modules/storage/minio.go

+21-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
@@ -58,6 +58,26 @@ type MinioStorage struct {
5858
basePath string
5959
}
6060

61+
func convertMinioErr(err error) error {
62+
if err == nil {
63+
return nil
64+
}
65+
errResp, ok := err.(minio.ErrorResponse)
66+
if !ok {
67+
return err
68+
}
69+
70+
// Convert two responses to standard analogues
71+
switch errResp.Code {
72+
case "NoSuchKey":
73+
return os.ErrNotExist
74+
case "AccessDenied":
75+
return os.ErrPermission
76+
}
77+
78+
return err
79+
}
80+
6181
// NewMinioStorage returns a minio storage
6282
func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error) {
6383
configInterface, err := toConfig(MinioStorageConfig{}, cfg)

routers/routes/routes.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ package routes
77
import (
88
"bytes"
99
"encoding/gob"
10+
"errors"
11+
"fmt"
1012
"io"
1113
"net/http"
14+
"os"
1215
"path"
1316
"strings"
1417
"text/template"
@@ -125,7 +128,13 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
125128
rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix)
126129
u, err := objStore.URL(rPath, path.Base(rPath))
127130
if err != nil {
128-
ctx.Error(500, err.Error())
131+
if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
132+
log.Warn("Unable to find %s %s", prefix, rPath)
133+
ctx.Error(404, "file not found")
134+
return
135+
}
136+
log.Error("Error whilst getting URL for %s %s. Error: %v", prefix, rPath, err)
137+
ctx.Error(500, fmt.Sprintf("Error whilst getting URL for %s %s", prefix, rPath))
129138
return
130139
}
131140
http.Redirect(
@@ -152,7 +161,13 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
152161
//If we have matched and access to release or issue
153162
fr, err := objStore.Open(rPath)
154163
if err != nil {
155-
ctx.Error(500, err.Error())
164+
if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
165+
log.Warn("Unable to find %s %s", prefix, rPath)
166+
ctx.Error(404, "file not found")
167+
return
168+
}
169+
log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
170+
ctx.Error(500, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath))
156171
return
157172
}
158173
defer fr.Close()

0 commit comments

Comments
 (0)