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

Ugly error message for bad require #1486

Closed
minghu6 opened this issue Jan 10, 2018 · 9 comments
Closed

Ugly error message for bad require #1486

minghu6 opened this issue Jan 10, 2018 · 9 comments
Labels

Comments

@minghu6
Copy link

minghu6 commented Jan 10, 2018

When I require a macro not existed, It throws:

Traceback (most recent call last):
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/importer.py", line 180, in hy_eval
    _ast, expr = hy_compile(hytree, module_name, get_expr=True)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 2213, in hy_compile
    result = compiler.compile(tree)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
    ret = self.compile_atom(_type, tree)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
    ret = self.compile_atom(fn, expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 819, in compile_do
    return self._compile_branch(expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in _compile_branch
    return _branch(self.compile(expr) for expr in exprs)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 318, in _branch
    results = list(results)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in <genexpr>
    return _branch(self.compile(expr) for expr in exprs)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
    ret = self.compile_atom(_type, tree)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
    ret = self.compile_atom(fn, expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 372, in checker
    return fn(self, expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 873, in compile_try_expression
    name)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1002, in _compile_catch_expression
    body = self._compile_branch(expr)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in _compile_branch
    return _branch(self.compile(expr) for expr in exprs)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 318, in _branch
    results = list(results)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 545, in <genexpr>
    return _branch(self.compile(expr) for expr in exprs)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
    ret = self.compile_atom(_type, tree)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
    ret = self.compile_atom(fn, expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 372, in checker
    return fn(self, expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 858, in compile_try_expression
    body += self.compile(expr.pop(0))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 472, in compile
    raise_empty(HyCompileError, e, sys.exc_info()[2])
  File "<string>", line 1, in raise_empty
hy.errors.HyCompileError: Internal Compiler Bug 😱
⤷ ImportError: cannot require names: [HySymbol('let')]
Compilation traceback:
File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 460, in compile
    ret = self.compile_atom(_type, tree)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1731, in compile_expression
    ret = self.compile_atom(fn, expression)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 447, in compile_atom
    else _compile_table[atom_type](self, atom))
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/compiler.py", line 1486, in compile_require
    require(module, self.module_name, assignments=assignments)
  File "/home/johnzhuang/.virtualenvs/hy/lib/python3.6/site-packages/hy/macros.py", line 106, in require
    raise ImportError("cannot require names: " + repr(list(unseen)))

And I can't catch the Exception using except

@minghu6 minghu6 changed the title How to catch Exception while require an not existed macro How to catch Exception while require a macro not existed Jan 10, 2018
@refi64
Copy link
Contributor

refi64 commented Jan 10, 2018

Well, for starters, require is basically import for macros, so (require file) will import any macros inside file.hy. You don't run (require macro-name).

That being said, the bug here is just that the raised exception isn't being handled properly.

@Kodiologist
Copy link
Member

I'm guessing you tried something like (try (require foo) (except [...] ...)). The try can't catch an exception raised by require because require takes effect at compile-time, not run-time.

There is a minor bug here, though, which is that the error message ("cannot require names") is raised as an ImportError instead of a HyTypeError, yielding the ugly stack trace and the message "Internal Compiler Bug 😱".

@Kodiologist Kodiologist changed the title How to catch Exception while require a macro not existed Ugly error message for bad require Jan 10, 2018
@minghu6
Copy link
Author

minghu6 commented Jan 11, 2018

You mean I should require whole file (including other macro), if I am not certain some specific macro exist?

@Kodiologist
Copy link
Member

No. There is currently no way to conditionally require a macro.

@gilch
Copy link
Member

gilch commented Jan 11, 2018

Not so. You can write a macro to conditionally require macros.

(defmacro ifreq []
  (if (dec (len (input)))
      `(require [bar [foo]])
      `(require [baz [foo]])))
(ifreq)
(foo)

Of course, the (input) only happens once, when the above file is compiled.

@Kodiologist
Copy link
Member

Ah, I hadn't thought of that. Well done.

@minghu6
Copy link
Author

minghu6 commented Jan 11, 2018

I don't understand the usage of if (dec (len (input)) @gilch

@minghu6
Copy link
Author

minghu6 commented Jan 11, 2018

And anyway, using require bring risks of critial error. We can't do anything except let it crash :(.

I am not sure it's proper that raise Exception instantly during compile-time for a bad request, if we can delay the exception until the run-time, so we can handle it?

@gilch
Copy link
Member

gilch commented Jan 11, 2018

It's just a stupid example not relevant to the question. You can use any condition you want. All those functions are documented. If you enter nothing, it picks one branch, something the other. I only picked that condition because it was easy to implement and test.

brandonwillard added a commit to brandonwillard/hy that referenced this issue Nov 27, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Nov 28, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Nov 29, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Dec 1, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Dec 2, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Dec 7, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
Kodiologist pushed a commit to brandonwillard/hy that referenced this issue Dec 8, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
Kodiologist pushed a commit to brandonwillard/hy that referenced this issue Dec 12, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Dec 17, 2018
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Jan 17, 2019
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
brandonwillard added a commit to brandonwillard/hy that referenced this issue Jan 17, 2019
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
Kodiologist pushed a commit to Kodiologist/hy that referenced this issue Jan 20, 2019
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
Kodiologist pushed a commit to Kodiologist/hy that referenced this issue Jan 26, 2019
Compiler and command-line error messages now reflect their Python counterparts.
E.g. where Python emits a `SyntaxError`, so does Hy; same for `TypeError`s.
Multiple tests have been added that check the format and type of raised
exceptions over varying command-line invocations (e.g. interactive and not).

A new exception type for `require` errors was added so that they can be treated
like normal run-time errors and not compiler errors.

The Hy REPL has been further refactored to better match the class-structured
API.  Now, different error types are handled separately and leverage more base
class-provided functionality.

Closes hylang#1486.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants