chore: re-package
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrStorageFailure int = 1000 + iota
|
||||
ErrGameNotInitialized
|
||||
ErrGameStateInvalid
|
||||
ErrReportNotFound
|
||||
)
|
||||
|
||||
const (
|
||||
ErrDummy int = -1
|
||||
|
||||
ErrDeleteShipTypeExistingGroup = 5000
|
||||
ErrDeleteShipTypePlanetProduction = 5001
|
||||
ErrDeleteSciencePlanetProduction = 5002
|
||||
ErrMergeShipTypeNotEqual = 5003
|
||||
ErrBeakGroupNumberNotEnough = 5005
|
||||
ErrEntityInUse = 5006
|
||||
ErrShipsBusy = 5007
|
||||
ErrShipsNotOnSamePlanet = 5008
|
||||
ErrUpgradeGroupNumberNotEnough = 5010
|
||||
ErrUpgradeInsufficientResources = 5011
|
||||
ErrSendShipHasNoDrives = 5012
|
||||
ErrSendUnreachableDestination = 5013
|
||||
ErrSendShipOwnerHasNoPlanets = 5014
|
||||
ErrRaceExinct = 5015
|
||||
)
|
||||
|
||||
const (
|
||||
ErrInputUnknownRace int = 3000 + iota
|
||||
ErrInputUnknownRelation
|
||||
ErrInputSameRace
|
||||
ErrInputEntityTypeNameInvalid
|
||||
ErrInputNewEntityDuplicateIdentifier
|
||||
ErrInputEntityTypeNameEquality
|
||||
ErrInputEntityNotExists
|
||||
ErrInputEntityNotOwned
|
||||
ErrInputPlanetNumber
|
||||
ErrInputDriveValue
|
||||
ErrInputWeaponsValue
|
||||
ErrInputShieldsValue
|
||||
ErrInputCargoValue
|
||||
ErrInputShipTypeArmamentValue
|
||||
ErrInputShipTypeWeaponsAndArmamentValue
|
||||
ErrInputShipTypeZeroValues
|
||||
ErrInputScienceSumValues
|
||||
ErrInputProductionInvalid
|
||||
ErrInputCargoTypeInvalid
|
||||
ErrInputCargoLoadNotEnough
|
||||
ErrInputCargoLoadNotEqual
|
||||
ErrInputNoCargoBay
|
||||
ErrInputCargoLoadNoSpaceLeft
|
||||
ErrInputCargoUnloadEmpty
|
||||
ErrInputBreakGroupIllegalNumber
|
||||
ErrInputTechUnknown
|
||||
ErrInputTechInvalidMixing
|
||||
ErrInputUpgradeShipTechNotUsed
|
||||
ErrInputUpgradeParameterNotAllowed
|
||||
ErrInputUpgradeShipsAlreadyUpToDate
|
||||
ErrInputUpgradeTechLevelInsufficient
|
||||
ErrInputQuitCommandFollowedByCommand
|
||||
ErrInputUnrecognizedCommand
|
||||
)
|
||||
|
||||
func GenericErrorText(code int) string {
|
||||
switch code {
|
||||
case ErrDummy:
|
||||
return "Dummy"
|
||||
case ErrStorageFailure:
|
||||
return "Storage failure"
|
||||
case ErrGameNotInitialized:
|
||||
return "Game not yet initialized"
|
||||
case ErrGameStateInvalid:
|
||||
return "Invalid game state"
|
||||
case ErrInputUnknownRace:
|
||||
return "Race name is unknown to this game"
|
||||
case ErrInputUnknownRelation:
|
||||
return "Unknown relation"
|
||||
case ErrInputSameRace:
|
||||
return "Race name must be different from your own"
|
||||
case ErrInputEntityTypeNameInvalid:
|
||||
return "Name has invalid length or symbols"
|
||||
case ErrInputNewEntityDuplicateIdentifier:
|
||||
return "Entity already exists"
|
||||
case ErrInputEntityTypeNameEquality:
|
||||
return "Names should differ"
|
||||
case ErrInputEntityNotExists:
|
||||
return "Entity does not exists"
|
||||
case ErrInputEntityNotOwned:
|
||||
return "Entity is not owned"
|
||||
case ErrEntityInUse:
|
||||
return "Entity currently in use"
|
||||
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 ErrInputCargoTypeInvalid:
|
||||
return "Invalid cargo type"
|
||||
case ErrInputCargoLoadNotEnough:
|
||||
return "Not enough cargo to load"
|
||||
case ErrInputCargoLoadNotEqual:
|
||||
return "Ship(s) already loaded with another cargo"
|
||||
case ErrInputNoCargoBay:
|
||||
return "Ship type is not designed to carry cargo"
|
||||
case ErrInputCargoLoadNoSpaceLeft:
|
||||
return "No space left on the ships to load cargo"
|
||||
case ErrInputCargoUnloadEmpty:
|
||||
return "Ships are not carrying any cargo"
|
||||
case ErrInputBreakGroupIllegalNumber:
|
||||
return "Illegal ships number to make new group"
|
||||
case ErrMergeShipTypeNotEqual:
|
||||
return "Source and target ship types are not the same"
|
||||
case ErrBeakGroupNumberNotEnough:
|
||||
return "Not enough ships in the group to make a separate group"
|
||||
case ErrShipsBusy:
|
||||
return "Ship(s) are'n free to use"
|
||||
case ErrShipsNotOnSamePlanet:
|
||||
return "Ships not on the same planet"
|
||||
case ErrInputTechUnknown:
|
||||
return "Technology name unknown"
|
||||
case ErrInputTechInvalidMixing:
|
||||
return "Technologies list must containt only specific values"
|
||||
case ErrInputUpgradeShipTechNotUsed:
|
||||
return "Technology is not used with ship class"
|
||||
case ErrInputUpgradeParameterNotAllowed:
|
||||
return "Parameter not allowed for upgrade"
|
||||
case ErrInputUpgradeShipsAlreadyUpToDate:
|
||||
return "Ships already up to date, nothing to upgrade"
|
||||
case ErrUpgradeGroupNumberNotEnough:
|
||||
return "Not enough ships in the group to make an upgrade"
|
||||
case ErrUpgradeInsufficientResources:
|
||||
return "Insufficient planet production capacity"
|
||||
case ErrInputUpgradeTechLevelInsufficient:
|
||||
return "Insifficient Tech level for requested upgrade"
|
||||
case ErrInputQuitCommandFollowedByCommand:
|
||||
return "'Quit' must be the last order's command"
|
||||
case ErrInputUnrecognizedCommand:
|
||||
return "Unrecognized command"
|
||||
case ErrSendShipHasNoDrives:
|
||||
return "One or more ships are not equipped with hyperdrive and cannot be moved"
|
||||
case ErrSendUnreachableDestination:
|
||||
return "Destination planet is too far for current Drive level"
|
||||
case ErrSendShipOwnerHasNoPlanets:
|
||||
return "Race is not owning any planet, all flights impossible"
|
||||
case ErrRaceExinct:
|
||||
return "Race is extinct"
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
module galaxy/error
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require github.com/stretchr/testify v1.11.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.10.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,173 @@
|
||||
package error
|
||||
|
||||
func NewRaceUnknownError(arg ...any) error {
|
||||
return newGenericError(ErrInputUnknownRace, arg...)
|
||||
}
|
||||
|
||||
func NewUnknownRelationError(arg ...any) error {
|
||||
return newGenericError(ErrInputUnknownRelation, arg...)
|
||||
}
|
||||
|
||||
func NewSameRaceError(arg ...any) error {
|
||||
return newGenericError(ErrInputSameRace, arg...)
|
||||
}
|
||||
|
||||
func NewEntityTypeNameValidationError(arg ...any) error {
|
||||
return newGenericError(ErrInputEntityTypeNameInvalid, arg...)
|
||||
}
|
||||
|
||||
func NewEntityDuplicateIdentifierError(arg ...any) error {
|
||||
return newGenericError(ErrInputNewEntityDuplicateIdentifier, 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 NewEntityInUseError(arg ...any) error {
|
||||
return newGenericError(ErrEntityInUse, 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 NewCargoTypeInvalidError(arg ...any) error {
|
||||
return newGenericError(ErrInputCargoTypeInvalid, arg...)
|
||||
}
|
||||
|
||||
func NewCargoLoadNotEnoughError(arg ...any) error {
|
||||
return newGenericError(ErrInputCargoLoadNotEnough, arg...)
|
||||
}
|
||||
|
||||
func NewCargoLoadNotEqualError(arg ...any) error {
|
||||
return newGenericError(ErrInputCargoLoadNotEqual, arg...)
|
||||
}
|
||||
|
||||
func NewNoCargoBayError(arg ...any) error {
|
||||
return newGenericError(ErrInputNoCargoBay, arg...)
|
||||
}
|
||||
|
||||
func NewCargoLoadNoSpaceLeftError(arg ...any) error {
|
||||
return newGenericError(ErrInputCargoLoadNoSpaceLeft, arg...)
|
||||
}
|
||||
|
||||
func NewCargoUnloadEmptyError(arg ...any) error {
|
||||
return newGenericError(ErrInputCargoUnloadEmpty, arg...)
|
||||
}
|
||||
|
||||
func NewBreakGroupIllegalNumberError(arg ...any) error {
|
||||
return newGenericError(ErrInputBreakGroupIllegalNumber, arg...)
|
||||
}
|
||||
|
||||
func NewMergeShipTypeNotEqualError(arg ...any) error {
|
||||
return newGenericError(ErrMergeShipTypeNotEqual, arg...)
|
||||
}
|
||||
|
||||
func NewBeakGroupNumberNotEnoughError(arg ...any) error {
|
||||
return newGenericError(ErrBeakGroupNumberNotEnough, arg...)
|
||||
}
|
||||
|
||||
func NewShipsBusyError(arg ...any) error {
|
||||
return newGenericError(ErrShipsBusy, arg...)
|
||||
}
|
||||
|
||||
func NewShipsNotOnSamePlanetError(arg ...any) error {
|
||||
return newGenericError(ErrShipsNotOnSamePlanet, arg...)
|
||||
}
|
||||
|
||||
func NewTechUnknownError(arg ...any) error {
|
||||
return newGenericError(ErrInputTechUnknown, arg...)
|
||||
}
|
||||
|
||||
func NewTechInvalidMixingError(arg ...any) error {
|
||||
return newGenericError(ErrInputTechInvalidMixing, arg...)
|
||||
}
|
||||
|
||||
func NewUpgradeShipTechNotUsedError(arg ...any) error {
|
||||
return newGenericError(ErrInputUpgradeShipTechNotUsed, arg...)
|
||||
}
|
||||
|
||||
func NewUpgradeParameterNotAllowedError(arg ...any) error {
|
||||
return newGenericError(ErrInputUpgradeParameterNotAllowed, arg...)
|
||||
}
|
||||
|
||||
func NewUpgradeShipsAlreadyUpToDateError(arg ...any) error {
|
||||
return newGenericError(ErrInputUpgradeShipsAlreadyUpToDate, arg...)
|
||||
}
|
||||
|
||||
func NewUpgradeGroupNumberNotEnoughError(arg ...any) error {
|
||||
return newGenericError(ErrUpgradeGroupNumberNotEnough, arg...)
|
||||
}
|
||||
|
||||
func NewUpgradeInsufficientResourcesError(arg ...any) error {
|
||||
return newGenericError(ErrUpgradeInsufficientResources, arg...)
|
||||
}
|
||||
|
||||
func NewUpgradeTechLevelInsufficientError(arg ...any) error {
|
||||
return newGenericError(ErrInputUpgradeTechLevelInsufficient, arg...)
|
||||
}
|
||||
|
||||
func NewSendShipHasNoDrivesError(arg ...any) error {
|
||||
return newGenericError(ErrSendShipHasNoDrives, arg...)
|
||||
}
|
||||
|
||||
func NewSendUnreachableDestinationError(arg ...any) error {
|
||||
return newGenericError(ErrSendUnreachableDestination, arg...)
|
||||
}
|
||||
|
||||
func NewSendShipOwnerHasNoPlanetsError(arg ...any) error {
|
||||
return newGenericError(ErrSendShipOwnerHasNoPlanets, arg...)
|
||||
}
|
||||
|
||||
func NewQuitCommandFollowedByCommandError(arg ...any) error {
|
||||
return newGenericError(ErrInputQuitCommandFollowedByCommand, arg...)
|
||||
}
|
||||
|
||||
func NewUnrecognizedCommandError(arg ...any) error {
|
||||
return newGenericError(ErrInputUnrecognizedCommand, arg...)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package error
|
||||
|
||||
func NewRepoError(arg ...any) error {
|
||||
return newGenericError(ErrStorageFailure, arg...)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package error
|
||||
|
||||
func NewRaceExinctError(arg ...any) error {
|
||||
return newGenericError(ErrRaceExinct, arg...)
|
||||
}
|
||||
|
||||
func NewGameNotInitializedError(arg ...any) error {
|
||||
return newGenericError(ErrGameNotInitialized, arg...)
|
||||
}
|
||||
|
||||
func NewReportNotFoundError(arg ...any) error {
|
||||
return newGenericError(ErrReportNotFound, arg...)
|
||||
}
|
||||
|
||||
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