Skip to content

Commit 4bdb4a8

Browse files
committed
Log the details of the editor when the HTML rappressentation of the editor is not availavle in case of crashes
1 parent 2908241 commit 4bdb4a8

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecExceptionHandler.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.wordpress.aztec
22

33
import org.wordpress.android.util.AppLog
4+
import org.wordpress.aztec.util.AztecLog
45
import java.lang.Thread.UncaughtExceptionHandler
56

67
class AztecExceptionHandler(private val logHelper: ExceptionHandlerHelper?, private val visualEditor: AztecText) : UncaughtExceptionHandler {
@@ -31,7 +32,8 @@ class AztecExceptionHandler(private val logHelper: ExceptionHandlerHelper?, priv
3132
try {
3233
AppLog.e(AppLog.T.EDITOR, "HTML Content of Aztec Editor before the crash " + visualEditor.toPlainHtml(false))
3334
} catch (e: Throwable) {
34-
AppLog.e(AppLog.T.EDITOR, "Visual Content of Aztec Editor before the crash " + visualEditor.text)
35+
AppLog.e(AppLog.T.EDITOR, "HTML Content of Aztec Editor before the crash is unavailable, log the details instead")
36+
AztecLog.logEditorContentDetails(visualEditor)
3537
}
3638
}
3739

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
10471047
output = SpannableStringBuilder(text)
10481048
} catch (e: java.lang.ArrayIndexOutOfBoundsException) {
10491049
// FIXME: Remove this log once we've data to replicate the issue, and fix it in some way.
1050-
AppLog.e(AppLog.T.EDITOR, "There was an error creating SpannableStringBuilder. See #452 for details. " +
1051-
"Following is the text that caused the issue " + text)
1050+
AppLog.e(AppLog.T.EDITOR, "There was an error creating SpannableStringBuilder. See #452 for details.")
1051+
// No need to log the exception here. There is the ExceptionHandler that does this for us.
10521052
throw e
10531053
}
10541054

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
package org.wordpress.aztec.util
22

3+
import org.json.JSONArray
4+
import org.json.JSONException
5+
import org.json.JSONObject
6+
import org.wordpress.android.util.AppLog
7+
import org.wordpress.aztec.AztecText
8+
import org.wordpress.aztec.spans.IAztecAttributedSpan
9+
310
class AztecLog {
411
interface ExternalLogger {
512
fun log(message : String)
613
fun logException(tr : Throwable)
714
fun logException(tr : Throwable, message : String)
815
}
16+
17+
companion object {
18+
fun logEditorContentDetails(aztecText: AztecText) {
19+
try {
20+
val logContentJSON = JSONObject()
21+
logContentJSON.put("content", aztecText.text.toString())
22+
logContentJSON.put("length", aztecText.text.length)
23+
val spansJSON = JSONArray()
24+
val spans = aztecText.text.getSpans(0, aztecText.text.length, IAztecAttributedSpan::class.java)
25+
spans.forEach {
26+
val currenSpanJSON = JSONObject()
27+
currenSpanJSON.put("clasz", it.javaClass.name)
28+
currenSpanJSON.put("start", aztecText.text.getSpanStart(it))
29+
currenSpanJSON.put("end", aztecText.text.getSpanEnd(it))
30+
currenSpanJSON.put("flags", aztecText.text.getSpanFlags(it))
31+
spansJSON.put(currenSpanJSON)
32+
}
33+
logContentJSON.put("spans", spansJSON)
34+
AppLog.d(AppLog.T.EDITOR, "Below are the details of the content in the editor:")
35+
AppLog.d(AppLog.T.EDITOR, logContentJSON.toString())
36+
} catch (e: JSONException) {
37+
AppLog.e(AppLog.T.EDITOR, "Uhh ohh! There was an error logging the content of the Editor. This should" +
38+
"never happen.", e)
39+
}
40+
}
41+
}
942
}

0 commit comments

Comments
 (0)