errorhelpers.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package errorhelpers
  2. import (
  3. "fmt"
  4. "errors"
  5. "git.mmnx.de/Moe/usermanager"
  6. "git.mmnx.de/Moe/templatehelpers"
  7. "github.com/kataras/iris"
  8. )
  9. const (
  10. ERR_LVL_NOTIFICATION = 0
  11. ERR_LVL_INFORMATION = 1
  12. ERR_LVL_WARNING = 2
  13. ERR_LVL_ERROR = 3
  14. ERR_LVL_FATAL = 4
  15. )
  16. type Error struct {
  17. err error
  18. errLvl int
  19. }
  20. func(err Error) getErrLvl() int {
  21. return err.errLvl
  22. }
  23. func(err Error) getError() error {
  24. return err.err
  25. }
  26. func MakeError(e interface{}) Error {
  27. var err error
  28. var logLvl int
  29. // test for input type
  30. if v, isError := e.(error); isError {
  31. err = v
  32. } else if v, isString := e.(string); isString {
  33. err = errors.New(v)
  34. } else {
  35. fmt.Print("Error Type not implemented:")
  36. DebugVar(e)
  37. }
  38. switch err.Error() {
  39. case usermanager.ERR_USER_NOT_FOUND, usermanager.ERR_PASSWORD_MISMATCH, usermanager.ERR_USERNAME_TAKEN:
  40. logLvl = ERR_LVL_WARNING
  41. default:
  42. logLvl = ERR_LVL_ERROR
  43. }
  44. return Error{err, logLvl}
  45. }
  46. func DebugVar(v interface{}) {
  47. fmt.Printf("%#v\n", v)
  48. }
  49. func HandleError(e interface{}, ctx *iris.Context, alt []string) {
  50. if e == nil { // if no error do nothing
  51. if len(alt) > 0 {
  52. templatehelpers.ShowNotification(alt, ctx)
  53. }
  54. return
  55. }
  56. // TODO: alternative for success (if err == nil)
  57. // ONLY if alternative's not empty!
  58. err := MakeError(e) // make an Error object
  59. switch err.getErrLvl() {
  60. case ERR_LVL_NOTIFICATION:
  61. templatehelpers.ShowNotification([]string{err.getError().Error()}, ctx)
  62. case ERR_LVL_INFORMATION:
  63. templatehelpers.ShowNotification([]string{err.getError().Error()}, ctx) // TODO: information custom color
  64. case ERR_LVL_WARNING:
  65. templatehelpers.ShowError([]string{err.getError().Error()}, ctx)
  66. case ERR_LVL_ERROR:
  67. templatehelpers.ShowError([]string{err.getError().Error()}, ctx)
  68. default:
  69. fmt.Print("Not implemented yet: ")
  70. DebugVar(err)
  71. }
  72. }