Better error handling in server

This commit is contained in:
Felix Krause 2016-02-18 22:01:13 +01:00
parent 7eca93f988
commit b2707f99d7

View File

@ -20,42 +20,31 @@ routes:
headers["Pragma"] = "no-cache"
headers["Cache-Control"] = "no-cache"
headers["Expires"] = "0"
case @"style"
of "minimal": style = psMinimal
of "canonical": style = psCanonical
of "default": style = psDefault
of "json": style = psJson
of "block": style = psBlockOnly
of "tokens":
var
output = ""
parser = newYamlParser()
events = parser.parse(newStringStream(@"input"))
try:
try:
case @"style"
of "minimal": style = psMinimal
of "canonical": style = psCanonical
of "default": style = psDefault
of "json": style = psJson
of "block": style = psBlockOnly
of "tokens":
var
output = ""
parser = newYamlParser()
events = parser.parse(newStringStream(@"input"))
for event in events:
output.add($event & "\n")
resultNode["code"] = %0
resultNode["output"] = %output
resp resultNode.pretty, "application/json"
tokens = true
except YamlParserError:
let e = (ref YamlParserError)(getCurrentException())
resultNode["code"] = %1
resultNode["line"] = %e.line
resultNode["column"] = %e.column
resultNode["message"] = %e.msg
resultNode["detail"] = %e.lineContent
if not tokens:
var output = newStringStream()
transform(newStringStream(@"input"), output, style)
resultNode["code"] = %0
resultNode["output"] = %output.data
resp resultNode.pretty, "application/json"
if not tokens:
echo "outputting YAML"
var
output = newStringStream()
try:
transform(newStringStream(@"input"), output, style)
resultNode["code"] = %0
resultNode["output"] = %output.data
resp resultNode.pretty, "application/json"
except YamlParserError:
except YamlParserError:
let e = (ref YamlParserError)(getCurrentException())
resultNode["code"] = %1
resultNode["line"] = %e.line
@ -63,13 +52,13 @@ routes:
resultNode["message"] = %e.msg
resultNode["detail"] = %e.lineContent
resp resultNode.pretty, "application/json"
except YamlPresenterJsonError:
except YamlPresenterJsonError:
let e = getCurrentException()
resultNode["code"] = %2
resultNode["message"] = %e.msg
headers["Content-Type"] = "application/json"
resp resultNode.pretty, "application/json"
except:
except:
let e = getCurrentException()
let msg = "Name: " & $e.name & "\nMessage: " & e.msg &
"\nTrace:\n" & e.getStackTrace