refactor: add sendError function and replace duplicates

This commit is contained in:
DarkCat09 2024-07-02 19:22:32 +04:00
parent 27a8c3d1d1
commit 850fcc1051
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3

View file

@ -76,8 +76,7 @@ func handler(ctx *fasthttp.RequestCtx) {
ctx.Response.SetStatusCode(404) ctx.Response.SetStatusCode(404)
return return
} else if err != nil { } else if err != nil {
ctx.Response.SetStatusCode(500) sendError(ctx, "Unable to fetch row: ", err)
ctx.Response.SetBodyString("Unable to fetch row: " + err.Error())
return return
} }
@ -91,8 +90,7 @@ func handler(ctx *fasthttp.RequestCtx) {
fh, err := os.Open(path + "/headers") fh, err := os.Open(path + "/headers")
if err != nil { if err != nil {
ctx.Response.SetStatusCode(500) sendError(ctx, "Unable to read headers: ", err)
ctx.Response.SetBodyString("Unable to read headers: " + err.Error())
return return
} }
sc := bufio.NewScanner(fh) sc := bufio.NewScanner(fh)
@ -108,8 +106,7 @@ func handler(ctx *fasthttp.RequestCtx) {
fb, err := os.Open(path + "/body") fb, err := os.Open(path + "/body")
if err != nil { if err != nil {
ctx.Response.SetStatusCode(500) sendError(ctx, "Unable to read body: ", err)
ctx.Response.SetBodyString("Unable to read body: " + err.Error())
return return
} }
@ -132,3 +129,8 @@ func parseHost(host []byte, https bool) ([]byte, []byte) {
} }
return resHost, port return resHost, port
} }
func sendError(ctx *fasthttp.RequestCtx, msg string, err error) {
ctx.Response.SetStatusCode(500)
ctx.Response.SetBodyString(msg + err.Error())
}