Must I call sendResponse? - javascript

I am using chrome.extension.onRequest.addListener, I see that I am calling sendResponse with no arguments (=> sendResponse(); ), Sometimes I get an error like this:
Error: Attempting to use a disconnected port object
Must I call sendResponse, or I can remove this function if I am not expecting to get a response from the background?

The documentation says:
Function to call (at most once) when you have a response.
This "at most once" sort of indicates that sending a response is optional. While I won't be able to get you an official confirmation, I checked the source code of my extension and there are several messages where sendResponse isn't being called - so far (after a year of heavy use) no issues.

Related

Best way to handle chrome.runtime.lastError

as you may all-ready know lastError is a global variable or property from chrome.runtime API used for determining if some error happened during the chrome API call execution.
It is defined only when there was an error, if the API call is OK it won't be defined. The error can be triggered from multiple reasons eg. bad API call due to missing or wrong parameter type, or it can be caused by user interaction, eg. the user rejects permission granting by pressing the "cancel" button.
Chrome has unified it's error handling by using this variable for error reporting across their API's instead of returning an error argument. Due to it's API's async nature Chrome makes an additional check that validates if chrome.runtime.lastError is checked/handled in the callback function from the extension code, if not it throws the famous Unchecked runtime.lastError
You can quickly get the error message by checking chrome.runtime.lastError.message and display it to the user, and all of this is great for most use cases, but I wouldn't write this question if that were my case.
So what happens when you need to implement some additional code logic based on the error result. Let's take launchWebAuthFlow for an example, for the sake of simplicity of the question, I will focus only on the two possible outcomes/errors that can happen:
User interaction required. thrown when API call with bad argument value is made, in this case interactive = false
The user did not approve access. happens when the user closes the authentication window
So my question is, what will be the best way to implement additional logic based on the error?
Is a comparison of chrome.runtime.lastError.message the only way to do it, and if so, is it safe considering that the user browser language might be different than English.
What's your opinion, should Chromium dev team implement: chrome.runtime.lastError.code?

What does observer.complete() do?

In rxjs what exactly does the observer.complete() do after observer.next() ?
From the documentation observer.complete notifies the Observer that the Observable has finished sending push-based notifications.
In the other hand, observer.complete it's a callback function and an Observable calls this method after it has called next() for the final time, if it has not encountered any errors.
In ReactiveX library, there are two types of messages.
First ones are ordinary messages. Ordinary messages are the ones sent with .next() and there can be 0-many of them.
Second type are notifications. These can be of two types - error and success. The error is sent with .error() and give some error details in it (like exception) and success is sent with .complete() meaning that there will intentionally be no messages. Every observable should end with a single error or a single success notification.

Differentiate between different error types on image load

I'm playing around with implementing a JavaScript server ping tool based on the accepted answer given on this question: Is it possible to ping a server from Javascript?. This essentially works by assuming the pinged server is down if no response has been given after n milliseconds.
That's great, and it's a pretty cool way of doing it, however there are two rather large pitfalls:
Not all servers do respond within the allocated time.
Sometimes an ERR_CONNECTION_TIMED_OUT error is thrown before our timeout timer has finished.
Both of these things cause incorrect results. The former suggests that the server is offline when it's possibly online and responding slowly, and the latter suggests the server is online when it's (likely) offline.
In an ideal world this code would capture what type of error thrown was thrown and handle this appropriately. After all, if the error thrown is a 404 Not Found error, this counter-intuitively means the server is online and has responded.
If we log the image error event, the only thing we see surrounding the error is:
Event {
...
type: "error"
}
There's no message or anything hinting at what the error thrown was, and both the 404 and ERR_CONNECTION_TIMED_OUT errors give identical information.
What can I do to capture the ERR_CONNECTION_TIMED_OUT error I see in Chrome's JavaScript console, rather than relying on a fixed-speed timer?
Update
The best way I can replicate this issue is by altering Trante's JSFiddle demo (as linked to in the question I've linked above) to use a 30000ms timer rather than a 1500ms timer:
this.timer = setTimeout(function () {
if (_that.inUse) {
_that.inUse = false;
_that.callback('timeout');
}
}, 30000);
The 'unknown' server should obviously not respond, but instead we see this:
In Chrome's console, the following error has been thrown:
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
As the Image's onerror function has been fired with the generic error as given above, the function believes this to mean that 1. 'unknown' exists, and 2. it's online. The ERR_NAME_NOT_RESOLVED error appears to be something which only Chrome is aware of, and isn't passed through to the error event at all.
Update 2
Today I tried doing this with web sockets instead of images and unfortunately these suffer from the same problem. The only data surrounding the error returned is type: "error" - no information about what the error actually was.

Callback not called? Chrome identity API getProfileUserInfo()

From the Chrome console window of my Chrome app, I'm making these calls:
window.navigator.appVersion.match(/Chrome\/(.*?) /)[1];
"37.0.2062.120"
chrome.identity.getProfileUserInfo(console.log)
undefined
Why is the callback (console.log) never called? It should print a userInfo object returned w/o needing network IO. I'm getting no callback in the console, or in the code run in my app.
The API is here: https://developer.chrome.com/apps/identity#method-getProfileUserInfo
What did I miss?
Thanks!
console.log is not a JavaScript function. Pass a function (an anonymous function will do) with an argument to the API call, and see if you get something then. Also, this API requires that the manifest.json file have "identity" permission, and (1) you may not have that in the manifest, or (2) you do, but somehow the API call isn't permitted when typing directly to the console (something I personally have never done). If your tests indicate that #2 is a possibility, put the API call into a JavaScript file and try it that way.

I can't see what went wrong with this WinJS.xhr call

I'm using WinJS.xhr to call a ReST service... when I call the url on the browser I can see the complete returned xml. So I want to parse that xml to show some of the data.
WinJS.xhr({ url: "http://myserver/myservice" }).
then(processPosts, downloadError);
The problem is my downloadError function does not have parameters so I have no idea what went wrong.
What am I missing?
The help page is not very helpful :(
Edit: I used to fiddler to see what's on the wire and I don't see the request. The server I'm targeting is my own LAN, I also tried with its IP Address with same results (none)
When there is an error the callback function will take one parameter. The downloadError will need to take in one parameter. If you define downloadError as follows you should get more details. The result type should be XMLHttpRequest and using that you can see the status of the request and why it failed.
function downloadError(result){
//check the result param.
}
EDIT:
Check the app capabilities in your application.AppManifest file. The capabilities section is where you define what capabilities are required by your app for example connect to the internet, use the webcam.

Categories