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

Importers return errors with full tracebacks #421

Open
jozo opened this issue Feb 20, 2023 · 1 comment
Open

Importers return errors with full tracebacks #421

jozo opened this issue Feb 20, 2023 · 1 comment

Comments

@jozo
Copy link

jozo commented Feb 20, 2023

I'm using importers in my project. Sometimes I need to raise an exception inside them because the path to a scss file is wrong. I want to show a simple message to my users that the file was not found. The problem is that libsass converts exception from importer to a string with full traceback. I don't want to show traceback to the user.

How can I get rid of the traceback? Can libsass throw a python exception instead of converting it to a string?

Example:

import sass


def importer(path, prev):
    raise ValueError(f"Oops, something is wrong with {path}")


try:
    css = sass.compile(
        string='@import "xxx";',
        importers=[(0, importer)]
    )
except sass.CompileError as e:
    # Display error to user:
    print("Error in Sass:", e)

Output:

Error in Sass: Error: 
       Traceback (most recent call last):
         File "/usr/local/lib/python3.11/site-packages/sass.py", line 191, in inner
           ret = func(path, prev)
                 ^^^^^^^^^^^^^^^^
         File "<ipython-input-15-a2aa58127f68>", line 2, in importer
           raise ValueError(f"Oops, something is wrong with {path}")
       ValueError: Oops, something is wrong with xxx
        on line 1:9 of stdin
>> @import "xxx";
   --------^
@asottile
Copy link
Member

I don't believe there's a way to do that -- in theory there could be some customization around returning a SassError and converting that into an import error -- but it doesn't look like that happens at the moment

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

No branches or pull requests

2 participants