jaysonsantos / jinja-assets-compressor

A Jinja extension (compatible with Flask and other frameworks) to compile and/or compress your assets.
Other
93 stars 10 forks source link

Raise a better error when compiler is not found in PATH #48

Closed lewis617 closed 7 years ago

lewis617 commented 7 years ago

When the compiler is not found we let OSError: [Errno 2] No such file or directory be thrown. We should return a better error and a better message like Executable {name} not found.

Full stacktrace of an example:

Traceback (most recent call last):
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask_cache/__init__.py", line 297, in decorated_function
    rv = f(*args, **kwargs)
  File "/Users/liuyiqi/code/src/opendata/app/home/views.py", line 9, in index
    return render_template('home.html')
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/liuyiqi/code/src/opendata/app/templates/home.html", line 3, in top-level template code
    {% extends 'layouts/base.html' %}
  File "/Users/liuyiqi/code/src/opendata/app/templates/layouts/base.html", line 26, in top-level template code
    {% compress 'css' %}
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/jac/extension.py", line 39, in _compress_block
    return self.compressor.compress(html, compression_type)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/jac/base.py", line 103, in compress
    debug=self.config.compressor_debug)
  File "/Users/liuyiqi/venv/opendata/lib/python2.7/site-packages/jac/compressors/less.py", line 35, in compile
    stderr=subprocess.PIPE, cwd=None)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
jaysonsantos commented 7 years ago

Make sure less compiler is in your path

lewis617 commented 7 years ago

I did this:

npm install less  -g

but the problem is still there. What do you mean "Make sure less compiler is in your path"?

lewis617 commented 7 years ago

I did this again:

sudo npm install less -g 

and the problem is solved. Thank you anyway.

jaysonsantos commented 7 years ago

I am leaving this open because we should make this error clear. To test if it would work for your script just run which compiler_executable and if it returns a valid result, your python script should work

lewis617 commented 7 years ago

I run the command in my project root:

which compiler_executable

and return nothing. I want to know how to run which compiler_executable,where to run? Can you give me more details?

jaysonsantos commented 7 years ago

In case of less the compiler executable is called lessc, if you run which lessc and it shows something, lessc is in your PATH variable and jac would be able to find it

jaysonsantos commented 7 years ago

To avoid this kind of problem, the executable path should be configurable, I've created #49 to tackle this

lewis617 commented 7 years ago

oh, I see what you mean. I run the which lessc, and return a valid result: /Users/liuyiqi/.nvm/versions/node/v7.1.0/bin/lessc.

lewis617 commented 7 years ago

Adding a config to override compilers executables is a good solution. The error came from nvm perhaps.

jaysonsantos commented 7 years ago

fixed by #50