Backbone - Handle empty response from the server - backbone.js

I use fetch to retrieve my models from the server. It works great when I have data in my DB but how to handle an empty response send by the server to the client ?
For example if no data have been already saved by the user the server send a http 200 response with an empty array and backbone triggers an error callback but I just want to inform the user that there is no data saved in DB. In this case an empty response just means there is no model to load and I don't want to create any model with the response.
Here is the code I use :
app.plans.fetch({
success: function(data) {
app.Notifications.updateMessages({text: "Plans loaded."});
},
error: function (){
app.Notifications.updateMessages({text: "Error."});
}
});
How the server can indicate that it's all right but there is just no data ?

If you are responsible for back-end and you can change response behavior you should use http status 204 for empty content response instead of 200
this should help

If server responds with the 200 it shouldn't trigger an error callback. It's weird.
Could you please add the example of the server response?
Also success and error callbacks take the following options as arguments: (collection, response, options)

Related

How to update a Azure AD user?

I have successfully created a new user in Azure AD following Create User reference.
Now I want to update that very same user following Update User reference. To keep things simple This is the JSon content I'm sending:
{
"userType": "T"
}
The documentation tells me to send a PATCH request, but I always receive an HTTP Error 400 (Bad request). If I try sending a POST request I receive an HTTP Error 405 (Method not allowed).
The destination URL is https://graph.microsoft.com/v1.0/users/user-id.
The user-id is the one returned with the user details.
What am I missing?
Update 1
Those are the permissions set to the application:
This is exactly how you update user (PATCH).
However the userType property cannot be T, from the docs you refer:
That property can only have one of the two distinct values: Member or Guest.
Of course, the user-id in path should the id property as returned by the get users operation.
I am pretty sure that if you use a normal REST client will be able to see the whole error message, which will be more meaningful.
If you still have issue - paste the exact error message.

OSB12c - Rest proxy service throwing Translation error in case of invalid JSON input

We have configured REST proxy service that accepts JSON input. If the input is not a well formed JSON OSB is throwing Translation error with HTTP 500 Staus code. Is that possible we can send Customized error message in this scenario
You need to create a global error handler for your pipeline and set the desired error message using a replace action here, followed by a "Reply" action.
Keep in mind that if you try to "read" the original request body in the global error handler, and if the original request was malformed, it will get thrown up to the system error handler and you will get the system error message again.
Here's a sample OSB 12.2.1.1 project you can use to try this: https://github.com/jvsingh/SOATestingWithCitrus/tree/develop/OSB/Samples/ServiceBusApplication1
The accompanying soapui project contains two requests. The malformed request should return this:
(I have only set the response here. You would also need to set the proper content type and decide whether you want to treat this as "success" or "failure" etc. in the reply action)

How to get correct $http error code, instead of -1?

I use $http to retrieve data from server. In case error happen, all statusCode = -1. I cannot get correct errorCode to handle in client. I don't have permission to update server code.
$http(options).then(function (response) {
if (response.status === 200) {
resolve(response.data);
} else {
exceptionHandler(response);
}
}, function (error) {
exceptionHandler(error);
});
console.log is put on exceptionHandler function.
Please help me get correct error code. Many thanks.
In the responses you have shown, the 404 error is available as error.status but the other two responses were denied access by your browser when it performed the preflight OPTIONS request.
In that case the browser returns a status of -1 to indicate that the request was aborted. There is no HTTP status code for your request because it was never sent. The status 500 shown in the console log is not made available to any javascript code.
You should handle a status of -1 as indicating that no response was received from the server, either because it was never sent (this case), or because it was sent but timed out.

Act upon response of AutoSync insert operation

I have a Store with autosync:true.
When loading the store, I'm getting complete models:
[{"id":11,"active":true,"name":"ABC","sens":7,"details":119,"type":13,"acl":false,"create":true,"delete":true,"owner":"alexander","members":"gustave\njerome"}]
When syncing a new model to the server, I'll send it with "id":0, so the server knows it has to create a new one. The server will then respond {"success":true,"data":[12],"debug":[]}, where 12 is the id of the newly created entry.
Now I have to add a callback function for the autoSync operation to patch the ids I receive back into the store.
If I had synced manually, this would have been easy:
Ext.getStore("RightsStore").sync({
success:function() {
}
})
But how can I get a special success or a callback function for insert syncs into a store that works with autoSync?
If the server sent {"success":true, "data"[{"id":12, ....}]} then you do not need to do anything. Best is if server sends back complete records it received for CRUD operation with the updated data (in same order). Ext takes care of the rest.
If you're unable to change the server output as #Saki had mentioned, you can just listen to the load event and update the records with the new id there.
store.on('load', me.loaded, me);
loaded: function(store, records) {
}
More details here - http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Store-event-load

PUT/GET with Payload using Restangular

I am using Restangular in one of my works
The server guys have give me the following calls which i need to integrate on the AngularJS client
PUT api/partners/password – RequestPayload[{password,confirmpassword}]
partner id is being sent in the header
GET api/partners/password/forgot/ - Request Payload [{emailaddress}]
partner id is being sent in the header
The javascript code that I have written to call these services is as follow
Restangular.all('Partners').one('Password').put(params); - sends params as query string
Restangular.all('Partners').one('Password').one('Forgot').get(params); - sends object in the url
I have tried other ways but it simply doesn't make the correct call.
Help me out guys!
So, for point #1. it puts the object at hand, not another object. So you have 2 options:
Option 1
var passReq = Restangular.all('Partners').one('Password');
passReq.confirmPassword = ....
passReq.put(); // confirmPassword and the params of the object will be sent
Option 2 is
var passReq = Restangular.all('Partners').one('Password').customPUT(obj);
For Point #2, you cannot send a request body (payload) in the GET unfortunately.

Resources