errors.go 1.1 KB

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