| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library trydart.iframe_error_handler; | |
| 6 | |
| 7 // TODO(ahe): Remove this import if issue 17936 is fixed. | |
| 8 import 'dart:js' as hack; | |
| 9 | |
| 10 import 'dart:html' show | |
| 11 Event, | |
| 12 IFrameElement, | |
| 13 window; | |
| 14 | |
| 15 import 'dart:async' show | |
| 16 Stream, | |
| 17 StreamController; | |
| 18 | |
| 19 class ErrorMessage { | |
| 20 final String message; | |
| 21 final String filename; | |
| 22 final num lineno; | |
| 23 final num colno; | |
| 24 final String stack; | |
| 25 | |
| 26 ErrorMessage( | |
| 27 this.message, this.filename, this.lineno, this.colno, this.stack); | |
| 28 | |
| 29 String toString() { | |
| 30 String result = filename == null ? '' : filename; | |
| 31 if (lineno != null && lineno != 0 && !lineno.isNaN) { | |
| 32 result += ':$lineno'; | |
| 33 } | |
| 34 if (colno != null && colno != 0 && !colno.isNaN) { | |
| 35 result += ':$colno'; | |
| 36 } | |
| 37 if (message != null && !message.isEmpty) { | |
| 38 if (!result.isEmpty) { | |
| 39 result += ': '; | |
| 40 } | |
| 41 result += message; | |
| 42 } | |
| 43 if (stack != null && !stack.isEmpty) { | |
| 44 if (!result.isEmpty) { | |
| 45 result += '\n'; | |
| 46 } | |
| 47 result += stack; | |
| 48 } | |
| 49 return result; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 Stream<ErrorMessage> errorStream(IFrameElement iframe) { | |
| 54 StreamController<ErrorMessage> controller = | |
| 55 new StreamController<ErrorMessage>(); | |
| 56 void onError( | |
| 57 String message, | |
| 58 String filename, | |
| 59 int lineno, | |
| 60 [int colno, | |
| 61 error]) { | |
| 62 // See: | |
| 63 // https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror | |
| 64 String stack; | |
| 65 if (error != null) { | |
| 66 var jsStack = error['stack']; | |
| 67 if (jsStack != null) { | |
| 68 stack = hack.context.callMethod('String', [jsStack]); | |
| 69 } | |
| 70 } | |
| 71 controller.add(new ErrorMessage(message, filename, lineno, colno, stack)); | |
| 72 } | |
| 73 | |
| 74 void installErrorHandler() { | |
| 75 // This method uses dart:js to install an error event handler on the content | |
| 76 // window of [iframe]. This is a workaround for http://dartbug.com/17936. | |
| 77 var iframeProxy = new hack.JsObject.fromBrowserObject(iframe); | |
| 78 var contentWindowProxy = iframeProxy['contentWindow']; | |
| 79 if (contentWindowProxy == null) { | |
| 80 String message = | |
| 81 'No contentWindow, call this method *after* adding iframe to' | |
| 82 ' document.'; | |
| 83 window.console.error(message); | |
| 84 throw message; | |
| 85 } | |
| 86 | |
| 87 // Note: we have two options, use "iframe.contentWindow.onerror = ..." or | |
| 88 // "iframe.contentWindow.addEventListener('error', ...)". The former seems | |
| 89 // to provide more details on both Chrome and Firefox (which provides no | |
| 90 // information at all in error events). | |
| 91 contentWindowProxy['onerror'] = onError; | |
| 92 } | |
| 93 iframe.onLoad.listen((Event event) => installErrorHandler()); | |
| 94 installErrorHandler(); | |
| 95 return controller.stream; | |
| 96 } | |
| OLD | NEW |