Refactor LessError and lesscHelper.formatError #2975
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
,stack
and 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.render
from your custom code.And then, we had a helper function called
lesscHelper.formatError
which accepted an instance ofLessError
and produced a human-readable message with optional support of shell colors.The problem was this method was only used in
lessc
and in the test launcher, when comparing the actual error message to the excepted one. So, this function was never called when callingless.render
directly 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.formatError
is actually what.toString
should do. Thus,.toString
can 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!