From 850fcc10510381fb565f349575a143ccfe20578a Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Tue, 2 Jul 2024 19:22:32 +0400 Subject: [PATCH] refactor: add sendError function and replace duplicates --- server.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server.go b/server.go index 9e06a5d..57ad1b8 100644 --- a/server.go +++ b/server.go @@ -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()) +}