Skip to content

Commit

Permalink
Merge pull request #18 from infra-io/develop
Browse files Browse the repository at this point in the history
v0.5.1
  • Loading branch information
FishGoddess authored Aug 11, 2024
2 parents 5454c94 + ae3c49c commit e3d99ca
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 146 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v0.5.1

> 此版本发布于 2024-08-11
* 重构错误处理

### v0.5.0

> 此版本发布于 2024-08-08
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: all fmt test build clean proto postar postaradmin

VERSION=v0.5.0
VERSION=v0.5.1

all:
make test && make clean && make build
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/FishGoddess/cachego v0.6.1
github.com/FishGoddess/cryptox v0.4.3
github.com/FishGoddess/errors v0.5.2
github.com/FishGoddess/errors v0.7.0
github.com/FishGoddess/logit v1.8.1
github.com/go-sql-driver/mysql v1.7.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/FishGoddess/cachego v0.6.1 h1:mbytec3loqw5dcO177LyRGyStKG0AngP5g2GR3S
github.com/FishGoddess/cachego v0.6.1/go.mod h1:VLSMwWRlRPazjGYer8pq+4aDrIe1Otj3Yy9HAb0eo3c=
github.com/FishGoddess/cryptox v0.4.3 h1:EP1732fzkkgWKi7RSdVje+/pN8WeYFtzdHhJG4JcSs4=
github.com/FishGoddess/cryptox v0.4.3/go.mod h1:1j6yGIdxvxJ/V0XThrGhZ+c0NzMi0WxO7WeDFTfNyIM=
github.com/FishGoddess/errors v0.5.2 h1:PYjU6Xb72NXbSKUlwNzHszdUz9/VGS8/CBVAh0U/IwM=
github.com/FishGoddess/errors v0.5.2/go.mod h1:gXd4tNYX6ZFr2XmelF2DHn2uF7oTAi+O9ToJDjNeneE=
github.com/FishGoddess/errors v0.7.0 h1:yPNSUvvD9xRnhHlZ6SAo/yAjxUGceD/W1ITjhPWu/c4=
github.com/FishGoddess/errors v0.7.0/go.mod h1:sjLqiuMcGykL4Wl7/wI13SJF4i+ZfL1ThSlPikZLLh0=
github.com/FishGoddess/logit v1.8.1 h1:FQM3A2KHZHT88DmcGibWO2ho69jhwJSYswa6JtsAzGo=
github.com/FishGoddess/logit v1.8.1/go.mod h1:6IAtVuIW3AaPTBn2NDfBnML7t0F7/wj1FFeWIPLJy98=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 1 addition & 1 deletion internal/postar-admin/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func checkSpaceInterceptor(spaceService service.SpaceService) grpc.UnaryServerIn

if space.Token != spaceToken {
err = errors.New("wrong token")
return nil, errors.Forbidden(err, errors.WithMsg("业务空间的令牌错误"))
return nil, errors.Forbidden("业务空间 (%d)%s 的令牌错误", space.ID, space.Name)
}

return handler(ctx, req)
Expand Down
38 changes: 13 additions & 25 deletions internal/postar-admin/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package service

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -40,28 +39,23 @@ func NewAccountService(conf *config.PostarAdminConfig, accountStore AccountStore

func (das *defaultAccountService) checkCreateAccountParams(account *model.Account) error {
if strings.TrimSpace(account.Host) == "" {
err := errors.New("trim account.Host == ''")
return errors.BadRequest(err, errors.WithMsg("账号主机不能为空"))
return errors.BadRequest("账号主机不能为空")
}

if account.Port <= 0 {
err := errors.New("account.Port <= 0")
return errors.BadRequest(err, errors.WithMsg("账号端口非法"))
return errors.BadRequest("账号端口需要大于 0")
}

if strings.TrimSpace(account.Username) == "" {
err := errors.New("trim account.Username == ''")
return errors.BadRequest(err, errors.WithMsg("账号用户名不能为空"))
return errors.BadRequest("账号用户名不能为空")
}

if account.Password == "" {
err := errors.New("account.Password == ''")
return errors.BadRequest(err, errors.WithMsg("账号密码不能为空"))
return errors.BadRequest("账号密码不能为空")
}

if account.SMTPAuth <= 0 {
err := errors.New("account.SMTPAuth <= 0")
return errors.BadRequest(err, errors.WithMsg("未指定 SMTP 认证方式"))
return errors.BadRequest("未指定 SMTP 认证方式")
}

return nil
Expand Down Expand Up @@ -99,13 +93,11 @@ func (das *defaultAccountService) CreateAccount(ctx context.Context, spaceID int

func (das *defaultAccountService) checkUpdateAccountParams(account *model.Account) error {
if account.ID <= 0 {
err := fmt.Errorf("account.ID %d <= 0", account.ID)
return errors.BadRequest(err, errors.WithMsg("账号编号需要大于 0"))
return errors.BadRequest("账号编号需要大于 0")
}

if account.State > 0 && !account.State.Valid() {
err := fmt.Errorf("account.State %d not valid", account.State)
return errors.BadRequest(err, errors.WithMsg("账号状态无效"))
return errors.BadRequest("账号状态 %d 无效", account.State)
}

return nil
Expand Down Expand Up @@ -142,10 +134,9 @@ func (das *defaultAccountService) UpdateAccount(ctx context.Context, spaceID int
return account, nil
}

func (das *defaultAccountService) checkGetAccountParams(spaceID int32, accountID int32) error {
func (das *defaultAccountService) checkGetAccountParams(accountID int32) error {
if accountID <= 0 {
err := fmt.Errorf("accountID %d <= 0", accountID)
return errors.BadRequest(err, errors.WithMsg("账号编号非法"))
return errors.BadRequest("账号编号需要大于 0")
}

return nil
Expand All @@ -154,7 +145,7 @@ func (das *defaultAccountService) checkGetAccountParams(spaceID int32, accountID
func (das *defaultAccountService) GetAccount(ctx context.Context, spaceID int32, accountID int32, withPassword bool) (*model.Account, error) {
logger := logit.FromContext(ctx)

if err := das.checkGetAccountParams(spaceID, accountID); err != nil {
if err := das.checkGetAccountParams(accountID); err != nil {
logger.Error("check get account params failed", "err", err, "account_id", accountID)
return nil, err
}
Expand Down Expand Up @@ -182,18 +173,15 @@ func (das *defaultAccountService) GetAccount(ctx context.Context, spaceID int32,

func (das *defaultAccountService) checkListAccountsParams(pageSize int32, filter *model.ListAccountsFilter) error {
if pageSize < minPageSize || pageSize > maxPageSize {
err := fmt.Errorf("pageSize %d not in [%d, %d]", pageSize, minPageSize, maxPageSize)
return errors.BadRequest(err, errors.WithMsg("分页大小需要位于区间 [%d, %d] 内", minPageSize, maxPageSize))
return errors.BadRequest("分页大小 %d 需要位于区间 [%d, %d] 内", pageSize, minPageSize, maxPageSize)
}

if filter.AccountID < 0 {
err := fmt.Errorf("filter.AccountID %d < 0", filter.AccountID)
return errors.BadRequest(err, errors.WithMsg("过滤的账号编号非法"))
return errors.BadRequest("账号编号不能为负数")
}

if filter.AccountState > 0 && !filter.AccountState.Valid() {
err := fmt.Errorf("filter.AccountState %d not valid", filter.AccountState)
return errors.BadRequest(err, errors.WithMsg("过滤的账号状态非法"))
return errors.BadRequest("账号状态 %d 无效", filter.AccountState)
}

return nil
Expand Down
25 changes: 8 additions & 17 deletions internal/postar-admin/service/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package service

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -41,8 +40,7 @@ func NewSpaceService(conf *config.PostarAdminConfig, spaceStore SpaceStore) Spac

func (dss *defaultSpaceService) checkCreateSpaceParams(space *model.Space) error {
if strings.TrimSpace(space.Name) == "" {
err := errors.New("trim space.Name == ''")
return errors.BadRequest(err, errors.WithMsg("空间名称不能为空"))
return errors.BadRequest("业务空间名称不能为空")
}

return nil
Expand Down Expand Up @@ -80,18 +78,15 @@ func (dss *defaultSpaceService) CreateSpace(ctx context.Context, space *model.Sp

func (dss *defaultSpaceService) checkUpdateSpaceParams(space *model.Space) error {
if space.ID <= 0 {
err := fmt.Errorf("space.ID %d <= 0", space.ID)
return errors.BadRequest(err, errors.WithMsg("空间编号需要大于 0"))
return errors.BadRequest("业务空间编号需要大于 0")
}

if strings.TrimSpace(space.Name) == "" {
err := errors.New("trim space.Name == ''")
return errors.BadRequest(err, errors.WithMsg("空间名称不能为空"))
return errors.BadRequest("业务空间名称不能为空")
}

if space.State > 0 && !space.State.Valid() {
err := fmt.Errorf("space.State %d not valid", space.State)
return errors.BadRequest(err, errors.WithMsg("账号状态无效"))
return errors.BadRequest("业务空间状态 %d 无效", space.State)
}

return nil
Expand All @@ -118,8 +113,7 @@ func (dss *defaultSpaceService) UpdateSpace(ctx context.Context, space *model.Sp

func (dss *defaultSpaceService) checkGetSpaceParams(spaceID int32) error {
if spaceID <= 0 {
err := fmt.Errorf("spaceID %d <= 0", spaceID)
return errors.BadRequest(err, errors.WithMsg("空间编号非法"))
return errors.BadRequest("业务空间编号需要大于 0")
}

return nil
Expand Down Expand Up @@ -156,18 +150,15 @@ func (dss *defaultSpaceService) GetSpace(ctx context.Context, spaceID int32, wit

func (dss *defaultSpaceService) checkListSpacesParams(pageSize int32, filter *model.ListSpacesFilter) error {
if pageSize < minPageSize || pageSize > maxPageSize {
err := fmt.Errorf("pageSize %d not in [%d, %d]", pageSize, minPageSize, maxPageSize)
return errors.BadRequest(err, errors.WithMsg("分页大小需要位于区间 [%d, %d] 内", minPageSize, maxPageSize))
return errors.BadRequest("分页大小 %d 需要位于区间 [%d, %d] 内", pageSize, minPageSize, maxPageSize)
}

if filter.SpaceID < 0 {
err := fmt.Errorf("filter.SpaceID %d < 0", filter.SpaceID)
return errors.BadRequest(err, errors.WithMsg("过滤的空间编号非法"))
return errors.BadRequest("业务空间编号不能为负数")
}

if filter.SpaceState > 0 && !filter.SpaceState.Valid() {
err := fmt.Errorf("filter.SpaceState %d not valid", filter.SpaceState)
return errors.BadRequest(err, errors.WithMsg("过滤的空间状态非法"))
return errors.BadRequest("业务空间状态 %d 无效", filter.SpaceState)
}

return nil
Expand Down
49 changes: 16 additions & 33 deletions internal/postar-admin/service/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package service

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -40,23 +39,19 @@ func NewTemplateService(conf *config.PostarAdminConfig, templateStore TemplateSt

func (dts *defaultTemplateService) checkCreateTemplateParams(template *model.Template) error {
if template.AccountID <= 0 {
err := errors.New("template.AccountID <= 0")
return errors.BadRequest(err, errors.WithMsg("模板绑定的账号编号非法"))
return errors.BadRequest("模板绑定的账号编号需要大于 0")
}

if strings.TrimSpace(template.Name) == "" {
err := errors.New("trim template.Name == ''")
return errors.BadRequest(err, errors.WithMsg("模板名称不能为空"))
return errors.BadRequest("模板名称不能为空")
}

if strings.TrimSpace(template.Email.Subject) == "" {
err := errors.New("trim template.Email.Subject == ''")
return errors.BadRequest(err, errors.WithMsg("模板邮件主题不能为空"))
return errors.BadRequest("模板邮件主题不能为空")
}

if !template.Email.ContentType.Valid() {
err := fmt.Errorf("template.Email.ContentType %d not valid", template.Email.ContentType)
return errors.BadRequest(err, errors.WithMsg("模板邮件内容类型无效"))
return errors.BadRequest("模板邮件内容类型 %d 无效", template.Email.ContentType)
}

return nil
Expand All @@ -81,33 +76,27 @@ func (dts *defaultTemplateService) CreateTemplate(ctx context.Context, spaceID i

func (dts *defaultTemplateService) checkUpdateTemplateParams(template *model.Template) error {
if template.ID <= 0 {
err := errors.New("template.ID <= 0")
return errors.BadRequest(err, errors.WithMsg("模板编号非法"))
return errors.BadRequest("模板编号需要大于 0")
}

if template.AccountID <= 0 {
err := errors.New("template.AccountID <= 0")
return errors.BadRequest(err, errors.WithMsg("模板绑定的账号编号非法"))
return errors.BadRequest("模板绑定的账号编号需要大于 0")
}

if strings.TrimSpace(template.Name) == "" {
err := errors.New("trim template.Name == ''")
return errors.BadRequest(err, errors.WithMsg("模板名称不能为空"))
return errors.BadRequest("模板名称不能为空")
}

if strings.TrimSpace(template.Email.Subject) == "" {
err := errors.New("trim template.Email.Subject == ''")
return errors.BadRequest(err, errors.WithMsg("模板邮件主题不能为空"))
return errors.BadRequest("模板邮件主题不能为空")
}

if template.Email.ContentType > 0 && !template.Email.ContentType.Valid() {
err := fmt.Errorf("template.Email.Content.Type %d not valid", template.Email.ContentType)
return errors.BadRequest(err, errors.WithMsg("模板邮件内容类型无效"))
return errors.BadRequest("模板邮件内容类型 %d 无效", template.Email.ContentType)
}

if template.State > 0 && !template.State.Valid() {
err := fmt.Errorf("template.State %d not valid", template.State)
return errors.BadRequest(err, errors.WithMsg("模板状态无效"))
return errors.BadRequest("模板状态 %d 无效", template.State)
}

return nil
Expand All @@ -130,8 +119,7 @@ func (dts *defaultTemplateService) UpdateTemplate(ctx context.Context, spaceID i

func (dts *defaultTemplateService) checkGetTemplateParams(templateID int64) error {
if templateID <= 0 {
err := fmt.Errorf("templateID %d <= 0", templateID)
return errors.BadRequest(err, errors.WithMsg("模板编号非法"))
return errors.BadRequest("模板编号需要大于 0")
}

return nil
Expand All @@ -150,23 +138,19 @@ func (dts *defaultTemplateService) GetTemplate(ctx context.Context, spaceID int3

func (dts *defaultTemplateService) checkListTemplatesParams(pageSize int32, filter *model.ListTemplatesFilter) error {
if pageSize < minPageSize || pageSize > maxPageSize {
err := fmt.Errorf("pageSize %d not in [%d, %d]", pageSize, minPageSize, maxPageSize)
return errors.BadRequest(err, errors.WithMsg("分页大小需要位于区间 [%d, %d] 内", minPageSize, maxPageSize))
return errors.BadRequest("分页大小 %d 需要位于区间 [%d, %d] 内", pageSize, minPageSize, maxPageSize)
}

if filter.AccountID < 0 {
err := fmt.Errorf("filter.AccountID %d < 0", filter.AccountID)
return errors.BadRequest(err, errors.WithMsg("过滤的账号编号非法"))
return errors.BadRequest("账号编号不能为负数")
}

if filter.TemplateID < 0 {
err := fmt.Errorf("filter.TemplateID %d < 0", filter.TemplateID)
return errors.BadRequest(err, errors.WithMsg("过滤的模板编号非法"))
return errors.BadRequest("模板编号不能为负数")
}

if filter.TemplateState > 0 && !filter.TemplateState.Valid() {
err := fmt.Errorf("filter.TemplateState %d not valid", filter.TemplateState)
return errors.BadRequest(err, errors.WithMsg("过滤的模板状态非法"))
return errors.BadRequest("模板状态 %d 无效", filter.TemplateState)
}

return nil
Expand Down Expand Up @@ -198,8 +182,7 @@ func (dts *defaultTemplateService) ListTemplates(ctx context.Context, spaceID in

func (dts *defaultTemplateService) checkDeleteTemplateParams(templateID int64) error {
if templateID <= 0 {
err := fmt.Errorf("templateID %d <= 0", templateID)
return errors.BadRequest(err, errors.WithMsg("模板编号非法"))
return errors.BadRequest("模板编号需要大于 0")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/postar-admin/store/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (as *AccountStore) newAccount(row *stdsql.Row) (*model.Account, error) {

err := row.Scan(&account.ID, &account.Host, &account.Port, &account.Username, &account.Password, &account.SMTPAuth, &account.State, &account.CreateTime, &account.UpdateTime)
if err == stdsql.ErrNoRows {
return nil, errors.NotFound(err, errors.WithMsg("账号不存在"))
return nil, errors.NotFound("账号不存在").With(err)
}

return account, err
Expand Down
2 changes: 1 addition & 1 deletion internal/postar-admin/store/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (ss *SpaceStore) newSpace(row *stdsql.Row) (*model.Space, error) {

err := row.Scan(&space.ID, &space.Name, &space.Token, &space.State, &space.CreateTime, &space.UpdateTime)
if err == stdsql.ErrNoRows {
return nil, errors.NotFound(err, errors.WithMsg("业务空间不存在"))
return nil, errors.NotFound("业务空间不存在").With(err)
}

return space, err
Expand Down
2 changes: 1 addition & 1 deletion internal/postar-admin/store/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (ts *TemplateStore) newTemplate(row *stdsql.Row) (*model.Template, error) {

err := row.Scan(&template.ID, &template.AccountID, &template.Name, &template.Description, &template.Email.Subject, &to, &cc, &bcc, &template.Email.ContentType, &template.Email.Content, &template.State, &template.CreateTime, &template.UpdateTime)
if err == stdsql.ErrNoRows {
return nil, errors.NotFound(err, errors.WithMsg("模板不存在"))
return nil, errors.NotFound("模板不存在").With(err)
}

template.Email.To = decodeStrings(to)
Expand Down
Loading

0 comments on commit e3d99ca

Please sign in to comment.