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

Vue SSR app renderToString catch errors/warnings #628

Open
TheSynt4x opened this issue Jan 3, 2024 Discussed in #627 · 0 comments
Open

Vue SSR app renderToString catch errors/warnings #628

TheSynt4x opened this issue Jan 3, 2024 Discussed in #627 · 0 comments

Comments

@TheSynt4x
Copy link

Hello everyone! I'd like to discuss a feature request for Vue and it's ability to do catch errors and warnings when they occur on the server-side. I've done research on this topic but it doesn't seem to be possible as of the moment to catch warnings/errors with Vue when using renderToString.

Here's an example of my use case:

const Vue = require('vue');
const { renderToString } = require('vue/server-renderer');

async function render(html) {
    const fullTemplate = `
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
        </head>
        <body>
            ${html}
        </body>
        </html>
    ` 

    const app = Vue.createSSRApp({
        data: function () {
            return {
                vars: {
                    hello: 'world',
                }
            }
        },

        template: fullTemplate,
    })

    try {
        return await renderToString(app);
    } catch(e) {
        console.log('in vue render: ' + e);
        throw e;
    }
}

What I've tried so far:

app.config.errorHandler = function (err, vm, info) {
    console.log('in vue error handler: ' + err);
}


app.config.warnHandler = function (msg, vm, trace) {
    console.log('in vue warn handler: ' + msg);
}

The errorHandler and warnHandler seem not to be working as I do not see my custom console logs. I've also tried attaching these same handlers to Vue itself but it seems to complain that these do not even exist on the Vue instance and throws as en error.

I've also tried the errorCaptured and the warningCaptured lifecycle hook and it doesn't seem to work either. The issue as of right now is that all errors that happen with the template will get outputted to the console.

Let's consider this as an example:

render('{{vars.hello}}');

Example above will work just fine as vars.hello is defined and it'll show world as expected.

If we consider the following as an example, is where we will run into some issues:

render('{{vars.hello}} {{vars.test}}');

vars.test is not defined and it will show nothing, but the issue here is that the error/warning is outputted in the console. Is there any way to get the error be shown in the template itself with a setup like this? From what I've researched, it does not seem possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant