Improve error handling

This commit is contained in:
Marco Realacci 2022-12-06 22:14:12 +01:00
parent df5b3fdda8
commit 3af4ee6c84
11 changed files with 34 additions and 35 deletions

View file

@ -39,9 +39,6 @@ func (rt *_router) Handler() http.Handler {
rt.router.GET("/stream", rt.wrap(rt.GetUserStream))
rt.router.GET("/", rt.getHelloWorld)
rt.router.GET("/context", rt.wrap(rt.getContextReply))
// Special routes
rt.router.GET("/liveness", rt.liveness)

View file

@ -1,14 +0,0 @@
package api
import (
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
"github.com/julienschmidt/httprouter"
"net/http"
)
// getContextReply is an example of HTTP endpoint that returns "Hello World!" as a plain text. The signature of this
// handler accepts a reqcontext.RequestContext (see httpRouterHandler).
func (rt *_router) getContextReply(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
w.Header().Set("content-type", "text/plain")
_, _ = w.Write([]byte("Hello World!"))
}

View file

@ -1,12 +0,0 @@
package api
import (
"github.com/julienschmidt/httprouter"
"net/http"
)
// getHelloWorld is an example of HTTP endpoint that returns "Hello world!" as a plain text
func (rt *_router) getHelloWorld(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("content-type", "text/plain")
_, _ = w.Write([]byte("Hello World!"))
}

View file

@ -15,7 +15,7 @@ import (
func (rt *_router) PostPhoto(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
//defer r.Body.Close()
// defer r.Body.Close()
uid := ps.ByName("user_id")
@ -109,7 +109,12 @@ func (rt *_router) GetPhoto(w http.ResponseWriter, r *http.Request, ps httproute
defer file.Close()
io.Copy(w, file)
_, err = io.Copy(w, file)
if err != nil {
helpers.SendInternalError(err, "Error writing response", w, rt.baseLogger)
return
}
}
func (rt *_router) DeletePhoto(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
@ -134,7 +139,7 @@ func (rt *_router) DeletePhoto(w http.ResponseWriter, r *http.Request, ps httpro
if err != nil {
helpers.SendInternalError(err, "Error deleting photo from database", w, rt.baseLogger)
return
} //todo: maybe let's use a transaction also here
} // todo: maybe let's use a transaction also here
if !deleted {
helpers.SendNotFound(w, "Photo not found", rt.baseLogger)