Skip to content

Commit 7254210

Browse files
refactor: model errors
1 parent 42448f5 commit 7254210

File tree

11 files changed

+146
-102
lines changed

11 files changed

+146
-102
lines changed

cmd/stress-database/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141
for {
4242
id, err := repo.MailmanDequeue(ctx, db)
4343
if err != nil {
44-
if !errors.Is(err, repo.ErrNoRows) {
44+
if !errors.Is(err, models.ErrNotFound) {
4545
log.Fatalln(err)
4646
}
4747

internal/app/app.go

+52-50
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,44 @@ type App struct {
3838
webTestFileStore WebTestFileStore
3939
}
4040

41-
// DatabaseVacuum implements core.App.
41+
func New(
42+
db database.Querier,
43+
fileStore FileStore,
44+
bus core.Bus,
45+
config *models.Config,
46+
endpointFactory endpoint.Factory,
47+
webTestFileStore WebTestFileStore,
48+
) (App, func()) {
49+
a := App{
50+
db: db,
51+
fileStore: fileStore,
52+
bus: bus,
53+
config: config,
54+
endpointFactory: endpointFactory,
55+
webTestFileStore: webTestFileStore,
56+
}
57+
58+
return a, a.init()
59+
}
60+
61+
func (a App) init() func() {
62+
return closers(
63+
a.bus.OnEnvelopeCreated(func(ctx context.Context, evt models.EventEnvelopeCreated) error {
64+
return a.MailmanEnqueue(ctx, evt.ID)
65+
}),
66+
a.bus.OnEnvelopeDeleted(func(ctx context.Context, evt models.EventEnvelopeDeleted) error {
67+
return a.AttachmentOrphanDelete(ctx, a.Tracer(trace.SourceApp))
68+
}),
69+
)
70+
}
71+
4272
func (a App) DatabaseVacuum(ctx context.Context) error {
4373
return repo.Vacuum(ctx, a.db)
4474
}
4575

4676
func (a App) RuleEndpointsGet(ctx context.Context, id int64) (models.RuleEndpoints, error) {
47-
return repo.RuleEndpointsGet(ctx, a.db, id)
77+
res, err := repo.RuleEndpointsGet(ctx, a.db, id)
78+
return res, checkErr(err)
4879
}
4980

5081
func (a App) EnvelopeCount(ctx context.Context) (int, error) {
@@ -54,15 +85,15 @@ func (a App) EnvelopeCount(ctx context.Context) (int, error) {
5485
func (a App) RuleDelete(ctx context.Context, id int64) error {
5586
r, err := repo.RuleGet(ctx, a.db, id)
5687
if err != nil {
57-
return err
88+
return checkErr(err)
5889
}
5990

6091
err = rule.Delete(r)
6192
if err != nil {
6293
return err
6394
}
6495

65-
return repo.RuleDelete(ctx, a.db, id)
96+
return checkErr(repo.RuleDelete(ctx, a.db, id))
6697
}
6798

6899
func (App) RuleExpressionCheck(ctx context.Context, expression string) error {
@@ -80,18 +111,19 @@ func (App) RuleExpressionCheck(ctx context.Context, expression string) error {
80111
}
81112

82113
func (a App) AttachmentGet(ctx context.Context, id int64) (models.Attachment, error) {
83-
return repo.AttachmentGet(ctx, a.db, id)
114+
res, err := repo.AttachmentGet(ctx, a.db, id)
115+
return res, checkErr(err)
84116
}
85117

86118
func (a App) EnvelopeSend(ctx context.Context, envelopeID int64, endpointID int64) error {
87119
env, err := repo.EnvelopeGet(ctx, a.db, envelopeID)
88120
if err != nil {
89-
return err
121+
return checkErr(err)
90122
}
91123

92124
endModel, err := repo.EndpointGet(ctx, a.db, endpointID)
93125
if err != nil {
94-
return err
126+
return checkErr(err)
95127
}
96128

97129
end, err := a.endpointFactory.Build(endModel)
@@ -102,39 +134,6 @@ func (a App) EnvelopeSend(ctx context.Context, envelopeID int64, endpointID int6
102134
return end.Send(ctx, a.fileStore, env)
103135
}
104136

105-
func New(
106-
db database.Querier,
107-
fileStore FileStore,
108-
bus core.Bus,
109-
config *models.Config,
110-
endpointFactory endpoint.Factory,
111-
webTestFileStore WebTestFileStore,
112-
) (App, func()) {
113-
a := App{
114-
db: db,
115-
fileStore: fileStore,
116-
bus: bus,
117-
config: config,
118-
endpointFactory: endpointFactory,
119-
webTestFileStore: webTestFileStore,
120-
}
121-
122-
return a, a.init()
123-
}
124-
125-
func (a App) init() func() {
126-
return closers(
127-
a.bus.OnEnvelopeCreated(func(ctx context.Context, evt models.EventEnvelopeCreated) error {
128-
return a.MailmanEnqueue(ctx, evt.ID)
129-
}),
130-
a.bus.OnEnvelopeDeleted(func(ctx context.Context, evt models.EventEnvelopeDeleted) error {
131-
return a.AttachmentOrphanDelete(ctx, a.Tracer(trace.SourceApp))
132-
}),
133-
)
134-
}
135-
136-
var ErrorLogin = fmt.Errorf("login invalid")
137-
138137
func (a App) AuthSMTPAnonymous() bool {
139138
return a.config.AuthSMTP.Anonymous
140139
}
@@ -149,15 +148,15 @@ func (a App) AuthHTTPLogin(ctx context.Context, username, password string) (mode
149148
return models.User{}, nil
150149
}
151150

152-
return models.User{}, ErrorLogin
151+
return models.User{}, models.ErrAuthInvalid
153152
}
154153

155154
func (a App) AuthSMTPLogin(ctx context.Context, username, password string) error {
156155
if a.config.AuthSMTP.Anonymous || auth.Check(a.config.AuthSMTP, username, password) {
157156
return nil
158157
}
159158

160-
return ErrorLogin
159+
return models.ErrAuthInvalid
161160
}
162161

163162
func (a App) EnvelopeCreate(ctx context.Context, dtoMsg models.DTOMessageCreate, dtoDatts []models.DTOAttachmentCreate) (int64, error) {
@@ -202,7 +201,7 @@ func (a App) EnvelopeCreate(ctx context.Context, dtoMsg models.DTOMessageCreate,
202201

203202
func (a App) EnvelopeDelete(ctx context.Context, id int64) error {
204203
if err := repo.EnvelopeDelete(ctx, a.db, id); err != nil {
205-
return err
204+
return checkErr(err)
206205
}
207206

208207
a.bus.EnvelopeDeleted(ctx)
@@ -211,7 +210,8 @@ func (a App) EnvelopeDelete(ctx context.Context, id int64) error {
211210
}
212211

213212
func (a App) EnvelopeGet(ctx context.Context, id int64) (models.Envelope, error) {
214-
return repo.EnvelopeGet(ctx, a.db, id)
213+
res, err := repo.EnvelopeGet(ctx, a.db, id)
214+
return res, checkErr(err)
215215
}
216216

217217
func (a App) EnvelopeList(ctx context.Context, page pagination.Page, req models.DTOEnvelopeListRequest) (models.DTOEnvelopeListResult, error) {
@@ -232,7 +232,8 @@ func (a App) EnvelopeDrop(ctx context.Context) error {
232232
}
233233

234234
func (a App) MessageHTMLGet(ctx context.Context, id int64) (string, error) {
235-
return repo.MessageHTMLGet(ctx, a.db, id)
235+
res, err := repo.MessageHTMLGet(ctx, a.db, id)
236+
return res, checkErr(err)
236237
}
237238

238239
func (a App) AttachmentList(ctx context.Context, page pagination.Page, req models.DTOAttachmentListRequest) (models.DTOAttachmentListResult, error) {
@@ -284,7 +285,7 @@ func (a App) RuleCreate(ctx context.Context, req models.DTORuleCreate) (int64, e
284285
func (a App) RuleUpdate(ctx context.Context, req models.DTORuleUpdate) error {
285286
r, err := repo.RuleGet(ctx, a.db, req.ID)
286287
if err != nil {
287-
return err
288+
return checkErr(err)
288289
}
289290

290291
r, err = rule.Update(r, req)
@@ -294,21 +295,22 @@ func (a App) RuleUpdate(ctx context.Context, req models.DTORuleUpdate) error {
294295

295296
err = repo.RuleUpdate(ctx, a.db, r)
296297
if err != nil {
297-
return err
298+
return checkErr(err)
298299
}
299300

300301
if req.Endpoints != nil {
301302
err := repo.RuleEndpointsSet(ctx, a.db, r.ID, *req.Endpoints)
302303
if err != nil {
303-
return err
304+
return checkErr(err)
304305
}
305306
}
306307

307308
return nil
308309
}
309310

310311
func (a App) RuleGet(ctx context.Context, id int64) (models.Rule, error) {
311-
return repo.RuleGet(ctx, a.db, id)
312+
res, err := repo.RuleGet(ctx, a.db, id)
313+
return res, checkErr(err)
312314
}
313315

314316
func (a App) RuleList(ctx context.Context) ([]models.Rule, error) {
@@ -322,7 +324,7 @@ func (a App) RuleEndpointsList(ctx context.Context) ([]models.RuleEndpoints, err
322324
func (a App) EndpointTest(ctx context.Context, id int64) error {
323325
e, err := repo.EndpointGet(ctx, a.db, id)
324326
if err != nil {
325-
return err
327+
return checkErr(err)
326328
}
327329

328330
end, err := a.endpointFactory.Build(e)

internal/app/error.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package app
2+
3+
import (
4+
"errors"
5+
6+
"github.com/ItsNotGoodName/smtpbridge/internal/models"
7+
"github.com/ItsNotGoodName/smtpbridge/internal/repo"
8+
)
9+
10+
func checkErr(err error) error {
11+
if err == nil {
12+
return nil
13+
}
14+
if errors.Is(err, repo.ErrNoRows) {
15+
return models.ErrNotFound
16+
}
17+
18+
return err
19+
}

internal/models/error.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package models
2+
3+
import "fmt"
4+
5+
type ErrorField struct {
6+
Field string
7+
Message string
8+
}
9+
10+
var (
11+
ErrNotFound = fmt.Errorf("not found")
12+
ErrAuthInvalid = fmt.Errorf("auth invalid")
13+
)

internal/repo/database.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package repo
2+
3+
import (
4+
"context"
5+
6+
"github.com/ItsNotGoodName/smtpbridge/internal/database"
7+
. "github.com/go-jet/jet/v2/sqlite"
8+
)
9+
10+
func Size(ctx context.Context, db database.Querier) (int64, error) {
11+
var res struct{ Size int64 }
12+
err := RawStatement("SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();").
13+
QueryContext(ctx, db, &res)
14+
return res.Size, err
15+
}
16+
17+
func Vacuum(ctx context.Context, db database.Querier) error {
18+
_, err := RawStatement("VACUUM;").
19+
ExecContext(ctx, db)
20+
return err
21+
}

internal/repo/endpoint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ func EndpointDelete(ctx context.Context, db database.Querier, id int64) error {
4848
if err != nil {
4949
return err
5050
}
51-
return oneRowAffected(res)
51+
return one(res)
5252
}

internal/repo/envelope.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func EnvelopeDelete(ctx context.Context, db database.Querier, id int64) error {
182182
if err != nil {
183183
return err
184184
}
185-
return oneRowAffected(res)
185+
return one(res)
186186
}
187187

188188
func EnvelopeDrop(ctx context.Context, db database.Querier) (int64, error) {
@@ -198,16 +198,16 @@ func EnvelopeDrop(ctx context.Context, db database.Querier) (int64, error) {
198198
}
199199

200200
func EnvelopeTrim(ctx context.Context, db database.Querier, age time.Time, keep int) (int64, error) {
201-
q := Messages.CreatedAt.LT(RawTimestamp(muhTypeAffinity(models.NewTime(age))))
201+
query := Messages.CreatedAt.LT(RawTimestamp(muhTypeAffinity(models.NewTime(age))))
202202
if keep != 0 {
203-
q = q.AND(
203+
query = query.AND(
204204
Messages.ID.NOT_IN(Messages.SELECT(Messages.ID).ORDER_BY(Messages.ID.DESC()).LIMIT(int64(keep))),
205205
)
206206
}
207207

208208
res, err := Messages.
209209
DELETE().
210-
WHERE(q).
210+
WHERE(query).
211211
ExecContext(ctx, db)
212212
if err != nil {
213213
return 0, err
@@ -244,32 +244,25 @@ func AttachmentGet(ctx context.Context, db database.Querier, id int64) (models.A
244244
}
245245

246246
func AttachmentList(ctx context.Context, db database.Querier, page pagination.Page, req models.DTOAttachmentListRequest) (models.DTOAttachmentListResult, error) {
247-
withFilter := func(stmt SelectStatement) SelectStatement {
248-
return stmt
249-
}
250-
withOrder := func(stmt SelectStatement) SelectStatement {
251-
if req.Ascending {
252-
return stmt.ORDER_BY(Attachments.ID.ASC())
253-
}
254-
return stmt.ORDER_BY(Attachments.ID.DESC())
255-
}
256-
257247
var res []models.Attachment
258248

259-
query := withOrder(withFilter(Attachments.
249+
query := Attachments.
260250
SELECT(attachmentPJ).
261251
LIMIT(int64(page.Limit())).
262-
OFFSET(int64(page.Offset()))))
252+
OFFSET(int64(page.Offset()))
253+
query = attachmentListWithWhere(query)
254+
query = attachmentListWithOrder(query, req)
263255

264256
err := query.QueryContext(ctx, db, &res)
265257
if err != nil {
266258
return models.DTOAttachmentListResult{}, err
267259
}
268260

261+
countQuery := Attachments.SELECT(COUNT(Raw("*")).AS("count"))
262+
countQuery = attachmentListWithWhere(countQuery)
263+
269264
var resCount struct{ Count int }
270-
err = withFilter(Attachments.
271-
SELECT(COUNT(Raw("*")).AS("count"))).
272-
QueryContext(ctx, db, &resCount)
265+
err = countQuery.QueryContext(ctx, db, &resCount)
273266
if err != nil {
274267
return models.DTOAttachmentListResult{}, err
275268
}
@@ -281,6 +274,17 @@ func AttachmentList(ctx context.Context, db database.Querier, page pagination.Pa
281274
}, nil
282275
}
283276

277+
func attachmentListWithWhere(stmt SelectStatement) SelectStatement {
278+
return stmt
279+
}
280+
281+
func attachmentListWithOrder(stmt SelectStatement, req models.DTOAttachmentListRequest) SelectStatement {
282+
if req.Ascending {
283+
return stmt.ORDER_BY(Attachments.ID.ASC())
284+
}
285+
return stmt.ORDER_BY(Attachments.ID.DESC())
286+
}
287+
284288
func AttachmentListByMessage(ctx context.Context, db database.Querier, messageID int64) ([]models.Attachment, error) {
285289
var res []models.Attachment
286290
err := Attachments.

0 commit comments

Comments
 (0)