Refactor LessError and lesscHelper.formatError#2975
Conversation
|
Any progress on this, guys? |
|
@kirillrogovoy Sorry, been busy in the last month, so I didn't spot this. I'll review as soon as I can. Thanks! |
|
@kirillrogovoy I notice that although you moved less-node error handling into the core Is it possible to align less-browser to what less-node is doing, at least for console errors? Also, less-browser contains two forms of error output: HTML and console. Ideally, "styling" the error would be separate from the generated parts of the error. Meaning: the error output is always identical, but then gets formatted for the Terminal (colors), or the browser console, or as HTML. (Although, I've proposed that HTML formatting of errors could probably be deprecated, since all major browsers now have good debuggers with console output.) |
|
@matthew-dean You're right about the code duplication. I didn't touch that browser-related code on purpose - I was to make an initial step by encapsulating I think refactoring of other consumers of Also, note that If I get to refactoring less-browser any soon, I'd try this idea along the way. :) |
Objective (what was wrong)
This fixes my initial concern of this issue: #2951
Solution
Firstly, I want to drop a few facts about Errors in Javascript.
name,message,stackand might have any number of additional, user-defined fields. Generally, such fields are designed to keep different pieces of information related to the error..toString()method which overrides defaultObject.prototype.toString(). This method is designed to create an as detailed message for the developer as possible using those informational fields.Then, we have 3 possible usages of Less:
lessc- the CLI wayless.renderfrom your custom code.And then, we had a helper function called
lesscHelper.formatErrorwhich accepted an instance ofLessErrorand produced a human-readable message with optional support of shell colors.The problem was this method was only used in
lesscand in the test launcher, when comparing the actual error message to the excepted one. So, this function was never called when callingless.renderdirectly resulting in unavailability of getting a detailed user-friendly message as if you uselessc.After some analysis I understood that, according to facts I described above,
lesscHelper.formatErroris actually what.toStringshould do. Thus,.toStringcan be used inlessc, in tests and, obviously, it's automatically called when Node tries to display the error in your console, resolving the issue I faced.I didn't touch anything regarding the browser as it was out of the scope of my problem.
Critique is welcomed!