Skip to content

Commit

Permalink
fix 404 error message (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h committed Mar 5, 2024
1 parent 1461284 commit 9504fed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/basolato/core/libservers/nostd/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ proc serve*(seqRoutes:seq[Routes], port=5000) =
let msg = getCurrentExceptionMsg()
let status = Http500
response = Response.new(status, errorPage(status, msg), headers)
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
echoErrorMsg(msg)

# except:
Expand All @@ -104,11 +105,24 @@ proc serve*(seqRoutes:seq[Routes], port=5000) =
# echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path}")
# echoErrorMsg(exception.msg)

if response.status == HttpCode(0):
if response.status.is4xx:
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
response = Response.new(response.status, errorPage(response.status, response.body), headers)
elif response.status.is5xx:
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
response = Response.new(response.status, errorPage(response.status, response.body), headers)
elif response.status == HttpCode(0):
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$Http404} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
response = Response.new(Http404, errorPage(Http404, ""), headers)
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path}")

when defined(httpbeast):
req.send(response.status, response.body, response.headers.format().toString())
Expand Down
13 changes: 8 additions & 5 deletions src/basolato/core/libservers/std/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ proc serveCore(params:(Routes, int)){.async.} =
let msg = getCurrentExceptionMsg()
let status = Http500
response = Response.new(status, errorPage(status, msg), headers)
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
echoErrorMsg(msg)

# except:
Expand Down Expand Up @@ -101,17 +102,20 @@ proc serveCore(params:(Routes, int)){.async.} =
if response.status.is4xx:
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
response = Response.new(response.status, errorPage(response.status, response.body), headers)
elif response.status.is5xx:
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$response.status} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
response = Response.new(response.status, errorPage(response.status, response.body), headers)
elif response.status == HttpCode(0):
var headers = newHttpHeaders()
headers["content-type"] = "text/html; charset=utf-8"
echoErrorMsg(&"{$response.status} {req.hostname} {$req.httpMethod} {req.path}")
let userAgent = req.headers["User-Agent"]
echoErrorMsg(&"{$Http404} {$req.httpMethod} {req.path} {req.hostname} {userAgent}")
response = Response.new(Http404, errorPage(Http404, ""), headers)

response.headers.setDefaultHeaders()
Expand All @@ -129,7 +133,6 @@ proc serveCore(params:(Routes, int)){.async.} =
# wait 500ms for FDs to be closed
await sleepAsync(500)

# asyncCheck server.serve(Port(port), cb, HOST_ADDR)

proc serve*(seqRoutes: seq[Routes]) =
var routes = Routes.new()
Expand Down

0 comments on commit 9504fed

Please sign in to comment.