diff --git a/internal/metadata/project.go b/internal/metadata/project.go index cecca2578..44ddba168 100644 --- a/internal/metadata/project.go +++ b/internal/metadata/project.go @@ -3,10 +3,14 @@ package metadata import ( "context" "database/sql" + "errors" "fmt" "sync" ) +var ErrDuplicatedDataset = errors.New("dataset is already created") +var ErrDuplicatedJob = errors.New("job is already created") + type Project struct { ID string datasets []*Dataset @@ -69,7 +73,7 @@ func (p *Project) AddDataset(ctx context.Context, tx *sql.Tx, dataset *Dataset) p.mu.Lock() defer p.mu.Unlock() if _, exists := p.datasetMap[dataset.ID]; exists { - return fmt.Errorf("dataset %s is already created", dataset.ID) + return fmt.Errorf("dataset %s: %w", dataset.ID, ErrDuplicatedDataset) } if err := dataset.Insert(ctx, tx); err != nil { return err @@ -111,7 +115,7 @@ func (p *Project) AddJob(ctx context.Context, tx *sql.Tx, job *Job) error { p.mu.Lock() defer p.mu.Unlock() if _, exists := p.jobMap[job.ID]; exists { - return fmt.Errorf("job %s is already created", job.ID) + return ErrDuplicatedJob } if err := job.Insert(ctx, tx); err != nil { return err diff --git a/server/handler.go b/server/handler.go index 692b66470..cd7c7a2ac 100644 --- a/server/handler.go +++ b/server/handler.go @@ -680,6 +680,10 @@ func (h *datasetsInsertHandler) ServeHTTP(w http.ResponseWriter, r *http.Request dataset: &dataset, }) if err != nil { + if errors.Is(err, metadata.ErrDuplicatedDataset) { + errDuplicate(err.Error()) + return + } errorResponse(ctx, w, errInternalError(err.Error())) return } @@ -723,6 +727,9 @@ func (h *datasetsInsertHandler) Handle(ctx context.Context, r *datasetsInsertReq nil, ), ); err != nil { + if errors.Is(err, metadata.ErrDuplicatedDataset) { + return nil, errDuplicate(err.Error()) + } return nil, err } if err := tx.Commit(); err != nil {