fix: 文件不存在时提供报错信息
getFileUrlLogic 捕获 S3 NotFound 返回语义明确的错误信息, getFileLogic 将 gRPC 错误转为普通 error 避免 HTTP 500。
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"juwan-backend/common/utils/contextj"
|
"juwan-backend/common/utils/contextj"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetFileLogic struct {
|
type GetFileLogic struct {
|
||||||
@@ -46,7 +47,7 @@ func (l *GetFileLogic) GetFile(req *types.GetFileReq) (string, error) {
|
|||||||
UserId: strconv.FormatInt(userID, 10),
|
UserId: strconv.FormatInt(userID, 10),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", errors.New(status.Convert(err).Message())
|
||||||
}
|
}
|
||||||
if rpcResp.GetUrl() == "" {
|
if rpcResp.GetUrl() == "" {
|
||||||
return "", errors.New("file url is empty")
|
return "", errors.New("file url is empty")
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"juwan-backend/app/objectstory/rpc/pb"
|
"juwan-backend/app/objectstory/rpc/pb"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
|
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +38,10 @@ func (l *GetFileUrlLogic) GetFileUrl(in *pb.GetFileUrlReq) (*pb.GetFileUrlResp,
|
|||||||
Key: &in.FileId,
|
Key: &in.FileId,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var nf *s3types.NotFound
|
||||||
|
if errors.As(err, &nf) {
|
||||||
|
return nil, errors.New("file not found")
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user