2018-09-18 22:42:38 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-22 02:36:33 +02:00
|
|
|
ErrMissedArgument = errors.New("missed argument")
|
|
|
|
ErrInvalidArgument = errors.New("invalid argument")
|
|
|
|
ErrInvalidArgumentNumber = errors.New("invalid argument number")
|
|
|
|
ErrInvalidType = errors.New("invalid type")
|
|
|
|
ErrInvalidOperation = errors.New("invalid operation")
|
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
ErrNotUnique = errors.New("not unique")
|
|
|
|
ErrTerminated = errors.New("operation is terminated")
|
|
|
|
ErrUnexpected = errors.New("unexpected error")
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const typeErrorTemplate = "expected %s, but got %s"
|
|
|
|
|
|
|
|
func SourceError(src SourceMap, err error) error {
|
|
|
|
return errors.Errorf("%s %s", err.Error(), src.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TypeError(actual Type, expected ...Type) error {
|
|
|
|
if len(expected) == 0 {
|
|
|
|
return Error(ErrInvalidType, actual.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(expected) == 1 {
|
|
|
|
return Error(ErrInvalidType, fmt.Sprintf(typeErrorTemplate, expected, actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
strs := make([]string, len(expected))
|
|
|
|
|
|
|
|
for idx, t := range expected {
|
|
|
|
strs[idx] = t.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedStr := strings.Join(strs, " or ")
|
|
|
|
|
|
|
|
return Error(ErrInvalidType, fmt.Sprintf(typeErrorTemplate, expectedStr, actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(err error, msg string) error {
|
|
|
|
return errors.Errorf("%s: %s", err.Error(), msg)
|
|
|
|
}
|