Add hints for NimbleErrors which help the user fix them.

This commit is contained in:
Dominik Picheta 2016-12-22 16:49:24 +01:00
commit 8f34336e91
4 changed files with 23 additions and 7 deletions

View file

@ -998,10 +998,10 @@ proc doAction(options: Options) =
let execResult = execTask(nimbleFile, options.action.command, options)
if not execResult.success:
writeHelp(false)
raise newException(NimbleError, "Could not find task $1 in $2" %
[options.action.command, nimbleFile])
raiseNimbleError(msg = "Could not find task $1 in $2" %
[options.action.command, nimbleFile],
hint = "Run `nimble --help` and/or `nimble tasks` for" &
" a list of possible commands.")
if execResult.command.normalize == "nop":
display("Warning:", "Using `setCommand 'nop'` is not necessary.", Warning,
@ -1027,14 +1027,18 @@ proc doAction(options: Options) =
when isMainModule:
var error = ""
var hint = ""
try:
parseCmdLine().doAction()
except NimbleError:
error = getCurrentExceptionMsg()
let err = (ref NimbleError)(getCurrentException())
error = err.msg
when not defined(release):
let stackTrace = getStackTrace(getCurrentException())
error = stackTrace & "\n\n" & error
if not err.isNil:
hint = err.hint
except NimbleQuit:
nil
finally:
@ -1043,4 +1047,6 @@ when isMainModule:
if error.len > 0:
displayTip()
display("Error:", error, Error, HighPriority)
if hint.len > 0:
display("Hint:", hint, Warning, HighPriority)
quit(1)

View file

@ -40,5 +40,10 @@ when not defined(nimscript):
## Same as quit(QuitSuccess), but allows cleanup.
NimbleQuit* = ref object of Exception
proc raiseNimbleError*(msg: string, hint = "") =
var exc = newException(NimbleError, msg)
exc.hint = hint
raise exc
const
nimbleVersion* = "0.7.11"

View file

@ -241,6 +241,8 @@ proc getInstalledPkgs*(libsDir: string, options: Options):
const
readErrorMsg = "Installed package $1 v$2 is outdated or corrupt."
validationErrorMsg = readErrorMsg & "\nPackage did not pass validation: $3"
hintMsg = "The corrupted package will need to be removed manually. To fix" &
" this error message, remove $1."
proc createErrorMsg(tmplt, path, msg: string): string =
let (name, version) = getNameVersion(path)
@ -261,14 +263,16 @@ proc getInstalledPkgs*(libsDir: string, options: Options):
except ValidationError:
let exc = (ref ValidationError)(getCurrentException())
exc.msg = createErrorMsg(validationErrorMsg, path, exc.msg)
exc.hint = hintMsg % path
if exc.warnInstalled:
display("Warning:", exc.msg, Warning, HighPriority)
else:
raise exc
except:
let exc = getCurrentException()
let tmplt = readErrorMsg & "\nMore info: $3"
exc.msg = createErrorMsg(tmplt, path, exc.msg)
let msg = createErrorMsg(tmplt, path, getCurrentException().msg)
var exc = newException(NimbleError, msg)
exc.hint = hintMsg % path
raise exc
proc isNimScript*(nf: string, options: Options): bool =

View file

@ -34,6 +34,7 @@ type
ParseVersionError* = object of ValueError
NimbleError* = object of Exception
hint*: string
proc newVersion*(ver: string): Version = return Version(ver)
proc newSpecial*(spe: string): Special = return Special(spe)