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

bot.process crash when say {{obj.prop}} with context.obj null. Cannot be catched #1215

Closed
miqmago opened this issue Nov 28, 2022 · 9 comments

Comments

@miqmago
Copy link
Contributor

miqmago commented Nov 28, 2022

Describe the bug
I'm using directline controller to addActivities to the bot and wait for the response.

Let's suppose we have a dialog like this:

dialog someDialog
 say {{issue.value}}

If context.issue === null then the bot crashes and cannot be catched from calling code.
Problem is that bot.process(session) crashes and there is no way to reject the promise.

The problem is that it calls executeAction and it goes to session.say --> compile and then the processString evaluator crashes

Stack trace:

@Apollon77
Copy link
Contributor

I think {{issue && isue.value }} should work or?

@aigloss
Copy link
Collaborator

aigloss commented Nov 29, 2022

Hi @miqmago,

catching the error silently would not be a good option, as it could lead to difficult to debug problems. You could avoid your issue using a condition, for instance you could do something like

[issue.value !== null] say i{{issue.value}}
[issue.value === null] do some other stuff

On the other hand, maybe it would be nice to have a kind of "onError" command, that allows you to define a fallback dialog when there is an unexpected error.

@Apollon77
Copy link
Contributor

We had an other issue (think ws closed because inactivity in last cleanup) that ways exactly about the same and yes - question is what should happen if the replacement is not posible.

Alternaive to an "onError" could be to simply catch the error and do not replace the placeholder at all - yes people would maybe wonder why but would maybe be consistent

@aigloss
Copy link
Collaborator

aigloss commented Dec 2, 2022

I see you point, but don't agree hiding errors is more consistent. If the developer is trying to use a variable to send a message to the user and that variable is not initialised, it can be intentional or not, we can't know.

Regarding the onError, in the meantime, the globalFallbackDialog setting is available in the bot, which allows you to set the name of the dialog to be executed in case of errors while processing the bot flow (defaults to '/globalFallbackDialog'). So this can be used in the meantime to catch errors; not the same behaviour, because it's global for the whole conversation flow, but still useful.

@miqmago
Copy link
Contributor Author

miqmago commented Dec 3, 2022

@aigloss so if I understood well, if I place a

dialog globalFallbackDialog
 say Something went really wrong, please contact someone

I would fallback there? By the name of the dialog I would'nt say it is an error dialog, is it configurable? Also I've searched in the whole node_modules/@nlpjs @4.22.0 for globalFallbackDialog but I couldn't find any reference.

On the other hand, maybe it would be nice to have a kind of "onError" command, that allows you to define a fallback dialog when there is an unexpected error.

I also think this could be a good option.

Currently I'm instantiating the bot like this:

        dock = await dockStart(JSON.parse(settings));
        registerValidators(dock);
        registerActions(dock);
        directline = dock.get('directline');
        apiServer = dock.get('api-server');
        contextManager = dock.get('context-manager');

        // Lo lanza el @nlpjs/nlp/src/context-manager.js::setContext
        contextManager.onCtxUpdate = async (context: IBotContext) => {
            // do some stuff
        };

Maybe this could be another possibility?:

    contextManager.onError = (context: IBotContext)  => {};

@miqmago
Copy link
Contributor Author

miqmago commented Dec 3, 2022

@Apollon77 @aigloss validation of any variable could be an option, but this would be a kind of silently let an error pass. In my case, the problem was that the variable was not set and I had to fix an action problem. If I would have prevented the error with variable validation, it could have lead to a bug deployment in productive in this case, so I'm not sure if this is the best option...

@aigloss
Copy link
Collaborator

aigloss commented Dec 3, 2022

Hi @miqmago ,

have a look at

if (!this.settings.globalFallbackDialog) {
this.settings.globalFallbackDialog = '/globalFallbackDialog';
}
const { globalFallbackDialog } = this.settings;
this.settings.globalFallbackDialog = globalFallbackDialog.startsWith('/')
? globalFallbackDialog
: `/${globalFallbackDialog}`;

and how it is used in the process method of the same class.

The name of the dialog is configurable in the bot configuration. Try updating your settings to include the following

{
    "settings": {
        ...
        "bot": {
            "globalFallbackDialog": "/myErrorDialog"
        }
    },
    "use": [...]
}

having that in place, the dialog myErrorDialog should be executed whenever there is an unexpected error

@miqmago
Copy link
Contributor Author

miqmago commented Dec 5, 2022

Ok I see, it's starting from v4.24.2... Will update, many thanks!!

If you are planning the onError feature I can keep this issue open.
If you agree I could do a pull request. I would do something like is:

  async process(session) {
    const context = await this.contextManager.getContext(session);
    try {
      context.channel = session.channel;
      // ...
    } catch(err) {
      if (typeof context.onError === 'function') {
        context.onError(err);
      }
      throw err;
    }
  }

@aigloss aigloss closed this as completed Dec 15, 2022
@miqmago
Copy link
Contributor Author

miqmago commented Dec 23, 2022

So @aigloss, what do you think? Makes sense to add the try-catch to process and to call onError function when exists?

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

3 participants