refactor: add sendError function and replace duplicates
This commit is contained in:
parent
27a8c3d1d1
commit
850fcc1051
1 changed files with 8 additions and 6 deletions
14
server.go
14
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())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue