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
result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
if not fileExists(result) and not dirExists(result):
if fail:
doAssert false, "File or directory not found: " & path
else:
return ""
doAssert not fail, "File or directory not found: " & path
result = ""
proc walkDirImpl(indir, inext: string, file=true): seq[string] =
let
@ -66,11 +64,21 @@ proc getFileDate(fullpath: string): string =
(result, ret) = gorgeEx(cmd)
if ret != 0:
doAssert false, "File date error: " & fullpath & "\n" & result
doAssert ret == 0, "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 =
var
ret = 0
cmd = when defined(Windows): "cmd /c " else: ""
cmd &= "toast --pnim --preprocess "
@ -86,9 +94,8 @@ proc getToast(fullpath: string, recurse: bool = false): string =
cmd.add &"{fullpath.quoteShell}"
echo cmd
var (output, exitCode) = gorgeEx(cmd, cache=getFileDate(fullpath))
doAssert exitCode == 0, $exitCode
result = output
(result, ret) = gorgeEx(cmd, cache=getFileDate(fullpath))
doAssert ret == 0, getToastError(result)
proc getGccPaths*(mode = "c"): string =
var

View file

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