error.go 758 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package muesli
  2. import "fmt"
  3. // ErrorValue are Muesli errors
  4. type ErrorValue struct {
  5. error
  6. }
  7. func (v ErrorValue) Type() TypeValue { return ErrorType }
  8. func (val ErrorValue) String() string {
  9. return fmt.Sprintf("%s", val.Error())
  10. }
  11. const ErrorType = TypeValue("Error")
  12. func NewErrorValuef(format string, args ...interface{}) ErrorValue {
  13. err := fmt.Errorf(format, args...)
  14. return ErrorValue{err}
  15. }
  16. func (from ErrorValue) Convert(to interface{}) error {
  17. switch toPtr := to.(type) {
  18. case *string:
  19. (*toPtr) = from.String()
  20. case *error:
  21. (*toPtr) = from.error
  22. case *ErrorValue:
  23. (*toPtr) = from
  24. case *Value:
  25. (*toPtr) = from
  26. default:
  27. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  28. }
  29. return nil
  30. }