How do Java errors (ExecutionResult.ABORT) map to FaultRules

Not applicable

In the apigee example "java-cookbook" the java code returns ExecutionResult.ABORT when the query "city" parameter is none of milan, rome, or venice. In the apiproxy/proxies/default.xml there is a single FaultRule with name="MissingCityName" and the Java ABORT seems to map to this FaultRule however I don't see anything in the Java code which specifies this FaultRule (in this example there is only the single FaultRule). Question: if I have multiple points of possible errors that I check for in my Java code, and each potential error needs to have its own specific xml response, how do I map each error to its particular FaultRule? For example if I extend this code example to need both city and state parameters, then I want to reply with xml specifying that the city param is missing, or that the state param is missing, or both are missing, or that the city, state does not exist (et cetera, et cetera). How do I do deal with this scenario?

Solved Solved
1 1 343
1 ACCEPTED SOLUTION

Hi Pete! (me again!) Gooood question!

In the cookbook, the proxy endpoint has this faultrules section:

<FaultRules>
    <FaultRule name="MissingCityName">
        <Condition>fault.name = "ExecutionReturnedFailure"</Condition>
        <Step>
            <Name>Fault.MissingCityName</Name>
        </Step>
    </FaultRule>
</FaultRules>

The name of the FaultRule here is not related to the Java code. It is used by Edge for management and diagnostics of the particular faultrule. The thing that relates THIS rule to the Java code is the condition:

 <Condition>fault.name = "ExecutionReturnedFailure"</Condition>

...which merely checks for "ExecutionReturnedFailure". This, as you can surmise from the name, is a general error, and it is returned from/by any Java callout that returns ExecutionStatus.ABORT, or throws an Exception.

If you would like to map specific errors within the Java code to specific Fault rules that run (or, perhaps better, specific AssignMessage policies that you would use within a FaultRule to set the output message), you can set a flag in your Java code to denote the specific error. For example.

try {

    int woeid = 0;
    String cityName = messageContext.getMessage().getQueryParam("city").toUpperCase();


    if(cityName.equals("MILAN"))
            woeid=718345;
    else if (cityName.equals("ROME"))
            woeid=721943;
    else if (cityName.equals("VENICE"))
            woeid=725746;
    else {
       messageContext.setVariable("citylookup_faulttype","InvalidCityName"); // <-- THIS 
       throw new Exception();
    }


    messageContext.getRequestMessage().setQueryParam("w", woeid);
    return ExecutionResult.SUCCESS;


} catch (Exception e) {
        return ExecutionResult.ABORT;
}

That would allow you to do something like this in the FaultRules:

<FaultRules>
    <FaultRule name="MissingCityName">
        <Condition>fault.name = "ExecutionReturnedFailure" and citylookup_faulttype = "InvalidCityName"</Condition>
        <Step>
            <Name>AssignMessage-MissingOrInvalidCityName</Name>
        </Step>
    </FaultRule>

    <FaultRule name="LookupFailed-this-Name-is-not-related-to-the-java-code">
        <Condition>fault.name = "ExecutionReturnedFailure" and citylookup_faulttype = "LookupFailed"</Condition>


        <Step>
            <Name>AssignMessage-ServerErrorLookupFailed</Name>
        </Step>
    </FaultRule>

    <FaultRule name="CatchAll">
        <Step>
            <Name>AssignMessage-UnknownError</Name>
        </Step>
    </FaultRule>
</FaultRules>
 

Does this help?

View solution in original post

1 REPLY 1

Hi Pete! (me again!) Gooood question!

In the cookbook, the proxy endpoint has this faultrules section:

<FaultRules>
    <FaultRule name="MissingCityName">
        <Condition>fault.name = "ExecutionReturnedFailure"</Condition>
        <Step>
            <Name>Fault.MissingCityName</Name>
        </Step>
    </FaultRule>
</FaultRules>

The name of the FaultRule here is not related to the Java code. It is used by Edge for management and diagnostics of the particular faultrule. The thing that relates THIS rule to the Java code is the condition:

 <Condition>fault.name = "ExecutionReturnedFailure"</Condition>

...which merely checks for "ExecutionReturnedFailure". This, as you can surmise from the name, is a general error, and it is returned from/by any Java callout that returns ExecutionStatus.ABORT, or throws an Exception.

If you would like to map specific errors within the Java code to specific Fault rules that run (or, perhaps better, specific AssignMessage policies that you would use within a FaultRule to set the output message), you can set a flag in your Java code to denote the specific error. For example.

try {

    int woeid = 0;
    String cityName = messageContext.getMessage().getQueryParam("city").toUpperCase();


    if(cityName.equals("MILAN"))
            woeid=718345;
    else if (cityName.equals("ROME"))
            woeid=721943;
    else if (cityName.equals("VENICE"))
            woeid=725746;
    else {
       messageContext.setVariable("citylookup_faulttype","InvalidCityName"); // <-- THIS 
       throw new Exception();
    }


    messageContext.getRequestMessage().setQueryParam("w", woeid);
    return ExecutionResult.SUCCESS;


} catch (Exception e) {
        return ExecutionResult.ABORT;
}

That would allow you to do something like this in the FaultRules:

<FaultRules>
    <FaultRule name="MissingCityName">
        <Condition>fault.name = "ExecutionReturnedFailure" and citylookup_faulttype = "InvalidCityName"</Condition>
        <Step>
            <Name>AssignMessage-MissingOrInvalidCityName</Name>
        </Step>
    </FaultRule>

    <FaultRule name="LookupFailed-this-Name-is-not-related-to-the-java-code">
        <Condition>fault.name = "ExecutionReturnedFailure" and citylookup_faulttype = "LookupFailed"</Condition>


        <Step>
            <Name>AssignMessage-ServerErrorLookupFailed</Name>
        </Step>
    </FaultRule>

    <FaultRule name="CatchAll">
        <Step>
            <Name>AssignMessage-UnknownError</Name>
        </Step>
    </FaultRule>
</FaultRules>
 

Does this help?