/
REST Service Error

REST Service Error

Search the IFS Workflow Wiki
 

Back to Troubleshooting Articles      

REST Service Error Issue

Issue: When making a REST Service call, we can check the HttpResponseCode to see whether the call was successful, failed, or something else happened. Depending on the outcome, we can handle errors in different ways. 

Resolution: Assuming the REST call fails, currently, we are adding the Error Message(s) to the Case Notes as well as displaying an Error Meesage like we do with validation, except this time we state that errors have been found and that the user should check the case notes for more information.

To achieve this, we first check the response code from any REST calls we make, then, if the code is an error code (e.g. 400) we add the the response body of the call to a PM global array - @errorMessage - which we then use in a subsequent trigger to output to the Case Notes as well as display the error message.



Step 1 - REST Service Call
In this example, we are looping through the PM global array, @@createNewDois, making a REST call for each element, and then either using the returned value if the call was successful ($httpResponse == 200), simply removing the element from the array if wasn't succesfull, but also didn't Fail (in this case, $httpResponse == 404 is an acceptable response), or if the REST call failed outright (any other $httpResponse code), then we add the $restResponse to @@errorMessages and also remove the element from the @@createNewDois array.
$i = 1;
foreach ( @@createNewDois as $key => &$doi ) {
    $newEntitlementDoi = $doi ['newEntitlementDoi'];
    $marketingGroupId = $doi ['marketingGroupId'];
    $reportCenterName = $doi ['reportCenterName'];
   
    $description = "REV DOI " . $reportCenterName . " created from " . $newEntitlementDoi;
    $description = str_replace ( " ", "+", $description );
   
    $url = @@apiUrl.'/api/financial/v1/dois/revFromEnt?username='.@@euUserNameUrl.'&entDoiUid=' . $newEntitlementDoi . '&marketingGroupUid=' . $marketingGroupId . '&description='.$description;
   
    $ch = curl_init ( $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
    curl_setopt($ch, CURLOPT_HTTPHEADER, array (
        'Content-Type: application/json',
        'Authorization: Bearer '.@@apiToken
        ) );
   
    $restResponse = curl_exec ( $ch );
    $httpResponse = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
    curl_close ( $ch );
   
    if ($httpResponse == 200) {
        $contents = utf8_encode ( $restResponse );
        $jsonArray = json_decode ( $contents, true );
       
        $doi ["revId"] = $jsonArray ["id"];
        @@newRevDoiGrid [$i] = array (
            'newEntitlementDoi' => $newEntitlementDoi." - ".$doi['oldEntitlementDescription'],
            'marketingGroupId' => $marketingGroupId,
            'marketingPartyIdAndName' => $doi ['marketingPartyIdAndName'],
            'revId' => $jsonArray ["id"]. " - " . $doi ['oldRevenueDescription']
            );   
        $i++;
    } elseif ($httpResponse == 404) {
        // Creating new DOI failed as no Parent Found - therefore remove from $createNewDois so it won't error out on future steps
        unset ( @@createNewDois [$key] );
    } else {
        @@errorMessages ["CreateNewRevDoi:" . $newEntitlementDoi.'-'.$marketingGroupId] = 'Response Body: ' . $restResponse.':::'.$url;
           
        // Creating new DOI failed - therefore remove from @@createNewDois so it won't error out on future steps
        unset ( @@createNewDois [$key] );
    }
    // unset needed when using reference variables in foreach loops
    unset ( $doi );
}
Note: As we are looping through numerous REST calls, we need to ensure that each error message gets recorded, therefore we create a unique key for each element within the associate array @@errorMessages:
@@errorMessages ["CreateNewRevDoi:" . $newEntitlementDoi.'-'.$marketingGroupId] = 'Response Body: ' . $restResponse.':::'.$url;

Step 2 - Error Handling
Here, we have a stand alone trigger that takes Checks to see if @@errorMessages is empty, and if it's not, we loop through each element, adding the value to the Case Notes, publishing the error message, telling the user to check the case notes, then finally, clearing out @@errorMessages so as to not cause false errors in further steps.


    if (is_array(@@errorMessages) && count(@@errorMessages) > 0){
        $oCaseNotes = new AppNotes();
        $emailMessage ='';
        foreach(@@errorMessages as $message){
            $emailMessage .= $message.PHP_EOL;
        }
        $response = $oCaseNotes->postNewNote(@@APPLICATION, @@USER_LOGGED, $emailMessage);
        $g = new G();
        $g->SendMessageText("Errors have occurred - Please see the case notes for details", "ERROR");
       
        @@errorMessages = array();
    }


Copyright© 2024 IFS AB. Copying prohibited. All rights reserved.