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