AriaMinaei/pretty-error

Allow access to the rendered html + css

Closed this issue · 4 comments

Hey @AriaMinaei

I am using pretty-error for the html-webpack-plugin.
The error is logged to the console and also added to the result html file.

Would it be possible to render the error message also as a html (including a style tag)?

The code that you're looking for is here:

getObject: (e) ->

Calling pe.getObject() would return an object describing the structure of the final rendered error object:

const e = new Error('blah')
const obj = pe.getObject(e)

If you inspect obj, you'll see that it has all the information there is about the original error object.

Now, since your goal is to ultimately print the error as an html document, you'll basically have two options:

  1. Write a convertor function that takes obj and returns "<html>...</html>". This is what I would do.
  2. There is a private method on RenderKid that takes the obj and refines it further. It works like this: const renderKid = pe._renderer; const refinedDom = renderKid._toDom(obj). In this method, refinedDom is actually a DOM tree compatible with htmlparser2. What you can do with it is to stringify it (similar to HTMLElement#innerHTML) using this function. Just take note that _toDom() and _renderer are private properties, so they may change or be removed in the future.

Isn't that sth you could easily provide so I don't have to build a large code around pretty-error?

If enough people wanted this, then it might make sense to provide it out of the box. Otherwise, it's just wasted effort that complicates the code base.

the html-webpack-plugin which makes heavy use of pretty error is downloaded million times a week so it would aid many users and javascript is very web focused so providing a html presentation might also help other use cases