I'm working on a screen where I sent a request and I get response in two types.One is success and contains the below format:{    searchNames: [        {            CustomerName: John,            CustomerType: Business,            Profession: Businessman,            address: {                address1: null,                address2: null,                city: null,                state: null,                country: null            },            ContactNumber: null        }    ],    Count: 0}The other is failure response and the format differently every time, there can be no error code or message in the format.{status: FAILUREerror: {errorMsg: [1]0:  {fieldName: CustomerNameerrorCode: nullmessage: }errorNo: 12}}I have tried validating the response and displaying the response, but I have no idea whether it is the right way to do the same.This function is called in a promise:function(response){if(response.searchNames){console.log(result, response.searchNames );}else                          {                            var errorString = [Error : Invalid Data];                              if (response.status && response.status.toLowerCase() ===                                  'failure' && response.error && response.error.errorMsg &&                                  response.error.errorMsg.length > 0) {                                  var errorString = [];                                  for (var errMsg in response.error.errorMsg) {                                      errorString.push(response.error.errorMsg[                                          errMsg].message);                                  }}My questions are:Is this the right way to do the error validations, or are there any best practices for this? The error response may or may not contain error message. If there is error message, I should show the error message, otherwise I should show the default error message stored in another variable. In the success response as well is it the right way to check the success response.What is the right way in handling all the conditions and how can I do it?
Handling all error conditions on success or failure response
javascript;error handling