React query cancel / abort mutation if paused / offline - react-native

I'm using some mutations in a React Native app. I'd like to set a default behavior for all mutations so when I'm in offline mode and I try to make a mutation, display an error and do nothing. Right now, I've been able to catch this by using
mutationCache: new MutationCache({
onMutate: async (_, mutation) => {
if (mutation.state.isPaused) {
toastError("Network error!", "Please check your network connection");
mutation.state.status = "error";
}
}
})
where I set up my QueryClient(), but, when I switch to online mode again, the mutation gets fired. I'd like to avoid this situation, and just abort / discard / cancel the mutation instead of automatically resume it.
I've also tried calling mutation.destroy() but nothing happens.
How can I do it? Note I've tried to "force" the mutation status to "error" but I've still getting the same behavior.

I think there is nothing currently in react-query that would support this. The idea is that if a mutation has been fired, but it cannot be done because you're offline, the execution will be "deferred" until you are online again.
What you can do is:
not allow firing the mutation when you are offline. So basically, disable the button or whatever triggers the mutation.
set networkMode: 'always' (available in v4). This will fire mutations regardless of online / offline status. If you are offline, the mutation will fail with a network error. Then, you can display an error message. I think this is what you want given the requirements of "when I'm in offline mode and I try to make a mutation, display an error and do nothing."

Related

Cancel all sagas in Redux-saga

I'm using React Native and I need to cancel and recreate all sagas on hot reload. I know about cancel(tasks) but the problem is that I have no reference to tasks after hot reload (if I assign them to a variable they get undefined) but some sagas still "hold on" to the previous app state for some reason and I need to cancel them all without having a reference to them.
How can I cancel all sagas, without having a reference to them explicitly, e.g. something like sagaMiddleware.cancelAll() (this method doesn't exist I made it up for example)?

Event "formSubmission" doesn't trigger on Circuit SDK

Circuit client in my app subscribed to event 'formSubmission':
client.addEventListener('formSubmission', function (event)
Until last days it works fine, but now it doesn't trigger.
I tried to test it by creating a simple app that just send a form (just one button on it) as a reply to user's message and should log form submissions, but it doesn't go on this function.
client.addEventListener('formSubmission', function (event) {
var submittedValue = event.form.data[0].value;
console.log(`[CIRCUIT]: Form was submitted.`);
};
Any other events like 'itemAdded' or 'itemUpdated' work as they should
Yes, just confirmed this is a bug and is being worked on. Will post here when its resolved. Are you using the circuitsandbox.net or production?
Production systems have been updated and the formSubmission event is received again. circuitsandbox will be updated with this fix soon.

How to implement global error handling in Vue

I want to have global error handling in Vue.JS, like the error handling system in Angular 2+. I have tried so much but I could not find a good approach to implement this handling.
Imagine you have many service methods and that these methods should run one after the other (I mean inside each other) so writing then and catch method inside the prevoius service is so ugly and unclean and now I'm looking for clean way to implement such way. I hope you understand what I mean.
As #Badgy mentioned you can install a Vue error handler to catch errors Vue encounters. This can be done as follows:
Vue.config.errorHandler = function (err, vm, info) {
// handle error
// `info` is a Vue-specific error info, e.g. which lifecycle hook
// the error was found in. Only available in 2.2.0+
}
The above code can be located anywhere you like in your javascript. I locate the code just before I create my vue instance. i.e before my var app = new Vue({...}); code. Because it's a global vue error handler it will handle errors from all instances of vue as well as vue components. I find that in practice it mostly catches errors that occur in the vue render methods.
You can read more about it in the official docs here: https://v2.vuejs.org/v2/api/#errorHandler
For more general (non vue related) javascript errors you still need a global error handler like so:
window.onerror = function (msg, url, line, col, error) {
//code to handle or report error goes here
}
Again, this code can be placed anywhere javascript is allowed but typically you will want to place it to run early in your javascript stack. You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
And finally, to catch a "Promise rejection" (i.e. an exception throw from a Promise function) we need to listen for unhandledrejection events since a Promise rejection is not caught by the window.onerror mechanism (thanks to #Blauhirn for the tip). In some browsers (Chrome and Edge currently) Promise rejections can be caught with the following approach:
window.addEventListener('unhandledrejection', function(event) {
//handle error here
//event.promise contains the promise object
//event.reason contains the reason for the rejection
});
For more info see this StackOverflow question: Catch all unhandled javascript promise rejections
I hope i understood your Question Right, but this could be what you are Looking for.
errorCaptured
Type: (err: Error, vm: Component, info: string) => ?boolean
Details: Called when an error from any descendent component is
captured. The hook receives three arguments: the error, the component
instance that triggered the error, and a string containing information
on where the error was captured. The hook can return false to stop the
error from propagating further.
Here is more in-Depth Info About it.
Be careful its Vue 2.5.0+

Selenium - watch for an error condition AND run 'happy path' test code

My app displays an error dialog whenever a JavaScript error occurs. This is always a bad sign, so I want to set up my tests so that, if the error dialog appears, it causes the test to fail there and then.
So I'd like to do something like (very much pseudocode!);
// start a new 'guard' thread;
start {
found = this.driver.wait(untilVisible(By.css('.myErrorDialog')), VERY_LONG_TIMEOUT);
if (found) {
// the error dialog appeared! That's bad!
throw();
}
}
// now run the test
login();
clickButton();
testBannerContains();
But I'm having trouble and I think it has to do with the way Selenium schedules actions.
What I've found is that for a single driver, I can only schedule one thing at a time, so the guard I set up early in the test blocks the body of the test from starting.
Is there a better way to handle conditions like 'this should never happen', or a way to create two independent threads in the same test?
So the problem with the code you have is that it immediately runs it and waits for a VERY_LONG_TIMEOUT amount of time for that error dialog to appear. Since it never does, it continues to wait. You have already discovered that is not what you want... ;)
I haven't done anything like this but I think you want a JS event handler that watches for the event that is triggered when the error dialog appears. See the link below for some guidance there.
Can my WebDriver script catch a event from the webpage?
One option would be to watch for that event to fire and then store true (or whatever) in some JS variable. Before leaving a page, check to see if the variable is set to true and if so, fail the test. You can set and get JS variables using JavascriptExecutor. Some google searches should get you all you need to use it.

WinJS app with an uncatchable crash

I have random crashes in my WinJS application when navigating between pages.
The problem is that these crashes never occurs when the app is attached to the Visual Studio debugger; so I can't find where they come from.
I use the WinJS.Application.onerror event to prevent crashes, and log what happens, but as this works well when I try with a random exception, my "uncatchable" crashes doesn't seem to fire this event (I don't have anything logged).
Do you have any idea of what could cause these crashes, or any solution to find more informations ?
Sometimes errors can't fire the WinJS.Application.onerror for several reasons (in my app, the problem was in an iframe, in a page not using winjs).
When it happens, errors can be found in the event log, under "administrative events"
Found this on this link :
http://www.davepaquette.com/archive/2012/09/15/windows-8-store-app-crash-logs.aspx
Jason gives a good solution to this problem in this video (start at time 14:48). In his example, the app was crashing if you had a callback and navigated to a different page before the callback completed. Could this be the case for your app? Any more details on what is going on when you navigate?
For others (since it seems you already know about this!):
To be able to debug easier, use the WinJS.Application.OnError event. Wire up an event handler that dumps out information about the problem before the app crashes.
WinJS.Application.onerror = function (info) {
var err = {
errorMessage: info.detail.errorMessage,
errorUrl: info.detail.errorUrl,
errorLine: info.detail.errorLine,
errorCharacter: info.detail.errorCharacter,
};
Windows.Storage.ApplicationData.current.localFolder
.createFileAsync("crash.txt", Windows.Storage.CreationCollisionOption.openIfExists)
.then(function (file) {
Windows.Storage.FileIO.appendLinesAsync(file, [JSON.stringify(err)]);
});
};
The final stop for exceptions in JavaScript is actually window.onerror; not every exception will get thrown through WinJS.Application.onerror. Try hooking window.onerror directly.