|
@@ -3,17 +3,30 @@ package errorhelpers
|
|
|
import (
|
|
import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"errors"
|
|
"errors"
|
|
|
- "git.mmnx.de/Moe/usermanager"
|
|
|
|
|
"git.mmnx.de/Moe/templatehelpers"
|
|
"git.mmnx.de/Moe/templatehelpers"
|
|
|
"github.com/kataras/iris"
|
|
"github.com/kataras/iris"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
const (
|
|
|
- ERR_LVL_NOTIFICATION = 0
|
|
|
|
|
- ERR_LVL_INFORMATION = 1
|
|
|
|
|
- ERR_LVL_WARNING = 2
|
|
|
|
|
- ERR_LVL_ERROR = 3
|
|
|
|
|
- ERR_LVL_FATAL = 4
|
|
|
|
|
|
|
+ // Error levels
|
|
|
|
|
+ ERR_NO_ERR = -1
|
|
|
|
|
+ ERR_LVL_INFORMATION = 0
|
|
|
|
|
+ ERR_LVL_WARNING = 1
|
|
|
|
|
+ ERR_LVL_ERROR = 2
|
|
|
|
|
+ ERR_LVL_FATAL = 3
|
|
|
|
|
+ // User errors
|
|
|
|
|
+ ERR_USER_NOT_FOUND = "ERR_USER_NOT_FOUND"
|
|
|
|
|
+ ERR_PASSWORD_MISMATCH = "ERR_PASSWORD_MISMATCH"
|
|
|
|
|
+ ERR_SESSION_TIMED_OUT = "ERR_SESSION_TIMED_OUT"
|
|
|
|
|
+ ERR_INVALID_TOKEN = "ERR_INVALID_TOKEN"
|
|
|
|
|
+ ERR_USERNAME_TAKEN = "ERR_USERNAME_TAKEN"
|
|
|
|
|
+ ERR_INVALID_PARAM = "ERR_INVALID_PARAM"
|
|
|
|
|
+ ERR_NO_CHANGES = "ERR_NO_CHANGES"
|
|
|
|
|
+ SUCCESS_LOGIN = "SUCCESS_LOGIN"
|
|
|
|
|
+ SUCCESS_LOGOUT = "SUCCESS_LOGOUT"
|
|
|
|
|
+ SUCCESS_UPDATE = "SUCCESS_UPDATE"
|
|
|
|
|
+ SUCCESS_REGISTER = "SUCCESS_REGISTER"
|
|
|
|
|
+ SUCCESS_TOKENS_GENERATED = "SUCCESS_TOKENS_GENERATED"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type Error struct {
|
|
type Error struct {
|
|
@@ -30,6 +43,10 @@ func(err Error) getError() error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func MakeError(e interface{}) Error {
|
|
func MakeError(e interface{}) Error {
|
|
|
|
|
+ if e == nil { // check if it's an error
|
|
|
|
|
+ return Error{nil, ERR_NO_ERR}
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
var err error
|
|
var err error
|
|
|
var logLvl int
|
|
var logLvl int
|
|
|
// test for input type
|
|
// test for input type
|
|
@@ -38,13 +55,14 @@ func MakeError(e interface{}) Error {
|
|
|
} else if v, isString := e.(string); isString {
|
|
} else if v, isString := e.(string); isString {
|
|
|
err = errors.New(v)
|
|
err = errors.New(v)
|
|
|
} else {
|
|
} else {
|
|
|
- fmt.Print("Error Type not implemented:")
|
|
|
|
|
|
|
+ fmt.Printf("Error Type not implemented: %T", e)
|
|
|
DebugVar(e)
|
|
DebugVar(e)
|
|
|
|
|
+ DebugVar(&e)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- switch err.Error() {
|
|
|
|
|
- case usermanager.ERR_USER_NOT_FOUND, usermanager.ERR_PASSWORD_MISMATCH, usermanager.ERR_USERNAME_TAKEN:
|
|
|
|
|
- logLvl = ERR_LVL_WARNING
|
|
|
|
|
|
|
+ switch err.Error() { // TODO: starts with ?
|
|
|
|
|
+ case SUCCESS_LOGIN, SUCCESS_LOGOUT, SUCCESS_TOKENS_GENERATED, SUCCESS_UPDATE, SUCCESS_REGISTER:
|
|
|
|
|
+ logLvl = ERR_LVL_INFORMATION
|
|
|
default:
|
|
default:
|
|
|
logLvl = ERR_LVL_ERROR
|
|
logLvl = ERR_LVL_ERROR
|
|
|
}
|
|
}
|
|
@@ -56,30 +74,45 @@ func DebugVar(v interface{}) {
|
|
|
fmt.Printf("%#v\n", v)
|
|
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)
|
|
|
|
|
|
|
+func HandleError(e interface{}, ctx *iris.Context) {
|
|
|
|
|
+ err := MakeError(e)
|
|
|
|
|
+ params := ctx.Get("params").(map[string]string)
|
|
|
|
|
+
|
|
|
|
|
+ if err.getError() == nil { // if no error ..., TODO clean this up
|
|
|
|
|
+ if len(params["notification"]) < 1 { // if we got one, show a notification
|
|
|
|
|
+ ctx.Next() // ... execute next middleware
|
|
|
|
|
+ return
|
|
|
|
|
+ } else {
|
|
|
|
|
+ err = MakeError(params["notification"]) // copy so that we dont get a nil pointer later
|
|
|
}
|
|
}
|
|
|
- return
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // TODO: alternative for success (if err == nil)
|
|
|
|
|
- // ONLY if alternative's not empty!
|
|
|
|
|
|
|
+ params["notification"] = err.getError().Error() // TODO human readable
|
|
|
|
|
+ params["notificationType"] = fmt.Sprintf("%d", err.getErrLvl())
|
|
|
|
|
+
|
|
|
|
|
+ /** Some errors need to be 'redirected' **/
|
|
|
|
|
+
|
|
|
|
|
+ switch err.getError().Error() {
|
|
|
|
|
+ case SUCCESS_LOGIN: // open home after login
|
|
|
|
|
+ params["reqDir"] = "home"
|
|
|
|
|
+ case SUCCESS_LOGOUT, ERR_SESSION_TIMED_OUT, ERR_INVALID_TOKEN:
|
|
|
|
|
+ params["reqDir"] = "login"
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ ctx.Set("params", params)
|
|
|
|
|
|
|
|
- err := MakeError(e) // make an Error object
|
|
|
|
|
|
|
+ // fmt.Printf("para %#v\n", params)
|
|
|
|
|
|
|
|
switch err.getErrLvl() {
|
|
switch err.getErrLvl() {
|
|
|
- case ERR_LVL_NOTIFICATION:
|
|
|
|
|
- templatehelpers.ShowNotification([]string{err.getError().Error()}, ctx)
|
|
|
|
|
case ERR_LVL_INFORMATION:
|
|
case ERR_LVL_INFORMATION:
|
|
|
- templatehelpers.ShowNotification([]string{err.getError().Error()}, ctx) // TODO: information custom color
|
|
|
|
|
|
|
+ templatehelpers.ShowNotification(ctx) // TODO: information custom color
|
|
|
case ERR_LVL_WARNING:
|
|
case ERR_LVL_WARNING:
|
|
|
- templatehelpers.ShowError([]string{err.getError().Error()}, ctx)
|
|
|
|
|
|
|
+ templatehelpers.ShowError(ctx)
|
|
|
case ERR_LVL_ERROR:
|
|
case ERR_LVL_ERROR:
|
|
|
- templatehelpers.ShowError([]string{err.getError().Error()}, ctx)
|
|
|
|
|
|
|
+ templatehelpers.ShowError(ctx)
|
|
|
default:
|
|
default:
|
|
|
fmt.Print("Not implemented yet: ")
|
|
fmt.Print("Not implemented yet: ")
|
|
|
|
|
+ ctx.StopExecution()
|
|
|
DebugVar(err)
|
|
DebugVar(err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|