From d260d0deb240f585eb0e9c0212c4c60f781d549e Mon Sep 17 00:00:00 2001 From: Philippe Duval Date: Tue, 18 Sep 2018 11:54:04 -0400 Subject: [PATCH 1/2] Add InvalidResourceError exception type, fix #393. --- dash/dash.py | 11 +++++++++-- dash/exceptions.py | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index ec15aada7b..95924d2242 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -222,6 +222,10 @@ def add_url(name, view_func, methods=('GET',)): self._cached_layout = None self.routes = [] + # add a handler for components suites errors to return 404 + self.server.errorhandler(exceptions.InvalidResourceError)( + self._invalid_resources_handler) + @property def layout(self): return self._layout @@ -411,14 +415,14 @@ def _generate_meta_html(self): # Serve the JS bundles for each package def serve_component_suites(self, package_name, path_in_package_dist): if package_name not in self.registered_paths: - raise Exception( + raise exceptions.InvalidResourceError( 'Error loading dependency.\n' '"{}" is not a registered library.\n' 'Registered libraries are: {}' .format(package_name, list(self.registered_paths.keys()))) elif path_in_package_dist not in self.registered_paths[package_name]: - raise Exception( + raise exceptions.InvalidResourceError( '"{}" is registered but the path requested is not valid.\n' 'The path requested: "{}"\n' 'List of registered paths: {}' @@ -962,6 +966,9 @@ def add_resource(p, filepath): elif f == 'favicon.ico': self._favicon = path + def _invalid_resources_handler(self, err): + return err.args[0], 404 + def get_asset_url(self, path): asset = _get_asset_path( self.config.requests_pathname_prefix, diff --git a/dash/exceptions.py b/dash/exceptions.py index 9cbb4e4dd9..5ec2779afb 100644 --- a/dash/exceptions.py +++ b/dash/exceptions.py @@ -60,3 +60,7 @@ class InvalidCallbackReturnValue(CallbackException): class InvalidConfig(DashException): pass + + +class InvalidResourceError(DashException): + pass From 12dadc17cb866ae1340616b690170b9c191f350d Mon Sep 17 00:00:00 2001 From: Philippe Duval Date: Wed, 19 Sep 2018 10:40:55 -0400 Subject: [PATCH 2/2] Update changelogs. --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 749f132b1e..68e6c0c493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ -## 0.26.6 - 2018-09-18 +## 0.26.6 - 2018-09-19 ## Fixed -- Added `Cache-Control` headers to files served by `Dash.serve_components_suites`. [#387](https://github.com/plotly/dash/pull/387) +- Added `Cache-Control` headers to files served by `Dash.serve_component_suites`. [#387](https://github.com/plotly/dash/pull/387) - Added time modified query string to collected components suites resources. +- Added `InvalidResourceError`. [#393](https://github.com/plotly/dash/pull/393) +- Added a flask errorhandler to catch `InvalidResourceError` from `serve_component_suites` and return a 404. ## 0.26.5 - 2018-09-10 ## Fixed