Repository: wagtail/wagtail

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.



Discussion:
Confirmed on latest master.

This was failing on my development copy because I had an invalid EMBEDLY_KEY in my settings. Embeds are working fine for me on a fresh install.

Which reveals that there is in fact a bug here: error messages are not being returned back to the user here. We'll need to fix this first before we know whether the embed is failing for an 'expected' reason (e.g. a misconfiguration like mine, a network error, or simply that your URL is not recognised as one of the embed providers on our list - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailembeds/oembed_providers.py) or for an unexpected reason that requires further investigation.

I am sure it was because URL was not on the list of recognised embed providers because embeds from youtube and soundcloud works just fine. 
Didn't know that list exists.

Confirmed on latest master.

This was failing on my development copy because I had an invalid EMBEDLY_KEY in my settings. Embeds are working fine for me on a fresh install.

Which reveals that there is in fact a bug here: error messages are not being returned back to the user here. We'll need to fix this first before we know whether the embed is failing for an 'expected' reason (e.g. a misconfiguration like mine, a network error, or simply that your URL is not recognised as one of the embed providers on our list - https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailembeds/oembed_providers.py) or for an unexpected reason that requires further investigation.

I am sure it was because URL was not on the list of recognised embed providers because embeds from youtube and soundcloud works just fine. 
Didn't know that list exists.


Resolving patch:
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 @@ def embed_to_frontend_html(url):
             '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 @@ def expand_db_attributes(attrs, for_editor):
         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()
 


Test patch:
diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py
--- a/wagtail/wagtailembeds/tests.py
+++ b/wagtail/wagtailembeds/tests.py
@@ -12,6 +12,7 @@
 from django import template
 from django.test import TestCase
 from django.core.exceptions import ValidationError
+from django.core.urlresolvers import reverse
 from django.utils.six.moves.urllib.error import URLError
 
 from wagtail.wagtailcore import blocks
@@ -112,7 +113,28 @@ def test_chooser(self):
         r = self.client.get('/admin/embeds/chooser/')
         self.assertEqual(r.status_code, 200)
 
-        # TODO: Test submitting
+    @patch('wagtail.wagtailembeds.embeds.get_embed')
+    def test_submit_valid_embed(self, get_embed):
+        get_embed.return_value = Embed(html='<img src="http://www.example.com" />', title="An example embed")
+
+        response = self.client.post(reverse('wagtailembeds:chooser_upload'), {
+            'url': 'http://www.example.com/'
+        })
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, """modal.respond('embedChosen'""")
+        self.assertContains(response, """An example embed""")
+
+    @patch('wagtail.wagtailembeds.embeds.get_embed')
+    def test_submit_unrecognised_embed(self, get_embed):
+        get_embed.side_effect = EmbedNotFoundException
+
+        response = self.client.post(reverse('wagtailembeds:chooser_upload'), {
+            'url': 'http://www.example.com/'
+        })
+        self.assertEqual(response.status_code, 200)
+        self.assertNotContains(response, """modal.respond('embedChosen'""")
+        self.assertContains(response, """Cannot find an embed for this URL.""")
+
 
 class TestEmbedly(TestCase):
     @unittest.skipIf(no_embedly, "Embedly is not installed")