Implement single error handler, start moving functions that discarded errors over to it

This commit is contained in:
Sean DuBois 2014-07-07 23:43:30 +00:00
parent f861fc22a8
commit 36cfd63495
3 changed files with 31 additions and 6 deletions

22
error.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type ErrorBody struct {
Error string
}
func writeHTTPError(res http.ResponseWriter, err error) {
res.WriteHeader(400)
encodedOutput, err := json.Marshal(ErrorBody{Error: err.Error()})
if err != nil {
fmt.Println("Failed to generate HTTP error: " + err.Error())
} else {
res.Write(encodedOutput)
}
}

View file

@ -15,10 +15,6 @@ type Configuration struct {
Fail2banSocket string
}
type ErrorBody struct {
Error string
}
var fail2goConn *fail2go.Conn
func main() {

View file

@ -8,10 +8,17 @@ import (
)
func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
globalStatus, _ := fail2goConn.GlobalStatus()
globalStatus, err := fail2goConn.GlobalStatus()
encodedOutput, err := json.Marshal(globalStatus)
if err != nil {
writeHTTPError(res, err)
return
}
encodedOutput, err := json.Marshal(globalStatus)
if err != nil {
writeHTTPError(res, err)
return
}
res.Write(encodedOutput)