Skip to content

Commit

Permalink
util/util-app: Include the exception in the error message printed whe…
Browse files Browse the repository at this point in the history
…n the application encounters an error on startup

Problem

When the application exits prematurely due to an exception encountered on startup, this
exception is not printed to stderr, only the message "Exception thrown in main on startup".
The exception stack trace is printed to stdout, but this is unintuitive for the user.

Solution

Include the exception and stack trace in the exception printed to stderr.

Differential Revision: https://phabricator.twitter.biz/D1116753
  • Loading branch information
jcrossley authored and jenkins committed Dec 21, 2023
1 parent 69b940a commit 644094e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Note that ``PHAB_ID=#`` and ``RB_ID=#`` correspond to associated messages in com
Unreleased
----------

Runtime Behavior Changes
~~~~~~~~~~~~~~~~~~~~~~~~

* util-app: When the application exits due to an error on startup, the error and
and stack trace are printed to stderr in addition to the existing stdout. ``PHAB_ID=D1116753``


23.11.0
-------

Expand Down
1 change: 0 additions & 1 deletion PROJECT.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ owners:
- csl-team:ldap
- jillianc
- mbezoyan
- nvong
10 changes: 9 additions & 1 deletion util-app/src/main/scala/com/twitter/app/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.twitter.app
import com.twitter.app.lifecycle.Event._
import com.twitter.conversions.DurationOps._
import com.twitter.util._
import java.io.PrintWriter
import java.io.StringWriter
import java.lang.reflect.InvocationTargetException
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicReference
Expand Down Expand Up @@ -82,7 +84,13 @@ trait App extends ClosableOnce with CloseOnceAwaitably with Lifecycle {
System.err.println(throwable.getMessage)
System.exit(1)
case _ =>
exitOnError("Exception thrown in main on startup")
util.Using(new StringWriter) { stringWriter =>
util.Using(new PrintWriter(stringWriter)) { printWriter =>
throwable.printStackTrace(printWriter)
printWriter.flush()
exitOnError(s"Exception thrown in main on startup: ${stringWriter.toString}")
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion util-app/src/test/scala/com/twitter/app/AppTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AppTest extends AnyFunSuite {

test1.main(Array())

assert(test1.reason.contains("Exception thrown in main on startup"))
assert(test1.reason.exists(_.contains("Exception thrown in main on startup")))
}

test("App: propagate underlying exception from fields in app") {
Expand Down

0 comments on commit 644094e

Please sign in to comment.