-
Notifications
You must be signed in to change notification settings - Fork 84
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
Error handling #95
Error handling #95
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,18 +61,6 @@ module.exports = function(options) { | |
options = { src: options }; | ||
} | ||
|
||
var sassMiddlewareError = null; | ||
var cachedErrorCb = options.error; | ||
|
||
// This function will be called if something goes wrong | ||
var error = function(err) { | ||
if (cachedErrorCb) { | ||
cachedErrorCb(err); | ||
} | ||
|
||
sassMiddlewareError = err; | ||
}; | ||
|
||
// Source directory (required) | ||
var src = options.src || function() { | ||
throw new Error('sass.middleware() requires "src" directory.'); | ||
|
@@ -109,6 +97,21 @@ module.exports = function(options) { | |
|
||
// Middleware | ||
return function sass(req, res, next) { | ||
var sassMiddlewareError = null; | ||
|
||
// This function will be called if something goes wrong | ||
var error = function(err, errorMessage) { | ||
if (debug) { | ||
log('error', 'error', errorMessage || err); | ||
} | ||
|
||
if (options.error) { | ||
options.error(err); | ||
} | ||
|
||
sassMiddlewareError = err; | ||
}; | ||
|
||
if (req.method != 'GET' && req.method != 'HEAD') { | ||
return next(); | ||
} | ||
|
@@ -141,24 +144,20 @@ module.exports = function(options) { | |
|
||
// When render is done, respond to the request accordingly | ||
var done = function(err, result) { | ||
var data; | ||
|
||
if (err) { | ||
var file = sassPath; | ||
if (err.file && err.file != 'stdin') { | ||
file = err.file; | ||
} | ||
|
||
var fileLineColumn = file + ':' + err.line + ':' + err.column; | ||
data = err.message.replace(/^ +/, '') + '\n\nin ' + fileLineColumn; | ||
if (debug) log('error', 'error', '\x07\x1B[31m' + data + '\x1B[91m'); | ||
|
||
error(err); | ||
var errorMessage = '\x07\x1B[31m' + err.message.replace(/^ +/, '') + '\n\nin ' + fileLineColumn + '\x1B[91m'; | ||
|
||
error(err, errorMessage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved logging into |
||
return next(err); | ||
} | ||
|
||
data = result.css; | ||
var data = result.css; | ||
|
||
if (debug) { | ||
log('debug', 'render', options.response ? '<response>' : sassPath); | ||
|
@@ -188,7 +187,7 @@ module.exports = function(options) { | |
res.end(data); | ||
} | ||
|
||
// If response is falsey, also write to file | ||
// If response is true, do not write to file | ||
if (options.response) { | ||
return doneWriting(); | ||
} | ||
|
@@ -198,12 +197,14 @@ module.exports = function(options) { | |
|
||
mkdirp(dirname(cssPath), '0700', function(err) { | ||
if (err) { | ||
return error(err); | ||
error(err); | ||
cssDone = true; | ||
return doneWriting(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as it was, in case of errors here, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add an option to toggle propagation to next middleware in case of error? My concern is someone might have taken dependency on this behavior by now and this would break them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that nothing calls Even if someone supplies In most other places, the call to Propagating to the next middleware in case of error is the expected behaviour – we are a middleware after all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense.
For instance, some consumer might be reading the error and not existing as Sass middle ware is existing the pipeline for them. With that being said, I don't have strong feelings about it. Consistent behavior wins. Thanks for elaboration. :) |
||
} | ||
|
||
fs.writeFile(cssPath, data, 'utf8', function(err) { | ||
if (err) { | ||
return error(err); | ||
error(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
} | ||
|
||
cssDone = true; | ||
|
@@ -215,13 +216,16 @@ module.exports = function(options) { | |
var sourceMapPath = this.options.sourceMap; | ||
mkdirp(dirname(sourceMapPath), '0700', function(err) { | ||
if (err) { | ||
return error(err); | ||
error(err); | ||
sourceMapDone = true; | ||
return doneWriting(); | ||
} | ||
|
||
fs.writeFile(sourceMapPath, result.map, 'utf8', function(err) { | ||
if (err) { | ||
return error(err); | ||
error(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both same as the above |
||
} | ||
|
||
sourceMapDone = true; | ||
doneWriting(); | ||
}); | ||
|
@@ -265,18 +269,18 @@ module.exports = function(options) { | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not really an error if we can't stat There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If sassPath doesn't exist then consumer of sass middleware must have wrong input path provider. Error/fail fast is the current behavior, this is intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand the code right, Inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually, this check is bit redundant because But lets keep it as is for now; I am planning to but some debug logs here and other places with new tag This will help consumer debugging. |
||
// Compare mtimes | ||
fs.stat(sassPath, function(err, sassStats) { | ||
if (err) { | ||
error(err); | ||
if (err) { // sassPath can't be accessed, nothing to compile | ||
return next(); | ||
} | ||
|
||
fs.stat(cssPath, function(err, cssStats) { | ||
if (err) { // CSS has not been compiled, compile it! | ||
if ('ENOENT' === err.code) { | ||
if (debug) { log('error', 'not found', cssPath); } | ||
if (err) { | ||
if ('ENOENT' === err.code) { // CSS has not been compiled, compile it! | ||
if (debug) { log('debug', 'not found', cssPath); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not finding the CSS file is common, not really an error - it happens before the first compilation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree the message can probably say: |
||
return compile(); | ||
} | ||
|
||
error(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. give this error to |
||
return next(err); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved
error()
here sosass()
is reentrant.added logging of all errors if debugging is on