errors.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package ut
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-playground/locales"
  6. )
  7. var (
  8. // ErrUnknowTranslation indicates the translation could not be found
  9. ErrUnknowTranslation = errors.New("Unknown Translation")
  10. )
  11. var _ error = new(ErrConflictingTranslation)
  12. var _ error = new(ErrRangeTranslation)
  13. var _ error = new(ErrOrdinalTranslation)
  14. var _ error = new(ErrCardinalTranslation)
  15. var _ error = new(ErrLocaleNotFound)
  16. var _ error = new(ErrMissingPluralTranslation)
  17. // ErrConflictingTranslation is the error representing a conflicting translation
  18. type ErrConflictingTranslation struct {
  19. key interface{}
  20. rule locales.PluralRule
  21. text string
  22. }
  23. // Error returns ErrConflictingTranslation's internal error text
  24. func (e *ErrConflictingTranslation) Error() string {
  25. return fmt.Sprintf("warning: conflicting key '%#v' rule '%d' with text '%s', value being ignored", e.key, e.rule, e.text)
  26. }
  27. // ErrRangeTranslation is the error representing a range translation error
  28. type ErrRangeTranslation struct {
  29. text string
  30. }
  31. // Error returns ErrRangeTranslation's internal error text
  32. func (e *ErrRangeTranslation) Error() string {
  33. return e.text
  34. }
  35. // ErrOrdinalTranslation is the error representing an ordinal translation error
  36. type ErrOrdinalTranslation struct {
  37. text string
  38. }
  39. // Error returns ErrOrdinalTranslation's internal error text
  40. func (e *ErrOrdinalTranslation) Error() string {
  41. return e.text
  42. }
  43. // ErrCardinalTranslation is the error representing a cardinal translation error
  44. type ErrCardinalTranslation struct {
  45. text string
  46. }
  47. // Error returns ErrCardinalTranslation's internal error text
  48. func (e *ErrCardinalTranslation) Error() string {
  49. return e.text
  50. }
  51. // ErrLocaleNotFound is the error signifying a locale which could not be found
  52. type ErrLocaleNotFound struct {
  53. text string
  54. }
  55. // Error returns ErrLocaleNotFound's internal error text
  56. func (e *ErrLocaleNotFound) Error() string {
  57. return e.text
  58. }
  59. // ErrMissingPluralTranslation is the error signifying a missing translation given
  60. // the locales plural rules.
  61. type ErrMissingPluralTranslation struct {
  62. key interface{}
  63. rule locales.PluralRule
  64. translationType string
  65. }
  66. // Error returns ErrMissingPluralTranslation's internal error text
  67. func (e *ErrMissingPluralTranslation) Error() string {
  68. return fmt.Sprintf("error: missing %s plural rule '%s' for translation with key '%#v", e.translationType, e.rule, e.key)
  69. }