Ally/War commands
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrDummy int = -1
|
||||
|
||||
ErrStorageFailure int = 1000
|
||||
ErrGameStateInvalid int = 2000
|
||||
|
||||
ErrInputUnknownHostRace int = 3000
|
||||
ErrInputUnknownOpponentRace int = 3001
|
||||
)
|
||||
|
||||
func errorText(code int) string {
|
||||
switch code {
|
||||
case ErrDummy:
|
||||
return "Dummy"
|
||||
case ErrStorageFailure:
|
||||
return "Storage failure"
|
||||
case ErrGameStateInvalid:
|
||||
return "Invalid game state"
|
||||
case ErrInputUnknownHostRace:
|
||||
return "Host race name is unknown to this game"
|
||||
case ErrInputUnknownOpponentRace:
|
||||
return "Opponent race name is unknown to this game"
|
||||
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 := errorText(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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user