I have a server and client side GAS app. The client side needs to run a function on the server. If for any reason, (like no internet connection), the connection is unsuccessful, I want it to gracefully alert the user. I tried this, but it doesn't work:
try{
google.script.run
.withSuccessHandler(onSuccess)
.myFunction();
return;
}
catch(e){
alert('Cannot connect to server: ' + e.message());
}
Using Chrome 46, the console spits out the error correctly: "net::ERR_INTERNET_DISCONNECTED". However, the alert doesn't appear. The console also says: "Uncaught NetworkError: Connection failure due to HTTP 0".
Why am I not able to catch the exception?
edit: as explained here (https://softwareengineering.stackexchange.com/questions/144326/try-catch-in-javascript-isnt-it-a-good-practice), maybe it has to do with the async nature of the calls. Can someone confirm this?
I want to confirm whether Apps Script functions in a spreadsheet container-bound project can or cannot be called via google.script.run from within a JavaScript function in an HTML template that was loaded by the HTMLService. My initial findings indicate "not" (see below), but I might be missing something.
The google.script.run call, when made from within the JS function called by the onclick handler, consistently routes to the error handler function, with the following error code: NetworkError: Connection failure due to HTTP 0.
If I call the google.script.run code directly from the onclick handler of a button, it does run. I'd prefer to be able to use it in a function, so that I can build up a set of data to pass into the real method I'm planning for the implementation.
The error you see is because you close the sidebar with that line, severing connection to the server-side script.
Related
I'm trying to log a custom message to Sentry and I'm doing it using
Sentry.captureMessage("This is a test");
When this function is executed on my application I can see in the Dev Tools Network tab that the request is executed successfully:
And it also returns an event ID:
{"id":"b1db5de34d6048b38f76077ed1315fe9"}
The problem is that when I check the issues tab on the Sentry project, the message is nowhere to be found. There are other issues for the application but these are things like unhandled errors.
Are there some steps I need to follow to log messages from the captureMessage function? Can I just immediately go to the event since I have the event ID?
After a usually-short delay, you can get to the related issue in sentry by visiting a URL like: f"https://sentry.io/{organization_slug}/{project_slug}/events/{event_id}".
I have some baffling behavior in my development environment for a ReactJS application which makes some API calls with fetch() and is bootstrapped with create-react-app
When handling the API call, I throw an error which is caught in a middleware elsewhere that wraps around every API call.
I have put console.log() in the catch() block in this middleware and confirmed it is catching all of these errors. Plus, it displays the error message correctly to the end user, and does not crash the application.
But the weird thing:
When I'm throwing an error explicitly with throw, the ReactJS error overlay pops up and shows it to me.
When, instead, the same code throws an error on the json() call (that is, the API call didn't return valid JSON), this is again caught by the same middleware and the error message is displayed the same way in the UI, but the ReactJS error overlay does NOT show up.
I even tried throwing SyntaxError explicitly to see what would happen, but in that case the error overlay does show up.
My question is, basically: is this anything to worry about? The error overlay is only functional on the development environment after all, and I'm not sure if it actually indicates that these errors will cause crashes/problems in production. I am not sure what the rules are for when it shows up or not. Is it expected that the overlay won't show up for an uncaught Json.parse() error, but will show up for everything else? When the error overlay shows up, is it anything to worry about if I can just close it and the app itself is still working correctly?
Errors in React are caught by Error Boundaries.
According to docs they can't catch errors in:
Event handlers
Asynchronous code (e.g. setTimeout or requestAnimationFrame callbacks)
Server side rendering
Errors thrown in the error boundary itself (rather than its children)
fetch() is an asynchronous code and React can't catch those errors. You should have try/catch block for async code or .catch() handler for promises to handle errors yourself (for example update React state to indicate that an error has happened and show the error message).
I have a Parse Cloud Code written in JavaScript, I have an code-based system so I have 3 different modes:
Code is found and have't been used before - SUCCESS
Code is found and have been used before - ERROR
Code isn't found - ERROR
Now, when the client (iOS) gets the success - everything works fine and the code is OK. But I want to determinate between the 2 errors I have and tell the user what's wrong (your code isn't available anymore/code not found), but the error code Parse sends is always 141, so I don't know which message should I print to the user.
How can I change the error code of the error/another way to detect what error is it on client/server side?
Thank you!
From https://parseplatform.github.io/docs/js/guide/#error-codes:
ScriptFailed 141 Cloud Code script failed. Usually points to a JavaScript error. Check error message for more details.
FunctionNotFound 141 Cloud function not found. Check that the specified Cloud function is present in your Cloud Code script and has been deployed.
JobNotFound 141 Background job not found. Check that the specified job is present in your Cloud Code script and has been deployed.
SuccessErrorNotCalled 141 success/error was not called. A cloud function will return once response.success() or response.error() is called. A background job will similarly finish execution once status.success() or status.error() is called. If a function or job never reaches either of the success/error methods, this error will be returned. This may happen when a function does not handle an error response correctly, preventing code execution from reaching the success() method call.
MultupleSuccessErrorCalls 141 Can’t call success/error multiple times. A cloud function will return once response.success() or response.error() is called. A background job will similarly finish execution once status.success() or status.error() is called. If a function or job calls success() and/or error() more than once in a single execution path, this error will be returned.
So looking above, you could have a Javascript error occurring during the function's run. You could be spelling the function name wrong when calling. You could forget to call response.success() to properly close the function. Or you could be calling response.success() / response.error() too many times, make sure that as you follow your code's logic, you only hit one of these for every path.
If you could post the code for your Cloud Code, we might be able to further help you.
I am trying to test some code that uses web workers. I want to check that the error path works; i.e., that an onError handler correctly recovers from an exception thrown in the web worker.
The problem I'm running into is that the exception propagating out of the web worker causes it to be considered unhandled. For example, it prints in the browser's console log and causes my testing environment (a simple wrapper around Karma) to consider the test as failed.
So, How do I indicate to the browser/Karma that an exception bubbling out of a given web worker is expected, and should not be considered unhandled? I don't want it to print to the console, and I want my test to pass.
The only idea I've come up with is to wrap the web worker code in a try/catch and marshal the caught exception out via postMessage, but that requires throwing away quite a lot of information because of the need to stringify the error object (otherwise it triggers a data clone error).
Call preventDefault on the error event object given to the onError handler.
worker.onError = function(e) {
e.preventDefault(); // <-- "Hey browser, I handled it!"
...
}
I'm using the cfwebsocket tag in Coldfusion to create a web socket connection.
I looked at an example from here http://www.sagarganatra.com/2012/03/html5-websockets-in-coldfusion-10.html
and near the end it shows you all the javascript calls you can make on the web socket object.
However, when I try to make any call on it I get an error that it is undefined.
For example I have:
<cfwebsocket name="ws" onMessage="messageHandler" onOpen="openHandler" onClose="closeHandler" onError="errorHandler" subscribeTo="chat" />
and in my javascript i call
alert(ws.isConnectionOpen());
and I get the error in firebug: TypeError: ws is undefined.
Anyone know why I can't call it?
My chat works fine and I can connect and chat properly. I just wanted to close the connection when the chat ended so I was looking into how it's done calling the websocket but I don't know why it's not working.
Note that I am using jQuery and the javascript is wrapped in the document ready.
First, you can't interact with the ws object till it establishes a connection to the server.
There are a couple of ways to handle this scenario. You can use the "onOpen" attribute and have it call a function once the web socket connect has been established.
However, you are probably better off just using the "onMessage" attribute and create a generic listener function that processes all web socket messages from the server.
function messageHandler(msg) {
if (msg.type == 'response' && msg.reqType == 'welcome'){
alert('user connected');
}
}