Failure Condition Bug

Looks like the failure condition is not working. I get a true value for the failure condition, yet I still don't trigger the failure event handler. I also get the successful query notification.


I am also running into this issue. The failure condition for me triggers the error so there is no success notification, however, the message that I set up for the failure condition is not shown as a notification to the user.

[Edit] Actually it seems to be working now. Very strange! I noticed now a bunch of other notifications that I hadn't seen before showing up as well, so maybe notifications was broken altogether.

1 Like

Hey @mblack!

Can you try referencing just data in the transformer instead of The former will get the value from the current query run while the latter still contains the value of the previous query run.

The failure condition is checked before everything in the model, including the Product_add_handler object, is updated, however data is a special variable that is populated sooner. Built-in transformers function similarly.

Let me know if that helps! Otherwise, it would be helpful to know what kind of query this is so that we can do some further investigation.

Sure, here's what I get

Ah :pensive: unfortunately failure conditions aren't working specifically with JS queries at the moment. It's something our dev team is aware of and we can let you know here when it's fixed.

Since you have control over the script here though you can build in some custom error handling, with utils.showNotification and the like. If you'd like to share more of the code you're using we can help out with that syntax!

@Kabirdas I am having trouble implementing some form error handling for my JS query via the script option you mention. I am trying to use utils.showNotification calls to surface the correct success/error message after running the query, conditioning on [query_name].error. What I am noticing is that [query_name].error is holding the previous run's result at run-time, thus, the success/error message is often incorrect. I think you reference similar erroneous behavior in this comment. How can I get around this?

What my script looks like:
createPortalUser is the name of the query that should kick off when user submits the form after passing all field validation etc

createPortalUser.trigger().then(() => {
  if(createPortalUser.error === null || createPortalUser.error === undefined){
    utils.showNotification({title: "Success", description: "This user has been emailed and has 24 hours to click the link and add their password", notificationType: "success"});
  else if(createPortalUser.error.includes('already exists')){
    utils.showNotification({title: "Error", description: "There is already a user for this email address. Please search for the email and edit the existing record", notificationType: "error"});
  else if(createPortalUser.error !== null && createPortalUser.error !== undefined){
    // in case of error other than dupe email
    utils.showNotification({title: "Error", description: "Failed to create portal user. Please try again.", notificationType: "error"});
}, () => {
  // in case of error (NOT REACHING THIS CODE)
  utils.showNotification({title: 'Error', description: "Creation of portal user failed. Please try again.", notificationType: "error"})

Hey @adit!

Is createPortalUser also a JavaScript query or some other kind of query?

It's possible to use "Keep variable references inside the query in sync with your app" (though it does come at some performance cost):

Alternatively, you can add an additional Promise wrapper to your query so that in rejects on failure and then build your handler into your then statement:

const query = new Promise((resolve, reject) =>
  api.trigger({ onSuccess: resolve, onFailure: reject })

  (data) => {/* Handle success */},
  (error) => {/* Handle failure */}

Do either of those work for you?

As a side note:

I had missed this before but if you have your JS query explicitly throw an error that should show up as other query errors would. For instance with:

throw new Error("This is an error message");
1 Like

@Kabirdas First of all, thank you for your prompt reply!!

Your suggestion to add an additional promise wrapper worked :slight_smile: Though on the success notification message, I do not see the nice little icon (Is that because my description is too long?)

Sorry didn't clarify earlier but to answer your questions -
I am using a rest api query! So the first solution you suggested was unfortunately not an option for me but I'm glad the second one worked.

Ah! Thanks for surfacing this. This looks like a bug on our end :grimacing:

Glad to hear the second solution worked for you. I've reported the issue to the dev team and can report back here when it's been fixed!

1 Like

Hey there!

Just want to report that Notification icons should be displaying properly again at the time of writing this (~2.108.2)! There's also an experimental featured flag being rolled out to Cloud orgs that will allow JS queries to fail properly. If you're interested in trying it, please feel free to reach out :slightly_smiling_face:

1 Like

@Kabirdas Thanks for all the information you shared around this so far!

I am currently having a similar problem with the failure condition in my graphql mutations. I will add some screenshots at the end.

Pretty much, I am trying to display a failure notification message when my mutation responds with null data. All the failure conditions are being met and are returning truthy, but the error message isn't coming through. I tried accessing data and metadata and error like it says in the docs, but they keep giving me undefined. I then tried to access the data object in the mutation itself, but that only works when I run the mutation twice.

It looks like the failure conditions are only accessing those properties before the response comes back. So for it to display the correct error info, I have to run the query twice, which does not work for the app we are building. How do I get the failure condition to display the correct error message after the first run of the mutation?

This is before the mutation runs:

This is after the first run:

This is after the second run:

So I figured out what I was doing wrong:

I had to put {{errors[0].message}} in the notification message. Even though it is undefined before the mutation runs, it does display correctly after I run the mutation the first time. However, I now am getting constant errors in the console from this mutation when I update inputs on the app:

After every character I input it throws an error:

I have the query set as Run query only when manually triggered and I understand that errors is not defined until the mutation runs, but why is this error from the Failure conditions getting logged every time I change the inputs?

Hey @thelovesmith!

Nice catch and good callout! The error variable, like data, is something that gets populated over the course of the query actually running so since you're viewing it at a time when they're undefined the linter throws an error, similar to when you pass a variable using additionalScope.

When one of the inputs to your query is recalculated it can cause the other transformers in the query to reevaluate as well. But, like you mentioned, since that also happens before the query actually runs those transformers run into the undefined errors and throw them to the console.

It can be confusing to see them but they can be ignored, there's also an internal ticket for cleaning them up since they're expected!

As a workaround, using something like {{(typeof errors === 'undefined') ? undefined : errors[0].message }} should get rid of the error since it's valid JavaScript whether or not the variable is declared (more info) but that might be a heavy lift for each instance of this :sweat_smile:

1 Like