We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Errors from embed chooser are not reported back to the user
It seems that embed functionality of wagtail hello.js editor is not working. I tried to embed a link, modal comes up, everything is fine but when I press "Insert" I get nothing and browser displays this error:

(Firefox) TypeError: a is undefined rangy-core.js:33:479
(Chrome) rangy-core.js:33 Uncaught TypeError: Cannot read property 'nodeType' of undefined

But network data says everything is OK:
POST .../admin/embeds/chooser/upload/ 200 OK
After closing the modal window, embed link is not present.

Errors from embed chooser are not reported back to the user
It seems that embed functionality of wagtail hello.js editor is not working. I tried to embed a link, modal comes up, everything is fine but when I press "Insert" I get nothing and browser displays this error:

(Firefox) TypeError: a is undefined rangy-core.js:33:479
(Chrome) rangy-core.js:33 Uncaught TypeError: Cannot read property 'nodeType' of undefined

But network data says everything is OK:
POST .../admin/embeds/chooser/upload/ 200 OK
After closing the modal window, embed link is not present.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `wagtail/wagtailembeds/views/chooser.py`
Content:
```
1 from django.forms.utils import ErrorList
2 from django.utils.translation import ugettext as _
3 
4 from wagtail.wagtailadmin.modal_workflow import render_modal_workflow
5 from wagtail.wagtailembeds.forms import EmbedForm
6 from wagtail.wagtailembeds.format import embed_to_editor_html
7 
8 from wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException
9 
10 
11 
12 def chooser(request):
13     form = EmbedForm()
14 
15     return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {
16         'form': form,
17     })
18 
19 
20 def chooser_upload(request):
21     if request.POST:
22         form = EmbedForm(request.POST, request.FILES)
23 
24         if form.is_valid():
25             error = None
26             try:
27                 embed_html = embed_to_editor_html(form.cleaned_data['url'])
28                 return render_modal_workflow(
29                     request, None, 'wagtailembeds/chooser/embed_chosen.js',
30                     {'embed_html': embed_html}
31                 )
32             except AccessDeniedEmbedlyException:
33                 error = _("There seems to be a problem with your embedly API key. Please check your settings.")
34             except EmbedNotFoundException:
35                 error = _("Cannot find an embed for this URL.")
36             except EmbedlyException:
37                 error = _("There seems to be an error with Embedly while trying to embed this URL. Please try again later.")
38 
39             if error:
40                 errors = form._errors.setdefault('url', ErrorList())
41                 errors.append(error)
42                 return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {
43                     'form': form,
44                 })
45     else:
46         form = EmbedForm()
47 
48     return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {
49         'form': form,
50     })
51
```

Path: `wagtail/wagtailembeds/rich_text.py`
Content:
```
1 from wagtail.wagtailembeds import format
2 
3 
4 class MediaEmbedHandler(object):
5     """
6     MediaEmbedHandler will be invoked whenever we encounter an element in HTML content
7     with an attribute of data-embedtype="media". The resulting element in the database
8     representation will be:
9     <embed embedtype="media" url="http://vimeo.com/XXXXX">
10     """
11     @staticmethod
12     def get_db_attributes(tag):
13         """
14         Given a tag that we've identified as a media embed (because it has a
15         data-embedtype="media" attribute), return a dict of the attributes we should
16         have on the resulting <embed> element.
17         """
18         return {
19             'url': tag['data-url'],
20         }
21 
22     @staticmethod
23     def expand_db_attributes(attrs, for_editor):
24         """
25         Given a dict of attributes from the <embed> tag, return the real HTML
26         representation.
27         """
28         if for_editor:
29             return format.embed_to_editor_html(attrs['url'])
30         else:
31             return format.embed_to_frontend_html(attrs['url'])
32
```

Path: `wagtail/wagtailembeds/format.py`
Content:
```
1 from __future__ import division  # Use true division
2 
3 from django.template.loader import render_to_string
4 
5 from wagtail.wagtailembeds import embeds
6 
7 
8 def embed_to_frontend_html(url):
9     try:
10         embed = embeds.get_embed(url)
11 
12         # Work out ratio
13         if embed.width and embed.height:
14             ratio = str(embed.height / embed.width * 100) + "%"
15         else:
16             ratio = "0"
17 
18         # Render template
19         return render_to_string('wagtailembeds/embed_frontend.html', {
20             'embed': embed,
21             'ratio': ratio,
22         })
23     except embeds.EmbedException:
24         return ''
25 
26 
27 def embed_to_editor_html(url):
28     try:
29         embed = embeds.get_embed(url)
30 
31         # Render template
32         return render_to_string('wagtailembeds/embed_editor.html', {
33             'embed': embed,
34         })
35     except embeds.EmbedException:
36         # Could be replaced with a nice error message
37         return ''
38
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/wagtail/wagtailembeds/format.py b/wagtail/wagtailembeds/format.py
--- a/wagtail/wagtailembeds/format.py
+++ b/wagtail/wagtailembeds/format.py
@@ -21,17 +21,15 @@
             'ratio': ratio,
         })
     except embeds.EmbedException:
+        # silently ignore failed embeds, rather than letting them crash the page
         return ''
 
 
 def embed_to_editor_html(url):
-    try:
-        embed = embeds.get_embed(url)
+    embed = embeds.get_embed(url)
+    # catching EmbedException is the responsibility of the caller
 
-        # Render template
-        return render_to_string('wagtailembeds/embed_editor.html', {
-            'embed': embed,
-        })
-    except embeds.EmbedException:
-        # Could be replaced with a nice error message
-        return ''
+    # Render template
+    return render_to_string('wagtailembeds/embed_editor.html', {
+        'embed': embed,
+    })
diff --git a/wagtail/wagtailembeds/rich_text.py b/wagtail/wagtailembeds/rich_text.py
--- a/wagtail/wagtailembeds/rich_text.py
+++ b/wagtail/wagtailembeds/rich_text.py
@@ -1,4 +1,4 @@
-from wagtail.wagtailembeds import format
+from wagtail.wagtailembeds import format, embeds
 
 
 class MediaEmbedHandler(object):
@@ -26,6 +26,10 @@
         representation.
         """
         if for_editor:
-            return format.embed_to_editor_html(attrs['url'])
+            try:
+                return format.embed_to_editor_html(attrs['url'])
+            except embeds.EmbedException:
+                # Could be replaced with a nice error message
+                return ''
         else:
             return format.embed_to_frontend_html(attrs['url'])
diff --git a/wagtail/wagtailembeds/views/chooser.py b/wagtail/wagtailembeds/views/chooser.py
--- a/wagtail/wagtailembeds/views/chooser.py
+++ b/wagtail/wagtailembeds/views/chooser.py
@@ -8,7 +8,6 @@
 from wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException
 
 
-
 def chooser(request):
     form = EmbedForm()

{"golden_diff": "diff --git a/wagtail/wagtailembeds/format.py b/wagtail/wagtailembeds/format.py\n--- a/wagtail/wagtailembeds/format.py\n+++ b/wagtail/wagtailembeds/format.py\n@@ -21,17 +21,15 @@\n             'ratio': ratio,\n         })\n     except embeds.EmbedException:\n+        # silently ignore failed embeds, rather than letting them crash the page\n         return ''\n \n \n def embed_to_editor_html(url):\n-    try:\n-        embed = embeds.get_embed(url)\n+    embed = embeds.get_embed(url)\n+    # catching EmbedException is the responsibility of the caller\n \n-        # Render template\n-        return render_to_string('wagtailembeds/embed_editor.html', {\n-            'embed': embed,\n-        })\n-    except embeds.EmbedException:\n-        # Could be replaced with a nice error message\n-        return ''\n+    # Render template\n+    return render_to_string('wagtailembeds/embed_editor.html', {\n+        'embed': embed,\n+    })\ndiff --git a/wagtail/wagtailembeds/rich_text.py b/wagtail/wagtailembeds/rich_text.py\n--- a/wagtail/wagtailembeds/rich_text.py\n+++ b/wagtail/wagtailembeds/rich_text.py\n@@ -1,4 +1,4 @@\n-from wagtail.wagtailembeds import format\n+from wagtail.wagtailembeds import format, embeds\n \n \n class MediaEmbedHandler(object):\n@@ -26,6 +26,10 @@\n         representation.\n         \"\"\"\n         if for_editor:\n-            return format.embed_to_editor_html(attrs['url'])\n+            try:\n+                return format.embed_to_editor_html(attrs['url'])\n+            except embeds.EmbedException:\n+                # Could be replaced with a nice error message\n+                return ''\n         else:\n             return format.embed_to_frontend_html(attrs['url'])\ndiff --git a/wagtail/wagtailembeds/views/chooser.py b/wagtail/wagtailembeds/views/chooser.py\n--- a/wagtail/wagtailembeds/views/chooser.py\n+++ b/wagtail/wagtailembeds/views/chooser.py\n@@ -8,7 +8,6 @@\n from wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException\n \n \n-\n def chooser(request):\n     form = EmbedForm()\n", "issue": "Errors from embed chooser are not reported back to the user\nIt seems that embed functionality of wagtail hello.js editor is not working. I tried to embed a link, modal comes up, everything is fine but when I press \"Insert\" I get nothing and browser displays this error:\n\n(Firefox) TypeError: a is undefined rangy-core.js:33:479\n(Chrome) rangy-core.js:33 Uncaught TypeError: Cannot read property 'nodeType' of undefined\n\nBut network data says everything is OK:\nPOST .../admin/embeds/chooser/upload/ 200 OK\nAfter closing the modal window, embed link is not present.\n\nErrors from embed chooser are not reported back to the user\nIt seems that embed functionality of wagtail hello.js editor is not working. I tried to embed a link, modal comes up, everything is fine but when I press \"Insert\" I get nothing and browser displays this error:\n\n(Firefox) TypeError: a is undefined rangy-core.js:33:479\n(Chrome) rangy-core.js:33 Uncaught TypeError: Cannot read property 'nodeType' of undefined\n\nBut network data says everything is OK:\nPOST .../admin/embeds/chooser/upload/ 200 OK\nAfter closing the modal window, embed link is not present.\n\n", "before_files": [{"content": "from django.forms.utils import ErrorList\nfrom django.utils.translation import ugettext as _\n\nfrom wagtail.wagtailadmin.modal_workflow import render_modal_workflow\nfrom wagtail.wagtailembeds.forms import EmbedForm\nfrom wagtail.wagtailembeds.format import embed_to_editor_html\n\nfrom wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException\n\n\n\ndef chooser(request):\n    form = EmbedForm()\n\n    return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {\n        'form': form,\n    })\n\n\ndef chooser_upload(request):\n    if request.POST:\n        form = EmbedForm(request.POST, request.FILES)\n\n        if form.is_valid():\n            error = None\n            try:\n                embed_html = embed_to_editor_html(form.cleaned_data['url'])\n                return render_modal_workflow(\n                    request, None, 'wagtailembeds/chooser/embed_chosen.js',\n                    {'embed_html': embed_html}\n                )\n            except AccessDeniedEmbedlyException:\n                error = _(\"There seems to be a problem with your embedly API key. Please check your settings.\")\n            except EmbedNotFoundException:\n                error = _(\"Cannot find an embed for this URL.\")\n            except EmbedlyException:\n                error = _(\"There seems to be an error with Embedly while trying to embed this URL. Please try again later.\")\n\n            if error:\n                errors = form._errors.setdefault('url', ErrorList())\n                errors.append(error)\n                return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {\n                    'form': form,\n                })\n    else:\n        form = EmbedForm()\n\n    return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {\n        'form': form,\n    })\n", "path": "wagtail/wagtailembeds/views/chooser.py"}, {"content": "from wagtail.wagtailembeds import format\n\n\nclass MediaEmbedHandler(object):\n    \"\"\"\n    MediaEmbedHandler will be invoked whenever we encounter an element in HTML content\n    with an attribute of data-embedtype=\"media\". The resulting element in the database\n    representation will be:\n    <embed embedtype=\"media\" url=\"http://vimeo.com/XXXXX\">\n    \"\"\"\n    @staticmethod\n    def get_db_attributes(tag):\n        \"\"\"\n        Given a tag that we've identified as a media embed (because it has a\n        data-embedtype=\"media\" attribute), return a dict of the attributes we should\n        have on the resulting <embed> element.\n        \"\"\"\n        return {\n            'url': tag['data-url'],\n        }\n\n    @staticmethod\n    def expand_db_attributes(attrs, for_editor):\n        \"\"\"\n        Given a dict of attributes from the <embed> tag, return the real HTML\n        representation.\n        \"\"\"\n        if for_editor:\n            return format.embed_to_editor_html(attrs['url'])\n        else:\n            return format.embed_to_frontend_html(attrs['url'])\n", "path": "wagtail/wagtailembeds/rich_text.py"}, {"content": "from __future__ import division  # Use true division\n\nfrom django.template.loader import render_to_string\n\nfrom wagtail.wagtailembeds import embeds\n\n\ndef embed_to_frontend_html(url):\n    try:\n        embed = embeds.get_embed(url)\n\n        # Work out ratio\n        if embed.width and embed.height:\n            ratio = str(embed.height / embed.width * 100) + \"%\"\n        else:\n            ratio = \"0\"\n\n        # Render template\n        return render_to_string('wagtailembeds/embed_frontend.html', {\n            'embed': embed,\n            'ratio': ratio,\n        })\n    except embeds.EmbedException:\n        return ''\n\n\ndef embed_to_editor_html(url):\n    try:\n        embed = embeds.get_embed(url)\n\n        # Render template\n        return render_to_string('wagtailembeds/embed_editor.html', {\n            'embed': embed,\n        })\n    except embeds.EmbedException:\n        # Could be replaced with a nice error message\n        return ''\n", "path": "wagtail/wagtailembeds/format.py"}], "after_files": [{"content": "from django.forms.utils import ErrorList\nfrom django.utils.translation import ugettext as _\n\nfrom wagtail.wagtailadmin.modal_workflow import render_modal_workflow\nfrom wagtail.wagtailembeds.forms import EmbedForm\nfrom wagtail.wagtailembeds.format import embed_to_editor_html\n\nfrom wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException\n\n\ndef chooser(request):\n    form = EmbedForm()\n\n    return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {\n        'form': form,\n    })\n\n\ndef chooser_upload(request):\n    if request.POST:\n        form = EmbedForm(request.POST, request.FILES)\n\n        if form.is_valid():\n            error = None\n            try:\n                embed_html = embed_to_editor_html(form.cleaned_data['url'])\n                return render_modal_workflow(\n                    request, None, 'wagtailembeds/chooser/embed_chosen.js',\n                    {'embed_html': embed_html}\n                )\n            except AccessDeniedEmbedlyException:\n                error = _(\"There seems to be a problem with your embedly API key. Please check your settings.\")\n            except EmbedNotFoundException:\n                error = _(\"Cannot find an embed for this URL.\")\n            except EmbedlyException:\n                error = _(\"There seems to be an error with Embedly while trying to embed this URL. Please try again later.\")\n\n            if error:\n                errors = form._errors.setdefault('url', ErrorList())\n                errors.append(error)\n                return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {\n                    'form': form,\n                })\n    else:\n        form = EmbedForm()\n\n    return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', {\n        'form': form,\n    })\n", "path": "wagtail/wagtailembeds/views/chooser.py"}, {"content": "from wagtail.wagtailembeds import format, embeds\n\n\nclass MediaEmbedHandler(object):\n    \"\"\"\n    MediaEmbedHandler will be invoked whenever we encounter an element in HTML content\n    with an attribute of data-embedtype=\"media\". The resulting element in the database\n    representation will be:\n    <embed embedtype=\"media\" url=\"http://vimeo.com/XXXXX\">\n    \"\"\"\n    @staticmethod\n    def get_db_attributes(tag):\n        \"\"\"\n        Given a tag that we've identified as a media embed (because it has a\n        data-embedtype=\"media\" attribute), return a dict of the attributes we should\n        have on the resulting <embed> element.\n        \"\"\"\n        return {\n            'url': tag['data-url'],\n        }\n\n    @staticmethod\n    def expand_db_attributes(attrs, for_editor):\n        \"\"\"\n        Given a dict of attributes from the <embed> tag, return the real HTML\n        representation.\n        \"\"\"\n        if for_editor:\n            try:\n                return format.embed_to_editor_html(attrs['url'])\n            except embeds.EmbedException:\n                # Could be replaced with a nice error message\n                return ''\n        else:\n            return format.embed_to_frontend_html(attrs['url'])\n", "path": "wagtail/wagtailembeds/rich_text.py"}, {"content": "from __future__ import division  # Use true division\n\nfrom django.template.loader import render_to_string\n\nfrom wagtail.wagtailembeds import embeds\n\n\ndef embed_to_frontend_html(url):\n    try:\n        embed = embeds.get_embed(url)\n\n        # Work out ratio\n        if embed.width and embed.height:\n            ratio = str(embed.height / embed.width * 100) + \"%\"\n        else:\n            ratio = \"0\"\n\n        # Render template\n        return render_to_string('wagtailembeds/embed_frontend.html', {\n            'embed': embed,\n            'ratio': ratio,\n        })\n    except embeds.EmbedException:\n        # silently ignore failed embeds, rather than letting them crash the page\n        return ''\n\n\ndef embed_to_editor_html(url):\n    embed = embeds.get_embed(url)\n    # catching EmbedException is the responsibility of the caller\n\n    # Render template\n    return render_to_string('wagtailembeds/embed_editor.html', {\n        'embed': embed,\n    })\n", "path": "wagtail/wagtailembeds/format.py"}]}