chore: improve delete

This commit is contained in:
2023-10-06 10:46:45 +01:00
parent 73511ec401
commit 16a6ae844b
4 changed files with 74 additions and 35 deletions

View File

@@ -219,14 +219,17 @@ func decodeBody(r *http.Request) (string, *Error) {
}
func handleError(err *Error, w http.ResponseWriter, context *Context) {
if err != nil {
data := context.AddMap(err.data)
w.WriteHeader(err.Code)
if err.Code == http.StatusNotFound {
if context.Mode == HTML {
w.WriteHeader(309)
context.Mode = HTMLFULL
}
LoadBasedOnAnswer(context.Mode, w, "404.html", data)
return
}
w.WriteHeader(err.Code)
if err.Code == http.StatusBadRequest {
LoadBasedOnAnswer(context.Mode, w, "400.html", data)
return
@@ -343,13 +346,48 @@ type Context struct {
Logger *log.Logger
}
func (c Context) Error400(err error, message string, w http.ResponseWriter, path string, base string, data AnyMap) *Error {
c.SetReportCaller(true)
c.Logger.Error(message)
c.SetReportCaller(false)
if err != nil {
c.Logger.Errorf("Something went wrong returning with: %d\n.Err:\n", http.StatusBadRequest)
c.Logger.Error(err)
}
if c.Mode == JSON {
return &Error{http.StatusBadRequest, nil, c.AddMap(data)}
}
LoadDefineTemplate(w, path, base, c.AddMap(data))
return nil
}
func (c Context) SetReportCaller(report bool) {
if (report) {
c.Logger.SetCallerOffset(2)
c.Logger.SetReportCaller(true)
} else {
c.Logger.SetCallerOffset(1)
c.Logger.SetReportCaller(false)
}
}
func (c Context) ErrorCode(err error, code int, data AnyMap) *Error {
// TODO Improve Logging
if (code == 400) {
c.SetReportCaller(true)
c.Logger.Warn("When returning BadRequest(400) please use context.Error400\n")
c.SetReportCaller(false)
}
if err != nil {
c.Logger.Errorf("Something went wrong returning with: %d\n.Err:\n", code)
c.Logger.Error(err)
}
return &Error{code, nil, data}
return &Error{code, nil, c.AddMap(data)}
}
func (c Context) Error500(err error) *Error {
return c.ErrorCode(err, http.StatusInternalServerError, nil)
}
func (c Context) AddMap(m AnyMap) AnyMap {
@@ -516,12 +554,12 @@ func NewHandler(db *sql.DB) *Handle {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Decide answertype
ans := NORMAL
if r.Header.Get("HX-Request") == "true" || r.Header.Get("Request-Type") == "html" {
ans = HTML
}
if r.Header.Get("Request-Type") == "htmlfull" {
ans = HTMLFULL
}
if r.Header.Get("Request-Type") == "html" {
ans = HTML
}
//TODO JSON
//Login state