Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve traceback of import errors #188

Merged
merged 4 commits into from
Feb 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/parser.pyj
Original file line number Diff line number Diff line change
@@ -746,7 +746,7 @@ def parse($TEXT, options):
def do_import(key):
if key in IMPORTED: return # we'll reuse the original imported code
if IMPORTING[key]: # cyclical import
raise ImportError('Detected a recursive import of: ' + key + ' while importing: ' + module_id)
raise ImportError('Detected a recursive import of: ' + key + ' while importing: ' + module_id, options.filename)

# Ensure that the package containing this module is also imported
package_module_id = key.split('.')[:-1].join('.')
@@ -761,20 +761,29 @@ def parse($TEXT, options):
if e.code is 'ENOENT' or e.code is 'EPERM' or e.code is 'EACCESS':
if i is 1:
return None, None
if i is 1:
else:
raise

src_code = filename = None
modpath = key.replace(/\./g, '/')

for location in import_dirs:
if location:
data, filename = safe_read(location + '/' + modpath)
try:
data, filename = safe_read(location + '/' + modpath)
except as e:
if isinstance(e, ImportError):
raise e
msg = "Failed Import: '" + key + "', due to an error: " + e.message
raise ImportError(msg, options.filename, e)
if data is not None:
src_code = data
break
if src_code is None:
raise "Failed Import: '" + key + "' module doesn't exist in any of the import directories: " + import_dirs.join(', ')
raise ImportError(
"Failed Import: '" + key + "' module doesn't exist in any of the import directories: " + import_dirs.join(', '),
options.filename
)
contents = parse(src_code, {
filename: filename,
toplevel: None,
6 changes: 5 additions & 1 deletion src/utils.pyj
Original file line number Diff line number Diff line change
@@ -48,8 +48,12 @@ def DefaultsError(msg, defs):
this.defs = defs

class ImportError(Error):
def __init__(self, message):
def __init__(self, message, filename, readfile_error):
# readfile_error - optional container for underlying error while reading module file
# it is may be useful when custom `readfile` is used
self.message = message
self.filename = filename
self.readfile_error = readfile_error

class ParseError(Error):
def __init__(self, message, line, col, pos, is_eof, filename):
14 changes: 14 additions & 0 deletions test/basic/imports.pyj
Original file line number Diff line number Diff line change
@@ -61,3 +61,17 @@ assert.equal(b, this)

# Test global symbol declared in other module
assert.equal(GLOBAL_SYMBOL, 'i am global')

# Test custom error while reading module file
try:
rapydscript.parse(
'import abc\n',
{
readfile: def(fp): raise Error('custom_error');,
basedir: '/'
}
)
except as e: # e is the instance of ImportError that should contain readfile_error
assert.ok(isinstance(e, rapydscript.ImportError))
assert.ok(e.readfile_error)
assert(e.readfile_error.message is 'custom_error')