Search code examples
javaspring-bootrestjava-8feign

how to continue the program when there is exception while calling feign client api in java


public string prospect(List<ProspectRequest> prospectRequest, String primaryClientId) {
    if(p2p(primaryClientId)=="Success") {
        for(ProspectRequest prospect : prospectRequest) {
            p2p(prospect.getId());
       }
   // rest of code i would like to continue
    }
 }

public string p2p(String id){
crmApi.getProspectId(id);//this is external client api
String message = "Success"; 
return message;
}

if p2p(primaryClientId) is failed then I need to stop the entire process .How do i like to continue with "rest of code i would like to continue".

if the feign client api crmApi.getProspectId is success then the success message is returned and then its a good case. If the crmApi.getProspectId api gives error then I still need to continue the p2p() program for next set of clients also .How does that work.

Thanks in Advance.


Solution

  • Seems like you just need to use a try catch finally block

      public string prospect(List<ProspectRequest> prospectRequest, String primaryClientId) {
      
    
        try {
          if(p2p(primaryClientId)=="Success") {
             for(ProspectRequest prospect : prospectRequest) {
                p2p(prospect.getId());
            }
        } catch (RuntimeException e) {}  // Exception can be specified exp. RuntimeException
        finally {
           // rest of code i would like to continue
        }
        }
     }
    
    public string p2p(String id){
    crmApi.getProspectId(id);//this is external client api
    String message = "Success"; 
    return message;
    }
    

    if you need something out of the first if-block you can put an option (if the if-block fails) in the catch-block. When

    if(p2p(primaryClientId)=="Success") {
                 for(ProspectRequest prospect : prospectRequest) {
                    p2p(prospect.getId());
                }
    

    throws an checked exception you need to change RuntimeExpection to the superclass Exception

    edited:

     for(ProspectRequest prospect : prospectRequest) {
          try {
            p2p(prospect.getId());
    

    } catch (RuntimeExpection e) {} }