errors.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(ErrMissingPluralTranslation)
  16. var _ error = new(ErrExistingTranslator)
  17. // ErrExistingTranslator is the error representing a conflicting translator
  18. type ErrExistingTranslator struct {
  19. locale string
  20. }
  21. // Error returns ErrExistingTranslator's internal error text
  22. func (e *ErrExistingTranslator) Error() string {
  23. return fmt.Sprintf("error: conflicting translator for locale '%s'", e.locale)
  24. }
  25. // ErrConflictingTranslation is the error representing a conflicting translation
  26. type ErrConflictingTranslation struct {
  27. key interface{}
  28. rule locales.PluralRule
  29. text string
  30. }
  31. // Error returns ErrConflictingTranslation's internal error text
  32. func (e *ErrConflictingTranslation) Error() string {
  33. if _, ok := e.key.(string); !ok {
  34. return fmt.Sprintf("error: conflicting key '%#v' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
  35. }
  36. return fmt.Sprintf("error: conflicting key '%s' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
  37. }
  38. // ErrRangeTranslation is the error representing a range translation error
  39. type ErrRangeTranslation struct {
  40. text string
  41. }
  42. // Error returns ErrRangeTranslation's internal error text
  43. func (e *ErrRangeTranslation) Error() string {
  44. return e.text
  45. }
  46. // ErrOrdinalTranslation is the error representing an ordinal translation error
  47. type ErrOrdinalTranslation struct {
  48. text string
  49. }
  50. // Error returns ErrOrdinalTranslation's internal error text
  51. func (e *ErrOrdinalTranslation) Error() string {
  52. return e.text
  53. }
  54. // ErrCardinalTranslation is the error representing a cardinal translation error
  55. type ErrCardinalTranslation struct {
  56. text string
  57. }
  58. // Error returns ErrCardinalTranslation's internal error text
  59. func (e *ErrCardinalTranslation) Error() string {
  60. return e.text
  61. }
  62. // ErrMissingPluralTranslation is the error signifying a missing translation given
  63. // the locales plural rules.
  64. type ErrMissingPluralTranslation struct {
  65. key interface{}
  66. rule locales.PluralRule
  67. translationType string
  68. }
  69. // Error returns ErrMissingPluralTranslation's internal error text
  70. func (e *ErrMissingPluralTranslation) Error() string {
  71. if _, ok := e.key.(string); !ok {
  72. return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%#v'", e.translationType, e.rule, e.key)
  73. }
  74. return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%s'", e.translationType, e.rule, e.key)
  75. }