Add more information to error message when importing a class fails (#814)
This commit is contained in:
		 David Gibbons
					David Gibbons
				
			
				
					committed by
					
						 Johannes Linke
						Johannes Linke
					
				
			
			
				
	
			
			
			 Johannes Linke
						Johannes Linke
					
				
			
						parent
						
							c2c37c7c30
						
					
				
				
					commit
					fd36cd699c
				
			| @@ -5,6 +5,8 @@ import django.contrib.staticfiles.finders | |||||||
| import django | import django | ||||||
|  |  | ||||||
| import compressor.utils.staticfiles | import compressor.utils.staticfiles | ||||||
|  | from compressor.exceptions import FilterError | ||||||
|  | from compressor.utils import get_class | ||||||
|  |  | ||||||
| from imp import reload | from imp import reload | ||||||
|  |  | ||||||
| @@ -44,3 +46,13 @@ class StaticFilesTestCase(TestCase): | |||||||
|                 self.assertTrue(compressor.utils.staticfiles.finders is None) |                 self.assertTrue(compressor.utils.staticfiles.finders is None) | ||||||
|         finally: |         finally: | ||||||
|             reload(compressor.utils.staticfiles) |             reload(compressor.utils.staticfiles) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TestGetClass(TestCase): | ||||||
|  |  | ||||||
|  |     def test_get_class_import_exception(self): | ||||||
|  |         with self.assertRaises(FilterError) as context: | ||||||
|  |             get_class('common.uglify.JsUglifySourcemapCompressor') | ||||||
|  |  | ||||||
|  |         self.assertTrue(('Failed to import common.uglify.JsUglifySourcemapCompressor. ' | ||||||
|  |                          'ImportError is: No module named' in str(context.exception))) | ||||||
|   | |||||||
| @@ -15,8 +15,10 @@ def get_class(class_string, exception=FilterError): | |||||||
|             mod_name, class_name = get_mod_func(class_string) |             mod_name, class_name = get_mod_func(class_string) | ||||||
|             if class_name: |             if class_name: | ||||||
|                 return getattr(__import__(mod_name, {}, {}, [str('')]), class_name) |                 return getattr(__import__(mod_name, {}, {}, [str('')]), class_name) | ||||||
|         except (ImportError, AttributeError): |         except AttributeError as e: | ||||||
|             raise exception('Failed to import %s' % class_string) |             raise exception('Failed to import %s. AttributeError is: %s' % (class_string, e)) | ||||||
|  |         except ImportError as e: | ||||||
|  |             raise exception('Failed to import %s. ImportError is: %s' % (class_string, e)) | ||||||
|  |  | ||||||
|         raise exception("Invalid class path '%s'" % class_string) |         raise exception("Invalid class path '%s'" % class_string) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user