[swift-users] intercept fatalError() or other termination messages from swift?

David Baraff davidbaraff at gmail.com
Fri Jun 23 23:13:30 CDT 2017

I realize this is slightly centric to iOS, but it irks me that both Apple’s crash report logs and popular platforms like PLCrashReporter can do the hard stuff like give you a stack trace, but are *completely* unable to display the error message from terminating a program via fatalError(), or the error message from, e.g. dying with a bad optional.

Is there *any* to intercept the error messages that from fatalError() and similar like things in swift (bad optionals, invalid array accesses, assertions)?  I would think that some sort of a “hook” into these standard error routines would be a good thing.

In my case, if I could simply save that darn error string in a file, i could pick it up when the app next launches and report it along with the rest of the info like the stack/signal, etc.

I’ve been looking through the code in stdlib/public/runtime/Errors.cpp but haven’t found anything promising that lets me jump in there.  In my code, I’m likely to write things like
	guard let x = … else {
		fatalError(“Data type has payload <T> but is hooked to UI control with intrinsic type <U>”)

and having that exact string tells me precisely what’s going, far simpler than a stack trace.

More information about the swift-users mailing list