How to avoid stack trace with sys.error(message)

184 views Asked by At

In some custom tasks, I detect some error, and I'm using this way to signal an error and stop processing current task:

sys.error("Some error")

Internally it throws a RunTimeException.

SBT shows a stack trace, and I'd like to report an error without stack trace. For expected errors, it is often verbose, not meaningful and confuses end users.

The stack trace is shown when running from the command line, not the shell, where the stack trace only is shown with the last command.

Is there a standard way of reporting errors with no stack trace?

UPDATE:

Asked to the SBT team for a better solution.

2

There are 2 answers

1
Eugene Yokota On BEST ANSWER

Is there a standard way of reporting errors with no stack trace?

Not yet. But there's an undocumented, not-guaranteed-to-work-in-the-future way.

Edit: As Dale commented sbt.internal.util.MessageOnlyException is aliased to sbt.MessageOnlyException, so we can use the following:

lazy val doomed = taskKey[Unit]("doomed")

ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / version      := "0.1.0-SNAPSHOT"

lazy val root = (project in file("."))
  .settings(
    name := "msg-only",
    doomed := {
      throw new MessageOnlyException("boom!")
    }
  )

This is not guaranteed to work in the future because it's using sbt.internal, but basically MessageOnlyException does what you are asking I think.

sbt:msg-only> doomed
[error] boom!
[error] (doomed) boom!
[error] Total time: 0 s, completed Nov 28, 2018 12:42:47 PM
1
marios On

Something like this should work, but it might hide other type of (unintentional) errors in your task? Maybe you can add the error to an error-task and then call that task each time you want to raise the error? Then you can set the traceLevel only on the error-task.

lazy val myTask = taskKey[Unit]("doomed to err")

traceLevel in myTask:= -1

myTask := {
    error("Some error")
}