Skip to content

Importers return errors with full tracebacks #421

Open
@jozo

Description

@jozo

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";
   --------^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions