Better error handling - fix #39, #47

This commit is contained in:
Ganesh Viswanathan 2019-01-19 15:02:49 -06:00
commit 3d7682c611
2 changed files with 21 additions and 17 deletions

View file

@ -18,10 +18,8 @@ proc findPath(path: string, fail = true): string =
# Relative to project path # Relative to project path
result = joinPathIfRel(getProjectPath(), path).replace("\\", "/") result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
if not fileExists(result) and not dirExists(result): if not fileExists(result) and not dirExists(result):
if fail: doAssert not fail, "File or directory not found: " & path
doAssert false, "File or directory not found: " & path result = ""
else:
return ""
proc walkDirImpl(indir, inext: string, file=true): seq[string] = proc walkDirImpl(indir, inext: string, file=true): seq[string] =
let let
@ -66,11 +64,21 @@ proc getFileDate(fullpath: string): string =
(result, ret) = gorgeEx(cmd) (result, ret) = gorgeEx(cmd)
if ret != 0: doAssert ret == 0, "File date error: " & fullpath & "\n" & result
doAssert false, "File date error: " & fullpath & "\n" & result
proc getToastError(output: string): string =
# Filter out preprocessor errors
for line in output.splitLines():
if "fatal error:" in line.toLowerAscii:
result &= &"\nERROR: {line.split(\"fatal error\")[1]}\n"
# Toast error
if result.len == 0:
result = output
proc getToast(fullpath: string, recurse: bool = false): string = proc getToast(fullpath: string, recurse: bool = false): string =
var var
ret = 0
cmd = when defined(Windows): "cmd /c " else: "" cmd = when defined(Windows): "cmd /c " else: ""
cmd &= "toast --pnim --preprocess " cmd &= "toast --pnim --preprocess "
@ -86,9 +94,8 @@ proc getToast(fullpath: string, recurse: bool = false): string =
cmd.add &"{fullpath.quoteShell}" cmd.add &"{fullpath.quoteShell}"
echo cmd echo cmd
var (output, exitCode) = gorgeEx(cmd, cache=getFileDate(fullpath)) (result, ret) = gorgeEx(cmd, cache=getFileDate(fullpath))
doAssert exitCode == 0, $exitCode doAssert ret == 0, getToastError(result)
result = output
proc getGccPaths*(mode = "c"): string = proc getGccPaths*(mode = "c"): string =
var var

View file

@ -78,17 +78,14 @@ proc process(path: string) =
else: else:
gStateRT.code = readFile(path) gStateRT.code = readFile(path)
doAssert gStateRT.code.len != 0, "Empty file or preprocessor error"
if gStateRT.mode == "c": if gStateRT.mode == "c":
if not parser.tsParserSetLanguage(treeSitterC()): doAssert parser.tsParserSetLanguage(treeSitterC()), "Failed to load C parser"
echo "Failed to load C parser"
quit()
elif gStateRT.mode == "cpp": elif gStateRT.mode == "cpp":
if not parser.tsParserSetLanguage(treeSitterCpp()): doAssert parser.tsParserSetLanguage(treeSitterCpp()), "Failed to load C++ parser"
echo "Failed to load C++ parser"
quit()
else: else:
echo "Invalid parser " & gStateRT.mode doAssert false, "Invalid parser " & gStateRT.mode
quit()
var var
tree = parser.tsParserParseString(nil, gStateRT.code.cstring, gStateRT.code.len.uint32) tree = parser.tsParserParseString(nil, gStateRT.code.cstring, gStateRT.code.len.uint32)