better message formatting
Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
This commit is contained in:
@@ -3,6 +3,7 @@ package merrors
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
@@ -27,8 +28,8 @@ func Internal(msg string) error {
|
||||
|
||||
var ErrInvalidArg = errors.New("invalidArgError")
|
||||
|
||||
func InvalidArgument(msg string) error {
|
||||
return fmt.Errorf("%w: %s", ErrInvalidArg, msg)
|
||||
func InvalidArgument(msg string, argumentNames ...string) error {
|
||||
return fmt.Errorf("%w: %s", ErrInvalidArg, invalidArgumentMessage(msg, argumentNames...))
|
||||
}
|
||||
|
||||
var ErrDataConflict = errors.New("DataConflict")
|
||||
@@ -64,8 +65,8 @@ func NoMessagingTopic(topic string) error {
|
||||
return fmt.Errorf("%w: messaging topic '%s' not found", ErrNoMessagingTopic, topic)
|
||||
}
|
||||
|
||||
func InvalidArgumentWrap(err error, msg string) error {
|
||||
return wrapError(ErrInvalidArg, msg, err)
|
||||
func InvalidArgumentWrap(err error, msg string, argumentNames ...string) error {
|
||||
return wrapError(ErrInvalidArg, invalidArgumentMessage(msg, argumentNames...), err)
|
||||
}
|
||||
|
||||
func InternalWrap(err error, msg string) error {
|
||||
@@ -79,3 +80,23 @@ func wrapError(base error, msg string, err error) error {
|
||||
}
|
||||
return errors.Join(baseErr, err)
|
||||
}
|
||||
|
||||
func invalidArgumentMessage(msg string, argumentNames ...string) string {
|
||||
names := make([]string, 0, len(argumentNames))
|
||||
for _, name := range argumentNames {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
names = append(names, fmt.Sprintf("%q", name))
|
||||
}
|
||||
if len(names) == 0 {
|
||||
return msg
|
||||
}
|
||||
|
||||
prefix := "broken argument"
|
||||
if len(names) > 1 {
|
||||
prefix = "broken arguments"
|
||||
}
|
||||
return fmt.Sprintf("%s %s: %s", prefix, strings.Join(names, ", "), msg)
|
||||
}
|
||||
|
||||
47
api/pkg/merrors/errors_test.go
Normal file
47
api/pkg/merrors/errors_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package merrors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInvalidArgumentSupportsBrokenArgumentName(t *testing.T) {
|
||||
t.Run("without argument name keeps old behavior", func(t *testing.T) {
|
||||
err := InvalidArgument("value is missing")
|
||||
expected := "invalidArgError: value is missing"
|
||||
if err.Error() != expected {
|
||||
t.Fatalf("unexpected error message: %s", err)
|
||||
}
|
||||
if !errors.Is(err, ErrInvalidArg) {
|
||||
t.Fatalf("error should wrap ErrInvalidArg")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("single argument name", func(t *testing.T) {
|
||||
err := InvalidArgument("value is missing", "bot_token_env")
|
||||
expected := `invalidArgError: broken argument "bot_token_env": value is missing`
|
||||
if err.Error() != expected {
|
||||
t.Fatalf("unexpected error message: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("multiple argument names", func(t *testing.T) {
|
||||
err := InvalidArgument("value mismatch", "bot_token_env", "chat_id_env", " ")
|
||||
expected := `invalidArgError: broken arguments "bot_token_env", "chat_id_env": value mismatch`
|
||||
if err.Error() != expected {
|
||||
t.Fatalf("unexpected error message: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestInvalidArgumentWrapSupportsBrokenArgumentName(t *testing.T) {
|
||||
base := errors.New("root cause")
|
||||
err := InvalidArgumentWrap(base, "value is missing", "bot_token_env")
|
||||
if !strings.Contains(err.Error(), `invalidArgError: broken argument "bot_token_env": value is missing`) {
|
||||
t.Fatalf("wrapped error should include broken argument name: %s", err)
|
||||
}
|
||||
if !errors.Is(err, ErrInvalidArg) || !errors.Is(err, base) {
|
||||
t.Fatalf("wrapped error should preserve all error layers")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user