mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-09 00:56:37 +08:00
autoresearch iter 2: compare error sentinels with errors.Is
Five sites used == against sentinels (redis.Nil, ingest.ErrForbidden, errs.ErrDatabaseUninitialized). The neighbouring not-found checks already went through errors.Is helpers, so a wrapped error would silently downgrade a 403 to a 400 and a 500 to a 400.
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"Wavelet/plugins/domain/admin/errs"
|
"Wavelet/plugins/domain/admin/errs"
|
||||||
"Wavelet/plugins/domain/admin/model"
|
"Wavelet/plugins/domain/admin/model"
|
||||||
"Wavelet/plugins/domain/admin/service"
|
"Wavelet/plugins/domain/admin/service"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -109,7 +110,7 @@ func ExecuteSQL(c *gin.Context) {
|
|||||||
|
|
||||||
resp, err := service.ExecuteCustomSQL(c.Request.Context(), trimmedSQL)
|
resp, err := service.ExecuteCustomSQL(c.Request.Context(), trimmedSQL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == errs.ErrDatabaseUninitialized {
|
if errors.Is(err, errs.ErrDatabaseUninitialized) {
|
||||||
response.AbortInternal(c, err.Error())
|
response.AbortInternal(c, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package pow
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -149,7 +150,7 @@ func NewRedisStore(client redis.UniversalClient) *RedisStore {
|
|||||||
// Get 从 RedisStore 获取指定 key 的值
|
// Get 从 RedisStore 获取指定 key 的值
|
||||||
func (s *RedisStore) Get(ctx context.Context, key string) (string, bool, error) {
|
func (s *RedisStore) Get(ctx context.Context, key string) (string, bool, error) {
|
||||||
val, err := s.client.Get(ctx, key).Result()
|
val, err := s.client.Get(ctx, key).Result()
|
||||||
if err == redis.Nil {
|
if errors.Is(err, redis.Nil) {
|
||||||
return "", false, nil
|
return "", false, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -176,7 +177,7 @@ func (s *RedisStore) SetNX(ctx context.Context, key, val string, ttl time.Durati
|
|||||||
// GetAndDelete wraps Redis GETDEL (available since Redis 6.2).
|
// GetAndDelete wraps Redis GETDEL (available since Redis 6.2).
|
||||||
func (s *RedisStore) GetAndDelete(ctx context.Context, key string) (string, bool, error) {
|
func (s *RedisStore) GetAndDelete(ctx context.Context, key string) (string, bool, error) {
|
||||||
val, err := s.client.GetDel(ctx, key).Result()
|
val, err := s.client.GetDel(ctx, key).Result()
|
||||||
if err == redis.Nil {
|
if errors.Is(err, redis.Nil) {
|
||||||
return "", false, nil
|
return "", false, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"Wavelet/plugins/domain/upload/models"
|
"Wavelet/plugins/domain/upload/models"
|
||||||
"Wavelet/plugins/domain/upload/repository"
|
"Wavelet/plugins/domain/upload/repository"
|
||||||
"Wavelet/plugins/domain/upload/shared"
|
"Wavelet/plugins/domain/upload/shared"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -233,7 +234,7 @@ func DeleteMyFile(c *gin.Context) {
|
|||||||
response.AbortNotFound(c, shared.ErrFileRecordNotFound)
|
response.AbortNotFound(c, shared.ErrFileRecordNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err == ingest.ErrForbidden {
|
if errors.Is(err, ingest.ErrForbidden) {
|
||||||
response.AbortForbidden(c, shared.ErrOperationForbidden)
|
response.AbortForbidden(c, shared.ErrOperationForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -287,7 +288,7 @@ func UpdateMyFile(c *gin.Context) {
|
|||||||
response.AbortNotFound(c, shared.ErrFileRecordNotFound)
|
response.AbortNotFound(c, shared.ErrFileRecordNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err == ingest.ErrForbidden {
|
if errors.Is(err, ingest.ErrForbidden) {
|
||||||
response.AbortForbidden(c, shared.ErrOperationForbidden)
|
response.AbortForbidden(c, shared.ErrOperationForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user