以编程方式使用 .less 时如何输出错误?
我编写了一个 ASP.NET MVC 操作方法,该方法接收 .less 文件名,通过 Less.Parse(
处理它并输出处理后的 css 文件。
只要 .less 代码有效,这种方法就可以正常工作,但如果出现错误,dotLess 只会返回一个空字符串。因此,如果处理文件时出现错误,我的操作方法将返回一个空的 css 文件。
如何输出包含更详细的语法错误描述的错误消息?
I've written an ASP.NET MVC action method that receives a .less file name, processes it via Less.Parse(<filename>)
and outputs the processed css file.
This works fine as long as the .less code is valid, but if there is an error, dotLess just returns an empty string. So if there is an error processing the file, my action method returns an empty css file.
How can I output an error message with a closer description of the syntax error instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我今天刚刚在我的 RequestReduce 项目中遇到了这个问题。我变得越来越空白 -> css 转换是因为存在解析错误,这些错误似乎正在进入以太坊。感谢 qes 的回答,我能够找到一个解决方案,可以将错误写入响应流。这是我的 dotless.Core.Loggers.ILogger:
我将其传递到发送到 EngineFactory 的配置中:
出于单元测试的目的,我想传递将写入错误的 HttpResponseBase。这就是我觉得事情变得丑陋的地方,通过一些令人讨厌的转换来获取对我的记录器的引用:
我希望这会有所帮助,如果有人知道一种更优雅的方法来获取对记录器的引用,请告诉我。
I just faced this today in my RequestReduce project. I was getting blank less -> css transforms because there were parse errors that appeared to be going into the ether. Thanks to qes's answer I was able to work out a solution where I could write the errors to the response stream. Here is my dotless.Core.Loggers.ILogger:
I pass this into the Configuration sent to the EngineFactory:
For unit testing purposes I wanted to pass in my HttpResponseBase that would write the error. This is where I felt things getting ugly with some nasty casting to get a reference to my logger:
I hope this helps out and if someone knows of a more elegant way to get a reference to the logger, please let me know.
您可以使用 web.config 轻松完成此操作。在 dotless 配置部分中,添加以下内容:
logger="dotless.Core.Loggers.AspResponseLogger"
。这将使错误输出成为无点输出,而不是空白 css。我已将以下内容作为示例。 (“...”代表 web.config 中现有的内容)。在我下面的示例中,缓存设置为 false。这对于调试目的很有用。正常情况下应该将其设置为 true。
You can do this very easily with web.config. In your dotless configuration section, add the following:
logger="dotless.Core.Loggers.AspResponseLogger"
. This will make dotless output the errors instead of blank css.I've included the following as an example. ("..." represents existing stuff in your web.config). In my example below cache is set to false. This is useful for debugging purposes. It should probably be set to true under normal circumstances.
我正在使用 dotless 的包装类,如下所示:
技巧是使用自己的
IStylizer
接口实现,在遇到解析错误时调用该接口来格式化生成的错误消息。这使我们能够捕获错误的离散部分,这与错误已经是格式化文本的 ILogger 接口的实现不同。I am using a wrapper class around dotless, as follows:
The trick is to use own implementation of
IStylizer
interface that is called upon encountering a parse error to format the resulting error message. This allows us to capture discrete pieces of the error, unlike implementation ofILogger
interface where the error is already a formatted text.为了他人的利益,如果您只是从页面引用 .less 文件,@tony722 的解决方案就可以工作。
但如果您直接调用
Less.Parse
,此方法会将任何错误写入Response
:For the benefit of others, @tony722's solution works if you simply reference .less files from your pages.
But if you call
Less.Parse
directly, this method will write any error intoResponse
:这将记录到 VS 中的输出窗口:
This logs to output window in VS:
这会将所有 LESS 解析错误记录到调试控制台:
This will log any LESS parsing errors to the debug console:
dotLess 解析器捕获异常并将其输出到记录器。执行此操作的 dotLess 源代码片段是
LessEngine.TransformToCss
:Less.Parse
有一个重载,它接受一个DotlessConfiguration
对象,该对象提供了多个属性您可以使用:您会注意到
Logger
属性的类型为Type
。无论您提供什么类型,都必须实现 dotless.Core.Loggers.ILogger:正如我们在第一个代码片段中看到的,遇到错误时将调用记录器上的 Error 方法在解析过程中。
现在,所有这一切的一个棘手问题是如何实例化实现 ILogger 的类型的实例。在内部,dotLess 使用嵌入到 DLL 中的 IoC 容器。在方法调用之后,它似乎最终会调用 Activator.CreateInstance 来实例化您的 ILogger。
我希望这至少有一些帮助。
The dotLess parser traps Exceptions and outputs them to a Logger. The snippet from dotLess's source that performs this is
LessEngine.TransformToCss
:Less.Parse
has an overload that takes aDotlessConfiguration
object, which provides several properties that you can use:You will notice that the
Logger
property is of typeType
. Whatever type you supply must implementdotless.Core.Loggers.ILogger
:As we saw in the first snippet, the
Error
method on the logger will get called when an error is encountered during parsing.Now, the one sticky point of all this is how exactly an instance of the type that implements
ILogger
gets instantiated. Internally, dotLess uses an IoC container that is baked into the DLL. Following the method calls, it appears that it will eventually callActivator.CreateInstance
to instantiate your ILogger.I hope this is at least somewhat helpful.