diff --git a/core/core.services.yml b/core/core.services.yml
index 08b422c..d78b38f 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1543,7 +1543,7 @@ services:
       - { name: needs_destruction }
   library.discovery.parser:
     class: Drupal\Core\Asset\LibraryDiscoveryParser
-    arguments: ['@app.root', '@module_handler', '@theme.manager']
+    arguments: ['@app.root', '@module_handler', '@theme.manager', '@theme_handler']
   library.dependency_resolver:
     class: Drupal\Core\Asset\LibraryDependencyResolver
     arguments: ['@library.discovery']
diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
index 799ee07..a088a8b 100644
--- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
+++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
@@ -11,6 +11,7 @@
 use Drupal\Core\Theme\ThemeManagerInterface;
 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
 use Drupal\Component\Utility\NestedArray;
+use Drupal\Core\Extension\ThemeHandlerInterface;
 
 /**
  * Parses library files to get extension data.
@@ -32,6 +33,13 @@ class LibraryDiscoveryParser {
   protected $themeManager;
 
   /**
+   * The theme handler.
+   *
+   * @var \Drupal\Core\Extension\ThemeHandlerInterface
+   */
+  protected $themeHandler;
+
+  /**
    * The app root.
    *
    * @var string
@@ -47,11 +55,14 @@ class LibraryDiscoveryParser {
    *   The module handler.
    * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
    *   The theme manager.
+   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
+   *   The theme handler.
    */
-  public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
+  public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, ThemeHandlerInterface $theme_handler) {
     $this->root = $root;
     $this->moduleHandler = $module_handler;
     $this->themeManager = $theme_manager;
+    $this->themeHandler = $theme_handler;
   }
 
   /**
@@ -63,6 +74,8 @@ public function __construct($root, ModuleHandlerInterface $module_handler, Theme
    * @return array
    *   All library definitions of the passed extension.
    *
+   * @return NULL | @throws \AssertionError
+   *   When the extension (theme, module, library) is not available
    * @throws \Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException
    *   Thrown when a library has no js/css/setting.
    * @throws \UnexpectedValueException
@@ -79,9 +92,15 @@ public function buildByExtension($extension) {
       if ($this->moduleHandler->moduleExists($extension)) {
         $extension_type = 'module';
       }
-      else {
+      elseif ($this->themeHandler->themeExists($extension)) {
         $extension_type = 'theme';
       }
+      else {
+        $message = sprintf('The extension "%s" is not available.', $extension);
+        assert(false, $message);
+        trigger_error($message, E_USER_WARNING);
+        return;
+      }
       $path = $this->drupalGetPath($extension_type, $extension);
     }
 
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
index e059405..7339db8 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
@@ -53,7 +53,7 @@ public function testDrupalSettingsCachingRegression() {
     $session = $this->getSession();
 
     // Insert a fake library into the already loaded library settings.
-    $fake_library = 'fakeLibrary/fakeLibrary';
+    $fake_library = 'core/fakeLibrary';
     $session->evaluateScript("drupalSettings.ajaxPageState.libraries = drupalSettings.ajaxPageState.libraries + ',$fake_library';");
 
     $libraries = $session->evaluateScript('drupalSettings.ajaxPageState.libraries');
diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php
index 121aebc..b4186a5 100644
--- a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php
+++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php
@@ -45,6 +45,13 @@ class LibraryDiscoveryParserTest extends UnitTestCase {
   protected $themeManager;
 
   /**
+   * The mocked theme handler.
+   *
+   * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $themeHandler;
+
+  /**
    * The mocked lock backend.
    *
    * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -59,6 +66,8 @@ protected function setUp() {
 
     $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
     $this->themeManager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
+    $this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
+
     $mock_active_theme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
       ->disableOriginalConstructor()
       ->getMock();
@@ -68,7 +77,7 @@ protected function setUp() {
     $this->themeManager->expects($this->any())
       ->method('getActiveTheme')
       ->willReturn($mock_active_theme);
-    $this->libraryDiscoveryParser = new TestLibraryDiscoveryParser($this->root, $this->moduleHandler, $this->themeManager);
+    $this->libraryDiscoveryParser = new TestLibraryDiscoveryParser($this->root, $this->moduleHandler, $this->themeManager, $this->themeHandler);
   }
 
   /**
@@ -109,6 +118,11 @@ public function testBuildByExtensionWithTheme() {
       ->with('example_theme')
       ->will($this->returnValue(FALSE));
 
+    $this->themeHandler->expects($this->atLeastOnce())
+      ->method('themeExists')
+      ->with('example_theme')
+      ->will($this->returnValue(TRUE));
+
     $path = __DIR__ . '/library_test_files';
     $path = substr($path, strlen($this->root) + 1);
     $this->libraryDiscoveryParser->setPaths('theme', 'example_theme', $path);
@@ -161,6 +175,35 @@ public function testInvalidLibrariesFile() {
   }
 
   /**
+   * Tests that an exception is thrown when the extension is not a valid
+   * module, theme or library.
+   *
+   * @covers ::buildByExtension
+   */
+  public function testMissingExtension() {
+    $this->moduleHandler->expects($this->atLeastOnce())
+      ->method('moduleExists')
+      ->with('missing_extension')
+      ->will($this->returnValue(FALSE));
+
+    $this->themeHandler->expects($this->atLeastOnce())
+      ->method('themeExists')
+      ->with('missing_extension')
+      ->will($this->returnValue(FALSE));
+
+    assert_options(ASSERT_ACTIVE, 0);
+    set_error_handler(function($severity, $message, $file, $line) {
+      $this->assertEquals($message, 'The extension "missing_extension" is not available.');
+    });
+    $this->assertEquals(NULL, $this->libraryDiscoveryParser->buildByExtension('missing_extension'), 'The extension "missing_extension" is not available');
+    restore_error_handler();
+
+    assert_options(ASSERT_ACTIVE, 1);
+    $this->setExpectedException('\AssertionError', 'The extension "missing_extension" is not available.');
+    $this->libraryDiscoveryParser->buildByExtension('missing_extension');
+  }
+
+  /**
    * Tests that an exception is thrown when no CSS/JS/setting is specified.
    *
    * @expectedException \Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException
