chore: refactor structure
This commit is contained in:
@@ -1,133 +0,0 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrDummy int = -1
|
||||
|
||||
ErrStorageFailure int = 1000
|
||||
ErrGameStateInvalid int = 2000
|
||||
|
||||
ErrDeleteShipTypeExistingGroup = 5000
|
||||
ErrDeleteShipTypePlanetProduction = 5001
|
||||
ErrDeleteSciencePlanetProduction = 5002
|
||||
ErrMergeShipTypeNotEqual = 5003
|
||||
)
|
||||
|
||||
const (
|
||||
ErrInputUnknownRace int = 3000 + iota
|
||||
ErrInputEntityTypeNameInvalid
|
||||
ErrInputEntityTypeNameDuplicate
|
||||
ErrInputEntityTypeNameEquality
|
||||
ErrInputEntityNotExists
|
||||
ErrInputEntityNotOwned
|
||||
ErrInputPlanetNumber
|
||||
ErrInputDriveValue
|
||||
ErrInputWeaponsValue
|
||||
ErrInputShieldsValue
|
||||
ErrInputCargoValue
|
||||
ErrInputShipTypeArmamentValue
|
||||
ErrInputShipTypeWeaponsAndArmamentValue
|
||||
ErrInputShipTypeZeroValues
|
||||
ErrInputScienceSumValues
|
||||
ErrInputProductionInvalid
|
||||
)
|
||||
|
||||
func GenericErrorText(code int) string {
|
||||
switch code {
|
||||
case ErrDummy:
|
||||
return "Dummy"
|
||||
case ErrStorageFailure:
|
||||
return "Storage failure"
|
||||
case ErrGameStateInvalid:
|
||||
return "Invalid game state"
|
||||
case ErrInputUnknownRace:
|
||||
return "Race name is unknown to this game"
|
||||
case ErrInputEntityTypeNameInvalid:
|
||||
return "Name has invalid length or symbols"
|
||||
case ErrInputEntityTypeNameDuplicate:
|
||||
return "Name already exists"
|
||||
case ErrInputEntityTypeNameEquality:
|
||||
return "Names should differ"
|
||||
case ErrInputEntityNotExists:
|
||||
return "Entity does not exists"
|
||||
case ErrInputEntityNotOwned:
|
||||
return "Entity is not owned"
|
||||
case ErrInputPlanetNumber:
|
||||
return "Invalid Planet number"
|
||||
case ErrInputDriveValue:
|
||||
return "Invalid Drive value"
|
||||
case ErrInputWeaponsValue:
|
||||
return "Invalid Weapons value"
|
||||
case ErrInputShieldsValue:
|
||||
return "Invalid Shields value"
|
||||
case ErrInputCargoValue:
|
||||
return "Invalid Cargo value"
|
||||
case ErrInputShipTypeArmamentValue:
|
||||
return "Invalid Armament value"
|
||||
case ErrInputShipTypeWeaponsAndArmamentValue:
|
||||
return "Invalid Armament or Weapons value"
|
||||
case ErrInputShipTypeZeroValues:
|
||||
return "Ship type values cannot be all zeros"
|
||||
case ErrDeleteShipTypeExistingGroup:
|
||||
return "Ship type exists in a Group"
|
||||
case ErrDeleteShipTypePlanetProduction:
|
||||
return "Ship type in production on the Planet"
|
||||
case ErrDeleteSciencePlanetProduction:
|
||||
return "Science in production on the Planet"
|
||||
case ErrInputScienceSumValues:
|
||||
return "Science proportions sum should be equal 1"
|
||||
case ErrInputProductionInvalid:
|
||||
return "Invalid Production type"
|
||||
case ErrMergeShipTypeNotEqual:
|
||||
return "Source and target ship types are not the same"
|
||||
default:
|
||||
return fmt.Sprintf("Undescribed error with code %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
type GenericError struct {
|
||||
code int
|
||||
subject string
|
||||
err error
|
||||
}
|
||||
|
||||
func (ge GenericError) Error() string {
|
||||
msg := GenericErrorText(ge.code)
|
||||
if ge.subject != "" {
|
||||
msg += ": " + ge.subject
|
||||
}
|
||||
if ge.err != nil {
|
||||
msg = fmt.Errorf("%s: %w", msg, ge.err).Error()
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func newGenericError(code int, arg ...any) error {
|
||||
e := &GenericError{code: code}
|
||||
if len(arg) > 0 {
|
||||
i := 0
|
||||
switch arg[i].(type) {
|
||||
case error:
|
||||
e.err = arg[i].(error)
|
||||
i += 1
|
||||
}
|
||||
if len(arg) == i+2 {
|
||||
e.subject = fmt.Sprintf(asString(arg[i]), arg[i+1:]...)
|
||||
} else if len(arg) == i+1 {
|
||||
e.subject = asString(arg[i])
|
||||
}
|
||||
}
|
||||
return *e
|
||||
}
|
||||
|
||||
func asString(v any) string {
|
||||
switch s := v.(type) {
|
||||
case string:
|
||||
return s
|
||||
default:
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewGenericError(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
arg []any
|
||||
message string
|
||||
}{
|
||||
{arg: []any{"Foo"}, message: "Dummy: Foo"},
|
||||
{arg: []any{"Foo%s", "Bar"}, message: "Dummy: FooBar"},
|
||||
{arg: []any{errors.New("Error")}, message: "Dummy: Error"},
|
||||
{arg: []any{errors.New("Error"), "Foo"}, message: "Dummy: Foo: Error"},
|
||||
{arg: []any{errors.New("Error"), "Foo%s", "Bar"}, message: "Dummy: FooBar: Error"},
|
||||
} {
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
err := newGenericError(ErrDummy, tc.arg...)
|
||||
assert.EqualError(t, err, tc.message)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package error
|
||||
|
||||
func NewRaceUnknownError(arg ...any) error {
|
||||
return newGenericError(ErrInputUnknownRace, arg...)
|
||||
}
|
||||
|
||||
func NewEntityTypeNameValidationError(arg ...any) error {
|
||||
return newGenericError(ErrInputEntityTypeNameInvalid, arg...)
|
||||
}
|
||||
|
||||
func NewEntityTypeNameDuplicateError(arg ...any) error {
|
||||
return newGenericError(ErrInputEntityTypeNameDuplicate, arg...)
|
||||
}
|
||||
|
||||
func NewEntityTypeNameEqualityError(arg ...any) error {
|
||||
return newGenericError(ErrInputEntityTypeNameEquality, arg...)
|
||||
}
|
||||
|
||||
func NewEntityNotExistsError(arg ...any) error {
|
||||
return newGenericError(ErrInputEntityNotExists, arg...)
|
||||
}
|
||||
|
||||
func NewEntityNotOwnedError(arg ...any) error {
|
||||
return newGenericError(ErrInputEntityNotOwned, arg...)
|
||||
}
|
||||
|
||||
func NewPlanetNumberError(arg ...any) error {
|
||||
return newGenericError(ErrInputPlanetNumber, arg...)
|
||||
}
|
||||
|
||||
func NewDriveValueError(arg ...any) error {
|
||||
return newGenericError(ErrInputDriveValue, arg...)
|
||||
}
|
||||
|
||||
func NewWeaponsValueError(arg ...any) error {
|
||||
return newGenericError(ErrInputWeaponsValue, arg...)
|
||||
}
|
||||
|
||||
func NewShieldsValueError(arg ...any) error {
|
||||
return newGenericError(ErrInputShieldsValue, arg...)
|
||||
}
|
||||
|
||||
func NewCargoValueError(arg ...any) error {
|
||||
return newGenericError(ErrInputCargoValue, arg...)
|
||||
}
|
||||
|
||||
func NewShipTypeArmamentValueError(arg ...any) error {
|
||||
return newGenericError(ErrInputShipTypeArmamentValue, arg...)
|
||||
}
|
||||
|
||||
func NewShipTypeArmamentAndWeaponsValueError(arg ...any) error {
|
||||
return newGenericError(ErrInputShipTypeWeaponsAndArmamentValue, arg...)
|
||||
}
|
||||
|
||||
func NewShipTypeShipTypeZeroValuesError(arg ...any) error {
|
||||
return newGenericError(ErrInputShipTypeZeroValues, arg...)
|
||||
}
|
||||
|
||||
func NewScienceSumValuesError(arg ...any) error {
|
||||
return newGenericError(ErrInputScienceSumValues, arg...)
|
||||
}
|
||||
|
||||
func NewProductionInvalidError(arg ...any) error {
|
||||
return newGenericError(ErrInputProductionInvalid, arg...)
|
||||
}
|
||||
|
||||
func NewMergeShipTypeNotEqualError(arg ...any) error {
|
||||
return newGenericError(ErrMergeShipTypeNotEqual, arg...)
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package error
|
||||
|
||||
func NewRepoError(arg ...any) error {
|
||||
return newGenericError(ErrStorageFailure, arg...)
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package error
|
||||
|
||||
func NewGameStateError(arg ...any) error {
|
||||
return newGenericError(ErrGameStateInvalid, arg...)
|
||||
}
|
||||
|
||||
func NewDeleteShipTypeExistingGroupError(arg ...any) error {
|
||||
return newGenericError(ErrDeleteShipTypeExistingGroup, arg...)
|
||||
}
|
||||
|
||||
func NewDeleteShipTypePlanetProductionError(arg ...any) error {
|
||||
return newGenericError(ErrDeleteShipTypePlanetProduction, arg...)
|
||||
}
|
||||
|
||||
func NewDeleteSciencePlanetProductionError(arg ...any) error {
|
||||
return newGenericError(ErrDeleteSciencePlanetProduction, arg...)
|
||||
}
|
||||
Reference in New Issue
Block a user