Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Exception Reporting #291

Open
lynchem opened this issue Mar 6, 2019 · 10 comments · May be fixed by #368
Open

Improve Exception Reporting #291

lynchem opened this issue Mar 6, 2019 · 10 comments · May be fixed by #368
Labels
ready We've decided how to solve or implement it
Milestone

Comments

@lynchem
Copy link

lynchem commented Mar 6, 2019

Currently in Kadira we get lots of exceptions that look like this:
[client] Exception in tempalte helper:

As there is no more detail they all get grouped together which can be painful to wade through.

One simple update we could make in Blaze is to pass the name of the helper function to _wrapCatchingException (in lookup.js) so

return Blaze._wrapCatchingExceptions(f, 'template helper').apply(self, args);

Would be instead something like

return Blaze._wrapCatchingExceptions(f, `template helper ${templateFunc.name}`).apply(self, args);

It would be even better if we passed the actual template name into wrapHelper and then we could report that too meaning a unique grouping by template name and helper in Kadira or whatever other error handling program someone is using.

I'm happy to make the PR if whoever monitors this agrees 😃

@lynchem
Copy link
Author

lynchem commented Mar 6, 2019

Actually that templateFunc.name was always set to "bind" so instead we decided to update wrapHelper to take a name parameter which is uses instead of "template helper". Then when calling wrapHelper we just pass

`${template.viewName} ${name}`

This works nicely so far.

@jamesgibson14
Copy link

Do you have a PR for this or just a fork of Blaze? Can you show your code?

@lynchem
Copy link
Author

lynchem commented Mar 1, 2021

Hi @jamesgibson14. I just monkeypatched it. It would be easy to make a PR as it's a tiny change. Here's our code:

// If `x` is a function, binds the value of `this` for that function
// to the current data context.
function bindDataContext(x) {
    if (typeof x === "function") {
        return function() {
            let data = Blaze.getData();
            if (data === null) {
                data = {};
            }
            return x.apply(data, arguments);
        };
    }
    return x;
}

function wrapHelper(f, templateFunc, name) {
    if (typeof f !== "function") {
        return f;
    }

    return function() {
        let self = this;
        let args = arguments;

        return Blaze.Template._withTemplateInstanceFunc(templateFunc, function() {
            return Blaze._wrapCatchingExceptions(f, name).apply(self, args);
        });
    };
}

Blaze._wrapCatchingExceptions = function(f, where) {
    if (typeof f !== "function") {
        return f;
    }

    return function() {
        try {
            return f.apply(this, arguments);
        } catch (e) {
            if (ObitEnvironment.isTesting()) {
                throw e;
            } else {
                Blaze._reportException(e, `Exception in ${where}:`);
            }
        }
    };
};

Blaze._getTemplateHelper = function(template, name, tmplInstanceFunc) {
    if (template.__helpers.has(name)) {
        let helper = template.__helpers.get(name);
        if (helper === Blaze._OLDSTYLE_HELPER) {
            throw new Meteor.Error("not-suported", "We removed support for old style templates");
        } else if (helper !== null) {
            return wrapHelper(
                bindDataContext(helper),
                tmplInstanceFunc,
                `${template.viewName} ${name}`
            );
        } else {
            return null;
        }
    }
    return null;
};

Blaze._getGlobalHelper = function(name, templateInstance) {
    if (Blaze._globalHelpers[name] !== null) {
        return wrapHelper(
            bindDataContext(Blaze._globalHelpers[name]),
            templateInstance,
            `global helper ${name}`
        );
    }
    return null;
};

@jamesgibson14
Copy link

jamesgibson14 commented Mar 9, 2021 via email

@filipenevola
Copy link
Collaborator

I'm closing this issue because it's too old.

If you think this issue is still relevant please open a new one.

Why? We need to pay attention to all the open items so we should keep opened only items where people are involved.

@lynchem
Copy link
Author

lynchem commented Mar 21, 2021

@filipenevola - I can understand bugs quickly becoming irrelevant on actively developed codebases but this is hardly the case here. Here's the last two releases.

v2.3.4, 2019-Dec-13
jquery 3 support #299
v2.3.2, 2017-Mar-21
Made beautification of compiled spacebars code happen only on the server.

There's also been a small bit of discussion of late too. This issue in particular is a really easy win for any blaze user and saved us a few hours of debugging for sure.

@filipenevola
Copy link
Collaborator

I can understand bugs quickly becoming irrelevant on actively developed codebases but this is hardly the case here

This is not the case here, I just closed all the old items without comments/activity.

We can re-open or open new issues for the same problem.

As I saw in your original comment that you are willing to make a PR, please go ahead @lynchem

@filipenevola filipenevola reopened this Mar 22, 2021
@filipenevola filipenevola added the ready We've decided how to solve or implement it label Mar 22, 2021
@jamesgibson14
Copy link

I just wanted to post here that I tried out @lynchem's "monkey patch" and it works great.

@jankapunkt
Copy link
Collaborator

@StorytellerCZ should we consider this for 3.0 ?

@StorytellerCZ
Copy link
Collaborator

Yes, that would be a great addition. PRs welcomed!

@StorytellerCZ StorytellerCZ added this to the Blaze 3.0 milestone Mar 28, 2022
@jankapunkt jankapunkt linked a pull request Apr 14, 2022 that will close this issue
@StorytellerCZ StorytellerCZ linked a pull request Jul 28, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready We've decided how to solve or implement it
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants