Skip to content

Commit

Permalink
Added InitError exception to wrap errors from __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
jdlangs committed Aug 13, 2015
1 parent 24a92a9 commit ddd476b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type LoadError <: Exception
error
end

type InitError <: Exception
mod::Symbol
error
end

type MethodError <: Exception
f
args
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export
InvalidStateException,
KeyError,
LoadError,
InitError,
MethodError,
NullException,
ParseError,
Expand Down
7 changes: 7 additions & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ function showerror(io::IO, ex::LoadError, bt; backtrace=true)
end
showerror(io::IO, ex::LoadError) = showerror(io, ex, [])

function showerror(io::IO, ex::InitError, bt; backtrace=true)
print(io, "InitError: ")
showerror(io, ex.error, bt, backtrace=backtrace)
print(io, "\nduring initialization of module $(ex.mod)")
end
showerror(io::IO, ex::InitError) = showerror(io, ex, [])

function showerror(io::IO, ex::DomainError, bt; backtrace=true)
print(io, "DomainError:")
for b in bt
Expand Down
1 change: 1 addition & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jl_datatype_t *jl_argumenterror_type;
jl_datatype_t *jl_typeerror_type;
jl_datatype_t *jl_methoderror_type;
jl_datatype_t *jl_loaderror_type;
jl_datatype_t *jl_initerror_type;
jl_datatype_t *jl_undefvarerror_type;
jl_datatype_t *jl_ref_type;
jl_datatype_t *jl_pointer_type;
Expand Down
1 change: 1 addition & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ DLLEXPORT void jl_get_system_hooks(void)
jl_typeerror_type = (jl_datatype_t*)basemod("TypeError");
jl_methoderror_type = (jl_datatype_t*)basemod("MethodError");
jl_loaderror_type = (jl_datatype_t*)basemod("LoadError");
jl_initerror_type = (jl_datatype_t*)basemod("InitError");
jl_complex_type = (jl_datatype_t*)basemod("Complex");
}

Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ extern DLLEXPORT jl_datatype_t *jl_utf8_string_type;
extern DLLEXPORT jl_datatype_t *jl_errorexception_type;
extern DLLEXPORT jl_datatype_t *jl_argumenterror_type;
extern DLLEXPORT jl_datatype_t *jl_loaderror_type;
extern DLLEXPORT jl_datatype_t *jl_initerror_type;
extern DLLEXPORT jl_datatype_t *jl_typeerror_type;
extern DLLEXPORT jl_datatype_t *jl_methoderror_type;
extern DLLEXPORT jl_datatype_t *jl_undefvarerror_type;
Expand Down
11 changes: 8 additions & 3 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,14 @@ void jl_module_run_initializer(jl_module_t *m)
jl_apply(f, NULL, 0);
}
JL_CATCH {
jl_printf(JL_STDERR, "WARNING: error initializing module %s:\n", m->name->name);
jl_static_show(JL_STDERR, jl_exception_in_transit);
jl_printf(JL_STDERR, "\n");
if (jl_initerror_type == NULL) {
jl_rethrow();
}
else {
jl_rethrow_other(jl_new_struct(jl_initerror_type, m->name,
jl_exception_in_transit));
jl_printf(JL_STDERR, "\n");
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ jl_value_t *jl_eval_module_expr(jl_expr_t *ex)
jl_typeerror_type = NULL;
jl_methoderror_type = NULL;
jl_loaderror_type = NULL;
jl_initerror_type = NULL;
jl_current_task->tls = jl_nothing; // may contain an entry for :SOURCE_FILE that is not valid in the new base
}
// export all modules from Main
Expand Down Expand Up @@ -585,8 +586,14 @@ jl_value_t *jl_parse_eval_all(const char *fname, size_t len)
jl_rethrow();
}
else {
jl_rethrow_other(jl_new_struct(jl_loaderror_type, fn, ln,
jl_exception_in_transit));
jl_value_t * exception_type = jl_typeof(jl_exception_in_transit);
if (strcmp("InitError", jl_typename_str(exception_type)) == 0) {
jl_rethrow();
}
else {
jl_rethrow_other(jl_new_struct(jl_loaderror_type, fn, ln,
jl_exception_in_transit));
}
}
}
jl_stop_parsing();
Expand Down

0 comments on commit ddd476b

Please sign in to comment.