errors.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package domain
  2. import (
  3. "errors"
  4. "fmt"
  5. _ "golang.org/x/text/message"
  6. )
  7. type ErrorKind int
  8. const (
  9. NoError ErrorKind = iota
  10. Unimplemented
  11. ShortNotCreated
  12. ShortNotFound
  13. TargetBlockedError
  14. TargetCensoredError
  15. TargetInvalidError
  16. StorageReadError
  17. StorageWriteError
  18. StorageUnspecifiedError
  19. )
  20. var Errors = map[ErrorKind]string{
  21. NoError: "No error",
  22. Unimplemented: "Not yet implemented",
  23. ShortNotCreated: "Short URL not created for a new target",
  24. ShortNotFound: "Short URL not defined",
  25. TargetBlockedError: "Target blocked",
  26. TargetCensoredError: "Target unavailable for legal reasons",
  27. TargetInvalidError: "Target invalid",
  28. StorageReadError: "Storage read error",
  29. StorageWriteError: "Storage write error",
  30. StorageUnspecifiedError: "Storage unspecified error",
  31. }
  32. type Error struct {
  33. error
  34. Kind ErrorKind
  35. }
  36. func MakeError(kind ErrorKind, detail string) Error {
  37. var message string
  38. if len(detail) == 0 {
  39. message = Errors[kind]
  40. } else {
  41. message = fmt.Sprintf("%s: %s", Errors[kind], detail)
  42. }
  43. return Error{
  44. Kind: kind,
  45. error: errors.New(message),
  46. }
  47. }