Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions backend/internal/extension/repo/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repo

import (
"context"
"fmt"

"entgo.io/ent/dialect/sql"

Expand All @@ -22,11 +23,18 @@ func NewExtensionRepo(db *db.Client) domain.ExtensionRepo {

// Latest implements domain.ExtensionRepo.
func (e *ExtensionRepo) Latest(ctx context.Context) (*db.Extension, error) {
return e.db.Extension.
es, err := e.db.Extension.
Query().
Order(extension.ByCreatedAt(sql.OrderDesc())).
Limit(1).
Only(ctx)
All(ctx)
if err != nil {
return nil, err
}
if len(es) == 0 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在修改后的代码中,虽然添加了对空结果的处理,但使用fmt.Errorf创建错误信息不是最佳实践。应该定义一个特定的错误类型或使用项目中已有的错误码。

Suggested change
if len(es) == 0 {
// 定义一个特定的错误变量
var ErrExtensionNotFound = errors.New("extension not found")
// 在Latest方法中使用这个错误变量
if len(es) == 0 {
return nil, ErrExtensionNotFound
}

return nil, fmt.Errorf("extension not found")
}
return es[0], nil
}

// Save implements domain.ExtensionRepo.
Expand Down
Loading