Exceptions: Raising code uses proper constructers now

This commit is contained in:
Sekulum Forka 2021-05-13 15:44:07 +02:00
parent 43ac1b6e04
commit 5116918ff2
1 changed files with 9 additions and 19 deletions

View File

@ -45,7 +45,7 @@ proc newShellException(msg: string, line=0, col=0, whileExec=""): ref ShellError
return e
proc newShellVariableException(msg: string, vari: string, line=0, col=0, whileExec=""): ref ShellVariableError =
var e= newException(ShellVariableError, msg)
var e= newException(ShellVariableError, vari & ": " & msg)
e.variable =vari
e.line=line
e.col=col
@ -53,7 +53,7 @@ proc newShellVariableException(msg: string, vari: string, line=0, col=0, whileEx
return e
proc newShellCommandException(msg: string, command: string, line=0, col=0, whileExec=""): ref ShellCommandError =
var e=newException(ShellCommandError, msg)
var e=newException(ShellCommandError, command & ": " & msg)
e.command=command
e.line=line
e.col=col
@ -186,9 +186,7 @@ proc evalVariable(vari: string): string =
elif envVariables.hasKey(vari):
result = envVariables[vari]
else:
var e = newException(ShellVariableError, "No such variable: " & vari)
e.variable = vari
raise e
raise newShellVariableException("No such variable", vari)
# substitute does a substitution on the stream passed in
proc substitute(strm: Stream, delim=";\p"): string =
@ -243,21 +241,17 @@ proc runBuiltin(builtin: string, args: openArray[string]): string =
case builtin:
of "set":
if args.len < 2:
var e = newException(ShellCommandError, "Set: not enough arguments provided")
e.command="set"
raise e
raise newShellCommandException("Not enough arguments provided", "set")
shellVariables[args[0]] = args[1..^1].join(" ")
return ""
of "setenv":
if args.len < 2:
var e = newException(ShellCommandError, "Setenv: not enough arguments provided")
e.command="setenv"
raise e
raise newShellCommandException("Not enough arguments provided", "setenv")
try:
envVariables[args[0]] = args[1..^1].join(":")
putEnv(args[0], args[1..^1].join(":"))
except OSError as e:
raise newException(Exception, "setenv: " & e.msg)
raise newShellCommandException(e.msg, "setenv")
return ""
of "echo":
if args[0] == "-n":
@ -283,7 +277,7 @@ proc runBuiltin(builtin: string, args: openArray[string]): string =
stdout.write(args.join(" "), "\p")
quit(0)
else:
raise newException(Exception, "Fsh: No such builtin implemented")
raise newShellCommandException("No such builtin implemented", builtin)
# executes executes the command. For now it involves catting the command and printing it
proc execute(cmd: string): int =
@ -303,9 +297,7 @@ proc execute(cmd: string): int =
result = ps.waitForExit
ps.close
except OSError as e:
var e = newException(ShellCommandError, progname & ": command not found")
e.command = progname
raise e
raise newShellCommandException("No such command", progname)
strm.close
# eval evaluates the given string
@ -325,9 +317,7 @@ proc eval(cmd: string): string =
result=ps.outputStream.readAll
ps.close
except OSError as e:
var e = newException(ShellCommandError, progname & ": Command not found")
e.command=progname
raise e
raise newShellCommandException("No such command", progname)
strm.close
when isMainModule: