benrbray / prosemirror-math

Schema and plugins for "first-class" math support in ProseMirror!
https://benrbray.com/prosemirror-math/
MIT License
245 stars 35 forks source link

MathView throws unknown errors instead of handling them like ParseError #30

Closed dragonman225 closed 3 months ago

dragonman225 commented 2 years ago

Hi benrbray,

Thank you for this great library, I have integrated it into the editor of Jade, my side-project to build a spatial note-taking tool.

An issue I notice recently is that MathView throws the KaTeX error if it's not a ParseError. It happens, for example, when I type this invalid LaTeX: \gdef\bar#1#{#1^2} \bar{y} + \bar{y}.

try {
    katex.render(texString, this._mathRenderElt, this._katexOptions);
    this._mathRenderElt.classList.remove("parse-error");
    this.dom.setAttribute("title", "");
} catch (err) {
    if (err instanceof ParseError) {
        console.error(err);
        this._mathRenderElt.classList.add("parse-error");
        this.dom.setAttribute("title", err.toString());
    } else {
        throw err; // <<
    }
}

Throwing the error means I need to handle it at higher level, or my app would crash. But when I catch the error in the app, I cannot easily know which MathView threw it and I have no access to _mathRenderElt to show the error message in place.

I think a better solution is to handle it the same way as handling ParseError: show the error message in place to notify users to correct their LaTeX. You can take a look at the approach in my fork here.

benrbray commented 2 years ago

Good point, you're right. I'll try to include this in the next release.