mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2026-11-11 05:00:47 +08:00
fix(net): unable to pass HttpStatusCode (#1397)
This commit is contained in:
1 parent
189cebe4c9
commit
da0c734aa3
2 files changed
+16
-3
No files matched your search
@@ -0,0 +1,12 @@
|
||||
package errs
|
||||
|
||||
import "errors"
|
||||
|
||||
func UnwrapOrSelf(err error) error {
|
||||
// errors.Unwrap has no fallback mechanism
|
||||
unwrapped := errors.Unwrap(err)
|
||||
if unwrapped == nil {
|
||||
return err
|
||||
}
|
||||
return unwrapped
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/conf"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/errs"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
||||
"github.com/OpenListTeam/OpenList/v4/pkg/http_range"
|
||||
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
|
||||
@@ -114,7 +115,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
|
||||
reader, err := RangeReadCloser.RangeRead(ctx, http_range.Range{Length: -1})
|
||||
if err != nil {
|
||||
code = http.StatusRequestedRangeNotSatisfiable
|
||||
if statusCode, ok := errors.Unwrap(err).(HttpStatusCodeError); ok {
|
||||
if statusCode, ok := errs.UnwrapOrSelf(err).(HttpStatusCodeError); ok {
|
||||
code = int(statusCode)
|
||||
}
|
||||
http.Error(w, err.Error(), code)
|
||||
@@ -137,7 +138,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
|
||||
sendContent, err = RangeReadCloser.RangeRead(ctx, ra)
|
||||
if err != nil {
|
||||
code = http.StatusRequestedRangeNotSatisfiable
|
||||
if statusCode, ok := errors.Unwrap(err).(HttpStatusCodeError); ok {
|
||||
if statusCode, ok := errs.UnwrapOrSelf(err).(HttpStatusCodeError); ok {
|
||||
code = int(statusCode)
|
||||
}
|
||||
http.Error(w, err.Error(), code)
|
||||
@@ -199,7 +200,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
|
||||
log.Warnf("Maybe size incorrect or reader not giving correct/full data, or connection closed before finish. written bytes: %d ,sendSize:%d, ", written, sendSize)
|
||||
}
|
||||
code = http.StatusInternalServerError
|
||||
if statusCode, ok := errors.Unwrap(err).(HttpStatusCodeError); ok {
|
||||
if statusCode, ok := errs.UnwrapOrSelf(err).(HttpStatusCodeError); ok {
|
||||
code = int(statusCode)
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
|
||||
Reference in new issue
Block a user