Skip to content

Instantly share code, notes, and snippets.

@dlew
Created March 1, 2016 20:46
Show Gist options
  • Save dlew/0dc95faa2d3eb923433d to your computer and use it in GitHub Desktop.
Save dlew/0dc95faa2d3eb923433d to your computer and use it in GitHub Desktop.
Automated onError() message generation
public static Action1<Throwable> crashOnError() {
final Throwable checkpoint = new Throwable();
return throwable -> {
StackTraceElement[] stackTrace = checkpoint.getStackTrace();
StackTraceElement element = stackTrace[1]; // First element after `crashOnError()`
String msg = String.format("onError() crash from subscribe() in %s.%s(%s:%s)",
element.getClassName(),
element.getMethodName(),
element.getFileName(),
element.getLineNumber());
throw new OnErrorNotImplementedException(msg, throwable);
};
}
@scompt
Copy link

scompt commented Apr 8, 2016

I thought about doing something like this but balked because you would have the pay the performance penalty of building the stack trace every time you call the method even if an error isn't emitted. I guess you think the debugability is worth the performance penalty?

@dlew
Copy link
Author

dlew commented Apr 8, 2016

Timber uses the same technique to determine the logging tag and I'm using it all over the place. (In fact, Timber is the inspiration for how this works.) If I'm alright with that, then I definitely am alright with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment