Skip to content

Commit

Permalink
refactor(context): change error handling from panic to wrapped errors
Browse files Browse the repository at this point in the history
BREAKING CHANGE: change error handling from panic to wrapped errors
  • Loading branch information
bastean committed May 8, 2024
1 parent 55c5de3 commit ec3245c
Show file tree
Hide file tree
Showing 95 changed files with 1,286 additions and 663 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
require (
github.com/JGLTechnologies/gin-rate-limit v1.5.4
github.com/a-h/templ v0.2.663
github.com/brianvoe/gofakeit/v6 v6.28.0
github.com/brianvoe/gofakeit/v7 v7.0.2
github.com/cucumber/godog v0.14.1
github.com/gin-contrib/secure v1.0.0
github.com/gin-gonic/gin v1.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/JGLTechnologies/gin-rate-limit v1.5.4 h1:1hIaXIdGM9MZFZlXgjWJLpxaK0WH
github.com/JGLTechnologies/gin-rate-limit v1.5.4/go.mod h1:mGEhNzlHEg/Tk+KH/mKylZLTfDjACnx7MVYaAlj07eU=
github.com/a-h/templ v0.2.663 h1:aa0WMm27InkYHGjimcM7us6hJ6BLhg98ZbfaiDPyjHE=
github.com/a-h/templ v0.2.663/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8=
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
github.com/brianvoe/gofakeit/v7 v7.0.2 h1:jzYT7Ge3RDHw7J1CM1kwu0OQywV9vbf2qSGxBS72TCY=
github.com/brianvoe/gofakeit/v7 v7.0.2/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@ import (
"github.com/bastean/codexgo/pkg/context/shared/domain/service/mother"
)

var RegisteredSucceededEventRoutingKey = message.NewMessageRoutingKey(&message.MessageRoutingKey{Module: "user", Version: "1", Type: message.Event, Aggregate: "user", Event: "registered", Status: message.Succeeded})
var RegisteredSucceededEventTypeRoutingKey = message.NewRoutingKey(&message.MessageRoutingKey{
Module: "user",
Version: "1",
Type: message.Type.Event,
Aggregate: "user",
Event: "registered",
Status: message.Status.Succeeded,
})

func Random() *message.Message {
id := mother.Create.UUID()
email := mother.Create.Email()
username := mother.Create.Username()

attributes := sendMail.NewRegisteredSucceededEventAttributes(id, email, username)

attributesJson, err := json.Marshal(attributes)

if err != nil {
panic(err)
attributes := sendMail.RegisteredSucceededEventAttributes{
Id: id,
Email: email,
Username: username,
}

return message.NewMessage(RegisteredSucceededEventRoutingKey, attributesJson, []byte{})
attributesJson, _ := json.Marshal(attributes)

return message.NewMessage(RegisteredSucceededEventTypeRoutingKey, attributesJson, []byte{})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,53 @@ package sendMail
import (
"encoding/json"

"github.com/bastean/codexgo/pkg/context/notify/domain/model"
"github.com/bastean/codexgo/pkg/context/notify/domain/template"
"github.com/bastean/codexgo/pkg/context/shared/domain/errs"
"github.com/bastean/codexgo/pkg/context/shared/domain/message"
"github.com/bastean/codexgo/pkg/context/shared/domain/model"
sharedModel "github.com/bastean/codexgo/pkg/context/shared/domain/model"
"github.com/bastean/codexgo/pkg/context/shared/domain/queue"
"github.com/bastean/codexgo/pkg/context/shared/domain/types"
)

type RegisteredSucceededEventConsumer struct {
*SendMail
sharedModel.UseCase[model.MailTemplate, *types.Empty]
Queues []*queue.Queue
}

func (consumer *RegisteredSucceededEventConsumer) SubscribedTo() []*queue.Queue {
return consumer.Queues
}

func (consumer *RegisteredSucceededEventConsumer) On(message *message.Message) {
func (consumer *RegisteredSucceededEventConsumer) On(message *message.Message) error {
attributes := new(RegisteredSucceededEventAttributes)

json.Unmarshal(message.Attributes, attributes)
err := json.Unmarshal(message.Attributes, attributes)

if err != nil {
return errs.NewFailedError(&errs.Bubble{
Where: "On",
What: "failed getting message attributes",
Why: errs.Meta{
"Message": message.Id,
},
Who: err,
})
}

mailTemplate := template.NewMail([]string{attributes.Email})
accountConfirmationTemplate := template.NewAccountConfirmationMail(mailTemplate, attributes.Username, attributes.Id)
accountConfirmationTemplate := template.AccountConfirmationMail{
Mail: &template.Mail{
To: []string{attributes.Email},
},
Username: attributes.Username,
ConfirmationLink: attributes.Id,
}

consumer.SendMail.Run(accountConfirmationTemplate)
}
_, err = consumer.UseCase.Run(accountConfirmationTemplate)

func NewRegisteredSucceededEventConsumer(sendEmail *SendMail, queues []*queue.Queue) model.Consumer {
return &RegisteredSucceededEventConsumer{
SendMail: sendEmail,
Queues: queues,
if err != nil {
return errs.BubbleUp("On", err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ type RegisteredSucceededEventConsumerTestSuite struct {
sut model.Consumer
sendMail *sendMail.SendMail
mail *communicationMock.MailMock
queue []*queue.Queue
queues []*queue.Queue
}

func (suite *RegisteredSucceededEventConsumerTestSuite) SetupTest() {
queueName := queue.NewQueueName(&queue.QueueName{Module: "queue", Action: "assert", Event: "test.succeeded"})
suite.queue = append(suite.queue, queue.NewQueue(queueName))
suite.mail = communicationMock.NewMailMock()
suite.sendMail = sendMail.NewSendMail(suite.mail)
suite.sut = sendMail.NewRegisteredSucceededEventConsumer(suite.sendMail, suite.queue)
queueName := queue.NewQueueName(&queue.QueueName{
Module: "queue",
Action: "assert",
Event: "test.succeeded",
})
suite.queues = append(suite.queues, &queue.Queue{Name: queueName})
suite.mail = new(communicationMock.MailMock)
suite.sendMail = &sendMail.SendMail{Mail: suite.mail}
suite.sut = &sendMail.RegisteredSucceededEventConsumer{
UseCase: suite.sendMail,
Queues: suite.queues,
}
}

func (suite *RegisteredSucceededEventConsumerTestSuite) TestEventConsumer() {
Expand All @@ -36,13 +43,17 @@ func (suite *RegisteredSucceededEventConsumerTestSuite) TestEventConsumer() {

json.Unmarshal(message.Attributes, attributes)

mailTemplate := template.NewMail([]string{attributes.Email})

accountConfirmationTemplate := template.NewAccountConfirmationMail(mailTemplate, attributes.Username, attributes.Id)
accountConfirmationTemplate := &template.AccountConfirmationMail{
Mail: &template.Mail{
To: []string{attributes.Email},
},
Username: attributes.Username,
ConfirmationLink: attributes.Id,
}

suite.mail.On("Send", accountConfirmationTemplate)

suite.sut.On(message)
suite.NoError(suite.sut.On(message))

suite.mail.AssertExpectations(suite.T())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,3 @@ type RegisteredSucceededEventAttributes struct {
Email string
Username string
}

func NewRegisteredSucceededEventAttributes(id, email, username string) *RegisteredSucceededEventAttributes {
return &RegisteredSucceededEventAttributes{
Id: id,
Email: email,
Username: username,
}
}
14 changes: 8 additions & 6 deletions pkg/context/notify/application/sendMail/sendMail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package sendMail

import (
"github.com/bastean/codexgo/pkg/context/notify/domain/model"
"github.com/bastean/codexgo/pkg/context/shared/domain/errs"
"github.com/bastean/codexgo/pkg/context/shared/domain/types"
)

type SendMail struct {
model.Mail
}

func (sendMail *SendMail) Run(mail model.MailTemplate) {
sendMail.Mail.Send(mail)
}
func (sendMail *SendMail) Run(mail model.MailTemplate) (*types.Empty, error) {
err := sendMail.Mail.Send(mail)

func NewSendMail(mail model.Mail) *SendMail {
return &SendMail{
Mail: mail,
if err != nil {
return nil, errs.BubbleUp("Run", err)
}

return nil, nil
}
2 changes: 1 addition & 1 deletion pkg/context/notify/domain/model/mail.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package model

type Mail interface {
Send(template MailTemplate)
Send(template MailTemplate) error
}
12 changes: 0 additions & 12 deletions pkg/context/notify/domain/template/account.confirmation.mail.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package template

import (
"github.com/bastean/codexgo/pkg/context/notify/domain/model"
)

type AccountConfirmationMail struct {
*Mail
Username string
ConfirmationLink string
}

func NewAccountConfirmationMail(mail *Mail, username, confirmationLink string) model.MailTemplate {
return &AccountConfirmationMail{
Mail: mail,
Username: username,
ConfirmationLink: confirmationLink,
}
}
6 changes: 0 additions & 6 deletions pkg/context/notify/domain/template/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@ package template
type Mail struct {
To []string
}

func NewMail(to []string) *Mail {
return &Mail{
To: to,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ type MailMock struct {
mock.Mock
}

func (m *MailMock) Send(template model.MailTemplate) {
m.Called(template)
}

func NewMailMock() *MailMock {
return new(MailMock)
func (mail *MailMock) Send(template model.MailTemplate) error {
args := mail.Called(template)
return args.Get(0).(error)
}
34 changes: 29 additions & 5 deletions pkg/context/notify/infrastructure/communication/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/bastean/codexgo/pkg/context/notify/domain/model"
"github.com/bastean/codexgo/pkg/context/notify/domain/template"
"github.com/bastean/codexgo/pkg/context/shared/domain/errs"
)

type Smtp struct {
Expand All @@ -17,18 +18,39 @@ type Smtp struct {
ServerUrl string
}

func (client *Smtp) Send(mailTemplate model.MailTemplate) {
func (client *Smtp) Send(mailTemplate model.MailTemplate) error {
var err error

switch mail := mailTemplate.(type) {
case *template.AccountConfirmationMail:
client.SendAccountConfirmation(mail)
err = client.SendAccountConfirmation(mail)
}

if err != nil {
return errs.BubbleUp("Send", err)
}

return nil
}

func (client *Smtp) SendMail(to []string, message []byte) error {
return smtp.SendMail(client.SmtpUrl, client.Auth, client.Username, to, message)
err := smtp.SendMail(client.SmtpUrl, client.Auth, client.Username, to, message)

if err != nil {
return errs.NewFailedError(&errs.Bubble{
Where: "SendMail",
What: "failed to send a mail",
Why: errs.Meta{
"SmtpUrl": client.SmtpUrl,
},
Who: err,
})
}

return nil
}

func (client *Smtp) SendAccountConfirmation(mail *template.AccountConfirmationMail) {
func (client *Smtp) SendAccountConfirmation(mail *template.AccountConfirmationMail) error {
var message bytes.Buffer

// TODO!: mail.To[0]
Expand All @@ -43,8 +65,10 @@ func (client *Smtp) SendAccountConfirmation(mail *template.AccountConfirmationMa
err := client.SendMail(mail.To, message.Bytes())

if err != nil {
panic(err)
return errs.BubbleUp("SendAccountConfirmation", err)
}

return nil
}

func NewNotifySmtpMail(host, port, username, password, serverUrl string) model.Mail {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ func (suite *SmtpMailTestSuite) SetupTest() {
}

func (suite *SmtpMailTestSuite) TestSendAccountConfirmation() {
mailTemplate := template.NewMail([]string{suite.username})

accountConfirmationTemplate := template.NewAccountConfirmationMail(mailTemplate, suite.username, "test-send-account-confirmation-success")

suite.NotPanics(func() { suite.sut.Send(accountConfirmationTemplate) })
accountConfirmationTemplate := &template.AccountConfirmationMail{
Mail: &template.Mail{
To: []string{suite.username},
},
Username: suite.username,
ConfirmationLink: "test-send-account-confirmation-success",
}

suite.NoError(suite.sut.Send(accountConfirmationTemplate))
}

func TestIntegrationSmtpMailSuite(t *testing.T) {
Expand Down
15 changes: 0 additions & 15 deletions pkg/context/shared/domain/errors/alreadyExist.go

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/context/shared/domain/errors/failed.go

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/context/shared/domain/errors/invalidValue.go

This file was deleted.

Loading

0 comments on commit ec3245c

Please sign in to comment.