Skip to content

Commit da0c734

Browse files
authored
fix(net): unable to pass HttpStatusCode (#1397)
1 parent 189cebe commit da0c734

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

internal/errs/unwrap.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package errs
2+
3+
import "errors"
4+
5+
func UnwrapOrSelf(err error) error {
6+
// errors.Unwrap has no fallback mechanism
7+
unwrapped := errors.Unwrap(err)
8+
if unwrapped == nil {
9+
return err
10+
}
11+
return unwrapped
12+
}

internal/net/serve.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/OpenListTeam/OpenList/v4/internal/conf"
17+
"github.com/OpenListTeam/OpenList/v4/internal/errs"
1718
"github.com/OpenListTeam/OpenList/v4/internal/model"
1819
"github.com/OpenListTeam/OpenList/v4/pkg/http_range"
1920
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
@@ -114,7 +115,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
114115
reader, err := RangeReadCloser.RangeRead(ctx, http_range.Range{Length: -1})
115116
if err != nil {
116117
code = http.StatusRequestedRangeNotSatisfiable
117-
if statusCode, ok := errors.Unwrap(err).(HttpStatusCodeError); ok {
118+
if statusCode, ok := errs.UnwrapOrSelf(err).(HttpStatusCodeError); ok {
118119
code = int(statusCode)
119120
}
120121
http.Error(w, err.Error(), code)
@@ -137,7 +138,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
137138
sendContent, err = RangeReadCloser.RangeRead(ctx, ra)
138139
if err != nil {
139140
code = http.StatusRequestedRangeNotSatisfiable
140-
if statusCode, ok := errors.Unwrap(err).(HttpStatusCodeError); ok {
141+
if statusCode, ok := errs.UnwrapOrSelf(err).(HttpStatusCodeError); ok {
141142
code = int(statusCode)
142143
}
143144
http.Error(w, err.Error(), code)
@@ -199,7 +200,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
199200
log.Warnf("Maybe size incorrect or reader not giving correct/full data, or connection closed before finish. written bytes: %d ,sendSize:%d, ", written, sendSize)
200201
}
201202
code = http.StatusInternalServerError
202-
if statusCode, ok := errors.Unwrap(err).(HttpStatusCodeError); ok {
203+
if statusCode, ok := errs.UnwrapOrSelf(err).(HttpStatusCodeError); ok {
203204
code = int(statusCode)
204205
}
205206
w.WriteHeader(code)

0 commit comments

Comments
 (0)