Reputation: 647
I am trying to consider the error scenario in the soundcloud's javscript connect oauth2 flow. Here are the steps:
1. Initiate Soundcloud connect using SC.connect(my_callback_fn)
2. a pop-up window opens with a aound-Cloud page requesting user to "Connect" or "Cancel" app authentication.
3. User clicks "Cancel". (This is where I am stuck)
After the user clicks cancel, I see a javascript error from the sdk on the window.opener (aka parent page). I would like to handle this more gracefully.
Currently I have an event handler on window.onerror
to catch the error string and proceed appropriately. I was looking for a better way to handle this error, as this method would be invoked for all the other js errors as well. thanks.
I get the following js error trace:
Uncaught Error: SC OAuth2 Error: The end-user denied the request. sdk.js:1
(anonymous function) sdk.js:1
window.SC.SC.Helper.merge.Dialog.AbstractDialog.AbstractDialog.handleReturn sdk.js:1
window.SC.SC.Helper.merge.Dialog._handleDialogReturn sdk.js:1
window.SC.SC.Helper.merge.connectCallback
Upvotes: 2
Views: 209
Reputation: 1289
It's a little dirty, but the fastest way to catch the exception is to overwrite the dialog's callback. The SC.connect() method returns an instance of the dialog, so you can:
var dialog = SC.connect(function () {
// Connected!
});
var originalCallback = dialog.options.callback;
dialog.options.callback = function () {
try { originalCallback.apply(this, arguments); } catch (e) {
// Error!
}
};
If you don't want to wait for the exception, you can simply use the first argument of the callback to get the error, if any:
dialog.options.callback = function (params) {
if (params.error) console.log(params.error_description);
else originalCallback.apply(this, arguments);
};
Additionally you can set a timer on the dialog object periodically to check if the user closed it, without clicking "Connect" or "Cancel". You can't use onunload since the browser won't let let you set any properties on a window with a different domain (in this case SoundCloud). Which means - if you get an exception trying to access any of the dialog.options.window
's properties - the window is still open.
Note: Another way to catch the error coming from SoundCloud is to directly modify your callback.html (the SoundCloud callback url), since the contents of params
are first passed to your callback URL as a query string and you can check for them simply call a different callback (in the onload function)
Upvotes: 2