Skip to content

Commit 361ac6d

Browse files
committed
SassMiddleware log syntax errors if exist
Fix #42
1 parent 90c5440 commit 361ac6d

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ To be released.
2020
- Now underscored files are ignored when compiling a directory.
2121
[:issue:`57` by Anthony Sottile]
2222
- Fixed broken FreeBSD build. [:issue:`34`, :issue:`60` by Ilya Baryshev]
23+
- :class:`~sassutils.wsgi.SassMiddleware` became to log syntax errors
24+
if exist during compilation to ``sassutils.wsgi.SassMiddleware`` logger
25+
with level ``ERROR``. [:issue:`42`]
2326

2427
__ https://github.com/sass/libsass/releases/tag/3.2.0
2528

sassutils/wsgi.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import absolute_import, with_statement
66

77
import collections
8+
import logging
89
import os
910
import os.path
1011

@@ -21,6 +22,54 @@ class SassMiddleware(object):
2122
requested it finds a matched SASS/SCSS source file and then compiled
2223
it into CSS.
2324
25+
It shows syntax errors in three ways:
26+
27+
Heading comment
28+
The result CSS includes detailed error message in the heading
29+
CSS comment e.g.:
30+
31+
.. code-block:: css
32+
33+
/*
34+
Error: invalid property name
35+
*/
36+
37+
Red text in ``body:before``
38+
The result CSS draws detailed error message in ``:before``
39+
pseudo-class of ``body`` element e.g.::
40+
41+
.. code-block:: css
42+
43+
/*
44+
body:before {
45+
content: 'Error: invalid property name';
46+
color: maroon;
47+
background-color: white;
48+
}
49+
*/
50+
51+
In most cases you could be aware of syntax error by refreshing your
52+
working document because it will removes all other styles and leaves
53+
only a red text.
54+
55+
:mod:`logging`
56+
It logs syntax errors if exist during compilation to
57+
``sassutils.wsgi.SassMiddleware`` logger with level ``ERROR``.
58+
59+
To enable this::
60+
61+
from logging import Formatter, StreamHandler, getLogger
62+
logger = getLogger('sassutils.wsgi.SassMiddleware')
63+
handler = StreamHandler(level=logging.ERROR)
64+
formatter = Formatter(fmt='*' * 80 + '\n%(message)s\n' + '*' * 80)
65+
handler.setFormatter(formatter)
66+
logger.addHandler(handler)
67+
68+
Or simply::
69+
70+
import logging
71+
logging.basicConfig()
72+
2473
:param app: the WSGI application to wrap
2574
:type app: :class:`collections.Callable`
2675
:param manifests: build settings. the same format to
@@ -36,6 +85,10 @@ class SassMiddleware(object):
3685
It creates also source map files with filenames followed by
3786
:file:`.map` suffix.
3887
88+
.. versionadded:: 0.8.0
89+
It logs syntax errors if exist during compilation to
90+
``sassutils.wsgi.SassMiddleware`` logger with level ``ERROR``.
91+
3992
"""
4093

4194
def __init__(self, app, manifests, package_dir={},
@@ -80,6 +133,8 @@ def __call__(self, environ, start_response):
80133
except (IOError, OSError):
81134
break
82135
except CompileError as e:
136+
logger = logging.getLogger(__name__ + '.SassMiddleware')
137+
logger.error(str(e))
83138
start_response(
84139
self.error_status,
85140
[('Content-Type', 'text/css; charset=utf-8')]

0 commit comments

Comments
 (0)