sass / libsass-python

A straightforward binding of libsass for Python. Compile Sass/SCSS in Python with no Ruby stack at all!
https://sass.github.io/libsass-python/
MIT License
563 stars 54 forks source link

Importers return errors with full tracebacks #421

Open jozo opened 1 year ago

jozo commented 1 year ago

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 commented 1 year ago

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