Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Handle Apex exceptions passed back to javascript remoting #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 114 additions & 31 deletions RemoteTK.component
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,32 @@ if (remotetk === undefined) {
}

if (remotetk.Client === undefined) {
function handleResult(result, callback, error, nullok) {
function handleResult(result, callback, error, nullok, deferred) {
if (result) {
result = JSON.parse(result);
if ( Array.isArray(result) && result[0].message && result[0].errorCode ) {
if ( typeof error === 'function' ) {
error(result);
}
deferred.reject(result);
} else {
callback(result);
if ( typeof callback === 'function' ) {
callback(result);
}
deferred.resolve(result);
}
} else if (typeof nullok !== 'undefined' && nullok) {
callback();
if ( typeof callback === 'function' ) {
callback();
}
deferred.resolve();
} else {
error([{ message : "Null return from action method","errorCode":"NULL_RETURN"}]);
}
var errorResult = [{ message : "Null return from action method","errorCode":"NULL_RETURN"}];
if ( typeof error === 'function' ) {
error(errorResult);
}
deferred.reject(errorResult);
}
}

/**
Expand All @@ -72,11 +83,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.describe = function(objtype, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.describe}', objtype, function(result){
handleResult(result, callback, error);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.describe}', objtype, function(result, err) {
if (err.status) {
handleResult(result, callback, error, false, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}

/*
Expand All @@ -89,11 +109,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.create = function(objtype, fields, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.create}', objtype, JSON.stringify(fields), function(result){
handleResult(result, callback, error);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.create}', objtype, JSON.stringify(fields), function(result, err) {
if (err.status) {
handleResult(result, callback, error, false, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}

/*
Expand All @@ -106,11 +135,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.retrieve = function(objtype, id, fieldlist, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.retrieve}', objtype, id, fieldlist, function(result){
handleResult(result, callback, error);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.retrieve}', objtype, id, fieldlist, function(result, err) {
if (err.status) {
handleResult(result, callback, error, false, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}

/*
Expand All @@ -126,11 +164,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.upsert = function(objtype, externalIdField, externalId, fields, callback, error) {
Visualforce.remoting.Manager.invokeAction('$RemoteAction.RemoteTKController.upser', objtype, externalIdField, externalId, JSON.stringify(fields), function(result){
handleResult(result, callback, error, true);
}, {
escape: false
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('$RemoteAction.RemoteTKController.upser', objtype, externalIdField, externalId, JSON.stringify(fields), function(result, err) {
if (err.status) {
handleResult(result, callback, error, true, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}

/*
Expand All @@ -144,11 +191,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.update = function(objtype, id, fields, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.updat}', objtype, id, JSON.stringify(fields), function(result){
handleResult(result, callback, error, true);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.updat}', objtype, id, JSON.stringify(fields), function(result, err) {
if (err.status) {
handleResult(result, callback, error, true, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}

/*
Expand All @@ -160,11 +216,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.del = function(objtype, id, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.del}', objtype, id, function(result){
handleResult(result, callback, error, true);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.del}', objtype, id, function(result, err) {
if (err.status) {
handleResult(result, callback, error, true, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}

/*
Expand All @@ -175,12 +240,21 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.query = function(soql, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.query}', soql, function(result){
handleResult(result, callback, error);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.query}', soql, function(result, err) {
if (err.status) {
handleResult(result, callback, error, false, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
}
return deferred.promise();
}

/*
* Executes the specified SOSL search.
Expand All @@ -190,11 +264,20 @@ if (remotetk.Client === undefined) {
* @param [error=null] function to which jqXHR will be passed in case of error
*/
remotetk.Client.prototype.search = function(sosl, callback, error) {
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.search}', sosl, function(result){
handleResult(result, callback, error);
}, {
var deferred = $.Deferred();
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteTKController.search}', sosl, function(result, err) {
if (err.status) {
handleResult(result, callback, error, false, deferred);
} else {
if (typeof error === 'function') {
error(err);
}
deferred.reject(err);
}
}, {
escape: false
});
return deferred.promise();
}
}
</script>
Expand Down