try new errors
All checks were successful
Build and Publish / Metadata (push) Successful in 9s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m27s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m0s

This commit is contained in:
matst80
2025-10-14 15:52:59 +02:00
parent b029a9d05a
commit 9f83717ea9
2 changed files with 34 additions and 7 deletions

View File

@@ -4,24 +4,37 @@ import (
"fmt" "fmt"
"slices" "slices"
"git.tornberg.me/go-cart-actor/pkg/actor"
"git.tornberg.me/go-cart-actor/pkg/messages" "git.tornberg.me/go-cart-actor/pkg/messages"
"git.tornberg.me/go-cart-actor/pkg/voucher" "git.tornberg.me/go-cart-actor/pkg/voucher"
) )
func (ctx *MutationContext) AddVoucher(g *CartGrain, m *messages.AddVoucher) error { func (ctx *MutationContext) AddVoucher(g *CartGrain, m *messages.AddVoucher) error {
if m == nil { if m == nil {
return fmt.Errorf("AddVoucher: nil payload") return &actor.MutationError{
Message: "AddVoucher: nil payload",
Code: 1001,
StatusCode: 400,
}
} }
if slices.ContainsFunc(g.Vouchers, func(v *voucher.Voucher) bool { if slices.ContainsFunc(g.Vouchers, func(v *voucher.Voucher) bool {
return v.Code == m.Code return v.Code == m.Code
}) { }) {
return fmt.Errorf("voucher already applied") return &actor.MutationError{
Message: "voucher already applied",
Code: 1002,
StatusCode: 400,
}
} }
voucherData, err := ctx.VoucherService.GetVoucher(m.Code) voucherData, err := ctx.VoucherService.GetVoucher(m.Code)
if err != nil { if err != nil {
return fmt.Errorf("cant find voucher: %w", err) return &actor.MutationError{
Message: fmt.Sprintf("cant find voucher: %v", err),
Code: 1003,
StatusCode: 400,
}
} }
if voucherData == nil { if voucherData == nil {

View File

@@ -12,7 +12,7 @@ import (
type ApplyResult struct { type ApplyResult struct {
Type string `json:"type"` Type string `json:"type"`
Mutation proto.Message `json:"mutation"` Mutation proto.Message `json:"mutation"`
Error string `json:"error,omitempty"` Error error `json:"error,omitempty"`
} }
type MutationRegistry interface { type MutationRegistry interface {
@@ -30,9 +30,23 @@ type ProtoMutationRegistry struct {
} }
var ( var (
ErrMutationNotRegistered = fmt.Errorf("mutation not registered") ErrMutationNotRegistered = &MutationError{
Message: "mutation not registered",
Code: 255,
StatusCode: 500,
}
) )
type MutationError struct {
Message string `json:"message"`
Code uint32 `json:"code"`
StatusCode uint32 `json:"status_code"`
}
func (m MutationError) Error() string {
return m.Message
}
// MutationOption configures additional behavior for a registered mutation. // MutationOption configures additional behavior for a registered mutation.
type MutationOption func(*mutationOptions) type MutationOption func(*mutationOptions)
@@ -167,11 +181,11 @@ func (r *ProtoMutationRegistry) Apply(grain any, msg ...proto.Message) ([]ApplyR
entry, ok := r.mutationRegistry[rt] entry, ok := r.mutationRegistry[rt]
r.mutationRegistryMu.RUnlock() r.mutationRegistryMu.RUnlock()
if !ok { if !ok {
results = append(results, ApplyResult{Error: ErrMutationNotRegistered.Error(), Type: rt.Name(), Mutation: m}) results = append(results, ApplyResult{Error: ErrMutationNotRegistered, Type: rt.Name(), Mutation: m})
continue continue
} }
err := entry.Handle(grain, m) err := entry.Handle(grain, m)
results = append(results, ApplyResult{Error: err.Error(), Type: rt.Name(), Mutation: m}) results = append(results, ApplyResult{Error: err, Type: rt.Name(), Mutation: m})
} }
// if entry.updateTotals { // if entry.updateTotals {