feat(worker): introduce ErrFileNotFound for consistent error handling and update GenStandardFile to use it

This commit is contained in:
keven1024
2026-02-27 16:39:25 +08:00
parent 60831c779e
commit 4a8932f921
3 changed files with 16 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
package services
import "errors"
var (
ErrFileNotFound = errors.New("FileNotFound")
)

View File

@@ -1,7 +1,6 @@
package services
import (
"errors"
"os"
"path/filepath"
"pkg/models"
@@ -20,7 +19,7 @@ type GenStandardFileReturn struct {
// 生成标准格式的file
func GenStandardFile(filePath string, mimeType string) (GenStandardFileReturn, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return GenStandardFileReturn{}, errors.New("文件不存在")
return GenStandardFileReturn{}, ErrFileNotFound
}
file, err := os.Open(filePath)
if err != nil {

View File

@@ -3,6 +3,8 @@ package tasks
import (
"context"
"encoding/json"
"errors"
"fmt"
"mime"
"path/filepath"
"pkg/models"
@@ -28,6 +30,9 @@ func CompressImage(ctx context.Context, task *asynq.Task) error {
originalPath := filepath.Join(uploadPath, payload.FileId)
compressedPath, err := services.CompressImage(originalPath, originalFileInfo.MimeType)
if err != nil {
if errors.Is(err, services.ErrUnsupportedMimeType) {
return fmt.Errorf("%w: %w", err, asynq.SkipRetry)
}
return err
}
compressedFileInfo, err := services.GenStandardFile(compressedPath, originalFileInfo.MimeType)
@@ -70,6 +75,9 @@ func ConvertImage(ctx context.Context, task *asynq.Task) error {
originalPath := filepath.Join(uploadPath, payload.FileId)
convertedPath, err := services.ConvertImageWithMagick(originalPath, originalFileInfo.MimeType, payload.TargetExt)
if err != nil {
if errors.Is(err, services.ErrUnsupportedMimeType) {
return fmt.Errorf("%w: %w", err, asynq.SkipRetry)
}
return err
}
mimeType := mime.TypeByExtension(payload.TargetExt)