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)
|
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())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue