1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-02 11:52:56 +00:00
arikawa/utils/bot/error.go

87 lines
1.7 KiB
Go
Raw Normal View History

2020-01-19 06:06:00 +00:00
package bot
import (
2020-05-03 22:59:10 +00:00
"errors"
"fmt"
2020-01-19 06:06:00 +00:00
"strings"
)
type UnknownCommandError struct {
2020-05-10 08:45:00 +00:00
Parts []string // max len 2
Subcmd *Subcommand
2020-01-19 06:06:00 +00:00
}
func newErrUnknownCommand(s *Subcommand, parts []string) error {
if len(parts) > 2 {
parts = parts[:2]
}
return &UnknownCommandError{
Parts: parts,
Subcmd: s,
2020-05-10 08:45:00 +00:00
}
}
func (err *UnknownCommandError) Error() string {
2020-01-25 02:49:33 +00:00
return UnknownCommandString(err)
}
var UnknownCommandString = func(err *UnknownCommandError) string {
// Subcommand check.
if err.Subcmd.StructName == "" || len(err.Parts) < 2 {
return "unknown command: " + err.Parts[0] + "."
}
return fmt.Sprintf("unknown %s subcommand: %s.", err.Parts[0], err.Parts[1])
2020-01-19 06:06:00 +00:00
}
2020-05-03 22:59:10 +00:00
var (
2020-05-16 21:14:49 +00:00
ErrTooManyArgs = errors.New("too many arguments given")
ErrNotEnoughArgs = errors.New("not enough arguments given")
2020-05-03 22:59:10 +00:00
)
type InvalidUsageError struct {
2020-05-03 22:59:10 +00:00
Prefix string
Args []string
Index int
Wrap error
2020-01-19 06:06:00 +00:00
// TODO: usage generator?
// Here, as a reminder
2020-05-10 08:45:00 +00:00
Ctx *MethodContext
2020-01-19 06:06:00 +00:00
}
func (err *InvalidUsageError) Error() string {
2020-01-25 02:49:33 +00:00
return InvalidUsageString(err)
}
func (err *InvalidUsageError) Unwrap() error {
2020-05-03 22:59:10 +00:00
return err.Wrap
}
var InvalidUsageString = func(err *InvalidUsageError) string {
2020-05-14 03:42:31 +00:00
if err.Index == 0 && err.Wrap != nil {
2020-05-16 21:14:49 +00:00
return "invalid usage, error: " + err.Wrap.Error() + "."
2020-01-19 06:06:00 +00:00
}
2020-05-14 03:42:31 +00:00
if err.Index == 0 || len(err.Args) == 0 {
2020-05-16 21:14:49 +00:00
return "missing arguments. Refer to help."
2020-01-19 06:06:00 +00:00
}
body := "Invalid usage at " +
2020-05-03 22:59:10 +00:00
// Write the prefix.
err.Prefix +
// Write the first part
strings.Join(err.Args[:err.Index], " ") +
// Write the wrong part
" __" + err.Args[err.Index] + "__ " +
// Write the last part
strings.Join(err.Args[err.Index+1:], " ")
2020-01-19 06:06:00 +00:00
2020-05-03 22:59:10 +00:00
if err.Wrap != nil {
body += "\nError: " + err.Wrap.Error() + "."
2020-01-19 06:06:00 +00:00
}
return body
}