   else
     throw new Error "Can't install on non-function"
 
-install = ->
+wrapFunctionWithErrorHandler = (fn) ->
+  trixDebugWrapper = ->
+    try
+      fn.apply(this, arguments)
+    catch error
+      reportError(error)
+      throw error
+  trixDebugWrapper
+
+reportError = (error) ->
+  Trix.Debugger.lastError = error
+
+  console.error "Trix error!"
+  console.log error.stack
+
+  activeElement = document.activeElement
+  editorElement = findClosestElementFromNode(activeElement, matchingSelector: "trix-editor")
+
+  if editorElement
+    notifyErrorListeners(error, editorElement)
+  else
+    console.warn "Can't find <trix-editor> element. document.activeElement =", activeElement
+
+notifyErrorListeners = (error, element) ->
+  for listener in errorListeners
+    try listener(error, element)
+
+do ->
   console.groupCollapsed("Trix debugger")
 
   for className, methodNames of DEBUG_METHODS

        console.warn "✗ #{className}##{methodName}:", error.message

  console.groupEnd()

install()

    didKeyDownDialogInput
  "

{findClosestElementFromNode} = Trix

errorListeners = []

Trix.Debugger =
  addErrorListener: (listener) ->
    unless listener in errorListeners
      errorListeners.push(listener)

  removeErrorListener: (listener) ->
    errorListeners = (l for l in errorListeners when l isnt listener)

installMethodDebugger = (className, methodName) ->
  [objectName, constructorNames...] = className.split(".")
