The JS API is really great for implementing handy shortcuts for both myself, and end-users. I've used it a few times to perform batch operations on assets, and I'm really quite pleased with what it can do.
One of the things it lacks though, is a machine-readable return status when you perform an action. I've found myself using a regular expression to test for the phrase " successfully ", but obviously that's a bit of a hacky workaround and I'm sure liable to change at any point.
What would be nice is if we could standardise on an additional return status to indicate whether an action has been successful. "action":"false" for failure "action":"true" for success, or something thereabouts. Thoughts?
(Having thought about it outside of work hours, I've just realised it's probably returning a relevant HTTP status which I could check against. I'll have to do some investigation.)
Return Statuses for the JS API
AshKyd
(Squiz)
#1
Anton_Babushkin
(Anton Babushkin)
#2
I happen to agree actually. It makes error handling far more cleaner.
If it used jQuery's style of returning the status that would also be neat. i.e.:
createAsset(..., { success: function(data) { }, error: function(errorMessage) { } });
Anthony_Barnes
(Anthony Barnes)
#3
In most cases if there has been an error during processing the JS API should return {error: "…message…"}, although in some cases figuring out what happened requires parsing the error text. To test for errors you can check:
// callback function from JS API returning 'data' if (typeof(data.error) !== "undefined") { // There was an error alert(data.error); }
If there is a case where there is an error and it doesn't return it via the JSON response, best to lodge it in the bug tracker. It could use some more consistency, but as with all things that evolve over time it takes a little work and community feedback to improve.