Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consistent error logging #851

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ let
colorStdout* = shouldUseColor(stdout)
colorStderr* = shouldUseColor(stderr)

proc showError*(s: string, writeHelp = true) =
proc showError* (s: string, writeHelp = true) {.noreturn.} =
const errorPrefix = "Error: "
if colorStderr:
stderr.styledWrite(fgRed, errorPrefix)
Expand Down
25 changes: 10 additions & 15 deletions src/create/create.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import "."/[approaches, articles]
proc create*(conf: Conf) =
if conf.action.kind == actCreate:
if conf.action.exerciseCreate.len == 0:
let msg = "Please specify an exercise, using --exercise <slug>"
stderr.writeLine msg
quit 1
let msg = "please specify an exercise, using --exercise <slug>"
showError(msg)
if conf.action.approachSlug.len > 0:
if conf.action.articleSlug.len > 0:
let msg = &"Both --approach and --article were provided. Please specify only one."
stderr.writeLine msg
quit 1
let msg = &"both --approach and --article were provided. Please specify only one."
showError(msg)
let trackConfigPath = conf.trackDir / "config.json"
let trackConfig = parseFile(trackConfigPath, TrackConfig)
let trackExerciseSlugs = getSlugs(trackConfig.exercises, conf, trackConfigPath)
Expand All @@ -24,11 +22,10 @@ proc create*(conf: Conf) =
elif userExercise in trackExerciseSlugs.practice:
conf.trackDir / "exercises" / "practice" / $userExercise
else:
let msg = &"The `-e, --exercise` option was used to specify an " &
let msg = &"the `-e, --exercise` option was used to specify an " &
&"exercise slug, but `{userExercise}` is not an slug in the " &
&"track config:\n{trackConfigPath}"
stderr.writeLine msg
quit 1
showError(msg)

createApproach(Slug(conf.action.approachSlug), userExercise, exerciseDir)
elif conf.action.articleSlug.len > 0:
Expand All @@ -43,16 +40,14 @@ proc create*(conf: Conf) =
elif userExercise in trackExerciseSlugs.practice:
conf.trackDir / "exercises" / "practice" / $userExercise
else:
let msg = &"The `-e, --exercise` option was used to specify an " &
let msg = &"the `-e, --exercise` option was used to specify an " &
&"exercise slug, but `{userExercise}` is not an slug in the " &
&"track config:\n{trackConfigPath}"
stderr.writeLine msg
quit 1
showError(msg)

createArticle(Slug(conf.action.articleSlug), userExercise, exerciseDir)
else:
let msg = "Please specify `--article <slug>` or `--approach <slug>`"
stderr.writeLine msg
quit 1
let msg = "please specify `--article <slug>` or `--approach <slug>`"
showError(msg)
else:
quit 1
6 changes: 6 additions & 0 deletions src/logger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ template withLevel*(verbosity: Verbosity, body: untyped): untyped =
body
consoleLogger.levelThreshold = currentLevel

template logError*(msgs: varargs[string]) =
consoleLogger.useStderr = true
error(msgs)

template logNormal*(msgs: varargs[string]) =
consoleLogger.useStderr = false
notice(msgs)

template logDetailed*(msgs: varargs[string]) =
consoleLogger.useStderr = false
info(msgs)