|
@@ -1 +1,85 @@
|
|
|
-package errorhelpers
|
|
|
|
|
|
|
+package errorhelpers
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "errors"
|
|
|
|
|
+ "git.mmnx.de/Moe/usermanager"
|
|
|
|
|
+ "git.mmnx.de/Moe/templatehelpers"
|
|
|
|
|
+ "github.com/kataras/iris"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ ERR_LVL_NOTIFICATION = 0
|
|
|
|
|
+ ERR_LVL_INFORMATION = 1
|
|
|
|
|
+ ERR_LVL_WARNING = 2
|
|
|
|
|
+ ERR_LVL_ERROR = 3
|
|
|
|
|
+ ERR_LVL_FATAL = 4
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+type Error struct {
|
|
|
|
|
+ err error
|
|
|
|
|
+ errLvl int
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func(err Error) getErrLvl() int {
|
|
|
|
|
+ return err.errLvl
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func(err Error) getError() error {
|
|
|
|
|
+ return err.err
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func MakeError(e interface{}) Error {
|
|
|
|
|
+ var err error
|
|
|
|
|
+ var logLvl int
|
|
|
|
|
+ // test for input type
|
|
|
|
|
+ if v, isError := e.(error); isError {
|
|
|
|
|
+ err = v
|
|
|
|
|
+ } else if v, isString := e.(string); isString {
|
|
|
|
|
+ err = errors.New(v)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ fmt.Print("Error Type not implemented:")
|
|
|
|
|
+ DebugVar(e)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ switch err.Error() {
|
|
|
|
|
+ case usermanager.ERR_USER_NOT_FOUND, usermanager.ERR_PASSWORD_MISMATCH, usermanager.ERR_USERNAME_TAKEN:
|
|
|
|
|
+ logLvl = ERR_LVL_WARNING
|
|
|
|
|
+ default:
|
|
|
|
|
+ logLvl = ERR_LVL_ERROR
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Error{err, logLvl}
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func DebugVar(v interface{}) {
|
|
|
|
|
+ fmt.Printf("%#v\n", v)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func HandleError(e interface{}, ctx *iris.Context, alt []string) {
|
|
|
|
|
+ if e == nil { // if no error do nothing
|
|
|
|
|
+ if len(alt) > 0 {
|
|
|
|
|
+ templatehelpers.ShowNotification(alt, ctx)
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: alternative for success (if err == nil)
|
|
|
|
|
+ // ONLY if alternative's not empty!
|
|
|
|
|
+
|
|
|
|
|
+ err := MakeError(e) // make an Error object
|
|
|
|
|
+
|
|
|
|
|
+ switch err.getErrLvl() {
|
|
|
|
|
+ case ERR_LVL_NOTIFICATION:
|
|
|
|
|
+ templatehelpers.ShowNotification([]string{err.getError().Error()}, ctx)
|
|
|
|
|
+ case ERR_LVL_INFORMATION:
|
|
|
|
|
+ templatehelpers.ShowNotification([]string{err.getError().Error()}, ctx) // TODO: information custom color
|
|
|
|
|
+ case ERR_LVL_WARNING:
|
|
|
|
|
+ templatehelpers.ShowError([]string{err.getError().Error()}, ctx)
|
|
|
|
|
+ case ERR_LVL_ERROR:
|
|
|
|
|
+ templatehelpers.ShowError([]string{err.getError().Error()}, ctx)
|
|
|
|
|
+ default:
|
|
|
|
|
+ fmt.Print("Not implemented yet: ")
|
|
|
|
|
+ DebugVar(err)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|