diff --git a/backend/cmd/server/wire_gen.go b/backend/cmd/server/wire_gen.go index ed56fb894b..2af1813537 100644 --- a/backend/cmd/server/wire_gen.go +++ b/backend/cmd/server/wire_gen.go @@ -214,7 +214,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) { soraGatewayService := service.NewSoraGatewayService(soraSDKClient, rateLimitService, httpUpstream, configConfig) soraClientHandler := handler.NewSoraClientHandler(soraGenerationService, soraQuotaService, soraStorageRouter, soraGatewayService, gatewayService, soraMediaStorage, apiKeyService) soraTaskRepository := repository.NewSoraTaskRepository(db) - soraTaskService := service.NewSoraTaskService(soraTaskRepository, soraSDKClient, httpUpstream) + soraTaskService := service.NewSoraTaskService(soraTaskRepository, accountRepository, soraSDKClient, httpUpstream) soraTaskWorker := service.NewSoraTaskWorker(soraTaskService, accountRepository, soraStorageRouter, soraMediaStorage, 60*time.Second) soraTaskWorker.Start() soraVideosHandler := handler.NewSoraVideosHandler(soraTaskService, gatewayService, soraStorageRouter, soraMediaStorage, soraGatewayService) diff --git a/backend/internal/handler/sora_videos_handler.go b/backend/internal/handler/sora_videos_handler.go index fff9041394..35c9ad8072 100644 --- a/backend/internal/handler/sora_videos_handler.go +++ b/backend/internal/handler/sora_videos_handler.go @@ -2,6 +2,7 @@ package handler import ( "encoding/json" + "errors" "fmt" "net/http" "unicode/utf8" @@ -64,8 +65,7 @@ func (h *SoraVideosHandler) CreateVideo(c *gin.Context) { task, err := h.taskService.CreateVideoTask(c.Request.Context(), apiKey.ID, account, &req, body) if err != nil { - logger.LegacyPrintf("handler.sora_videos", "[CreateVideo] error: %v", err) - soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create video task") + handleTaskCreateError(c, "CreateVideo", err) return } @@ -146,8 +146,7 @@ func (h *SoraVideosHandler) RemixVideo(c *gin.Context) { task, err := h.taskService.CreateVideoTask(c.Request.Context(), apiKey.ID, account, videoReq, reqBody) if err != nil { - logger.LegacyPrintf("handler.sora_videos", "[RemixVideo] error: %v", err) - soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create remix task") + handleTaskCreateError(c, "RemixVideo", err) return } @@ -250,8 +249,7 @@ func (h *SoraVideosHandler) CreateImage(c *gin.Context) { task, err := h.taskService.CreateImageGeneration(c.Request.Context(), apiKey.ID, account, &req, body) if err != nil { - logger.LegacyPrintf("handler.sora_videos", "[CreateImage] error: %v", err) - soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create image task") + handleTaskCreateError(c, "CreateImage", err) return } @@ -296,8 +294,7 @@ func (h *SoraVideosHandler) EditImage(c *gin.Context) { task, err := h.taskService.CreateImageGeneration(c.Request.Context(), apiKey.ID, account, imageReq, body) if err != nil { - logger.LegacyPrintf("handler.sora_videos", "[EditImage] error: %v", err) - soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create image edit task") + handleTaskCreateError(c, "EditImage", err) return } @@ -334,17 +331,8 @@ func (h *SoraVideosHandler) selectAccount(c *gin.Context, model string) (*servic return apiKey, selection.Account, true } -func (h *SoraVideosHandler) selectAccountByID(c *gin.Context, _ int64) (*service.Account, error) { - selection, err := h.gatewayService.SelectAccountWithLoadAwareness( - c.Request.Context(), nil, "", "", nil, "", - ) - if err != nil { - return nil, err - } - if selection.ReleaseFunc != nil { - selection.ReleaseFunc() - } - return selection.Account, nil +func (h *SoraVideosHandler) selectAccountByID(c *gin.Context, accountID int64) (*service.Account, error) { + return h.taskService.GetAccountByID(c.Request.Context(), accountID) } func readBody(c *gin.Context) ([]byte, error) { @@ -373,6 +361,18 @@ func soraErrorResponse(c *gin.Context, status int, errType, message string) { }) } +// handleTaskCreateError writes the appropriate error response, transparently +// forwarding upstream HTTP status codes when available. +func handleTaskCreateError(c *gin.Context, logTag string, err error) { + logger.LegacyPrintf("handler.sora_videos", "[%s] error: %v", logTag, err) + var ue *service.SoraUpstreamError + if errors.As(err, &ue) { + c.Data(ue.StatusCode, "application/json", ue.Body) + return + } + soraErrorResponse(c, http.StatusInternalServerError, "server_error", "Failed to create task") +} + func inferImageModel(size string) string { switch size { case "540x360": diff --git a/backend/internal/service/sora_task_service.go b/backend/internal/service/sora_task_service.go index 7983345722..0cfaa1d376 100644 --- a/backend/internal/service/sora_task_service.go +++ b/backend/internal/service/sora_task_service.go @@ -18,17 +18,20 @@ import ( // SoraTaskService manages Sora async task creation and status queries. type SoraTaskService struct { repo SoraTaskRepository + accountRepo AccountRepository soraClient SoraClient httpUpstream HTTPUpstream } func NewSoraTaskService( repo SoraTaskRepository, + accountRepo AccountRepository, soraClient SoraClient, httpUpstream HTTPUpstream, ) *SoraTaskService { return &SoraTaskService{ repo: repo, + accountRepo: accountRepo, soraClient: soraClient, httpUpstream: httpUpstream, } @@ -259,6 +262,10 @@ func (s *SoraTaskService) GetTaskByID(ctx context.Context, taskID string) (*Sora return s.repo.GetByID(ctx, taskID) } +func (s *SoraTaskService) GetAccountByID(ctx context.Context, accountID int64) (*Account, error) { + return s.accountRepo.GetByID(ctx, accountID) +} + // ── Internal methods ── func (s *SoraTaskService) createViaSdk( @@ -324,7 +331,7 @@ func (s *SoraTaskService) forwardCreateToUpstream( return nil, fmt.Errorf("read upstream response: %w", err) } if resp.StatusCode >= 400 { - return nil, fmt.Errorf("upstream error %d: %s", resp.StatusCode, string(respBody)) + return nil, &SoraUpstreamError{StatusCode: resp.StatusCode, Body: respBody} } var result map[string]any