增加日志与退出时404问题处理

This commit is contained in:
andy committed 2026-08-18 16:58:58 +08:00
1 parent 2a2f78c81e
commit 648b274c55
7 files changed
+226 -2

No files matched your search

+48
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/url"
"path"
"strings"
@@ -77,10 +78,12 @@ func (s *OSS) Put(ctx context.Context, key string, body io.Reader, size int64, c
}
err := s.client.Put(ctx, OSSPutRequest{Endpoint: s.config.Endpoint, Bucket: s.config.Bucket, Key: key, Body: body, Size: size, ContentType: contentType})
if err != nil {
logOSSOperationFailure("put", err)
return StoredObject{}, mapOSSError(err)
}
if s.config.PublicRead {
if err = s.client.SetACL(ctx, OSSACLRequest{Endpoint: s.config.Endpoint, Bucket: s.config.Bucket, Key: key, ACL: OSSACLPublicRead}); err != nil {
logOSSOperationFailure("set_acl", err)
return StoredObject{}, mapOSSError(err)
}
}
@@ -92,6 +95,9 @@ func (s *OSS) Read(ctx context.Context, key string) (Blob, error) {
}
blob, err := s.client.Get(ctx, OSSObjectRequest{Endpoint: s.config.Endpoint, Bucket: s.config.Bucket, Key: key})
if err != nil {
if !isMissingOSS(err) {
logOSSOperationFailure("get", err)
}
return Blob{}, mapOSSError(err)
}
return blob, nil
@@ -104,8 +110,50 @@ func (s *OSS) Delete(ctx context.Context, key string) error {
if isMissingOSS(err) {
return nil
}
if err != nil {
logOSSOperationFailure("delete", err)
}
return mapOSSError(err)
}
type ossOperationDiagnostic struct {
Operation string
Status int
Code string
ErrorClass string
}
func diagnoseOSSOperation(operation string, err error) ossOperationDiagnostic {
diagnostic := ossOperationDiagnostic{Operation: operation, ErrorClass: "client"}
var ossErr *OSSError
if errors.As(err, &ossErr) {
diagnostic.Status = ossErr.Status
diagnostic.Code = ossErr.Code
if ossErr.Status > 0 || ossErr.Code != "" {
diagnostic.ErrorClass = "service"
} else {
diagnostic.ErrorClass = "transport"
}
return diagnostic
}
if errors.Is(err, context.Canceled) {
diagnostic.ErrorClass = "canceled"
} else if errors.Is(err, context.DeadlineExceeded) {
diagnostic.ErrorClass = "timeout"
}
return diagnostic
}
func logOSSOperationFailure(operation string, err error) {
diagnostic := diagnoseOSSOperation(operation, err)
log.Printf(
"zhinian-api OSS operation failed operation=%s status=%d code=%q errorClass=%s",
diagnostic.Operation,
diagnostic.Status,
diagnostic.Code,
diagnostic.ErrorClass,
)
}
func mapOSSError(err error) error {
if err == nil {
return nil