Bugfix: Adding handlers

Bugfix: In case of trying to add multiple handlers to one path, the
system panics and stops. Now, the case was covered and an error message
appears, but the systems still works.
This commit is contained in:
Thorsten Sommer 2015-06-21 20:28:28 +02:00
parent e1465c2fd0
commit 326eb9ba7a

View File

@ -1,15 +1,36 @@
package Handlers package Handlers
import ( import (
"fmt"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"net/http" "net/http"
) )
// Function to add a new public handler. // Function to add a new public handler.
func AddPublicHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) { func AddPublicHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
// In case of an error, catch the error:
defer func() {
if err := recover(); err != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameSTATE, fmt.Sprintf("Was not able to add a public handler, because the path '%s' is already in use. %s", pattern, err))
return
}
}()
// Add the handler:
muxPublic.HandleFunc(pattern, handler) muxPublic.HandleFunc(pattern, handler)
} }
// Function to add a new private handler. // Function to add a new private handler.
func AddAdminHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) { func AddAdminHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) {
// In case of an error, catch the error:
defer func() {
if err := recover(); err != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameSTATE, fmt.Sprintf("Was not able to add a private admin handler, because the path '%s' is already in use. %s", pattern, err))
return
}
}()
// Add the handler:
muxAdmin.HandleFunc(pattern, handler) muxAdmin.HandleFunc(pattern, handler)
} }