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