How to capture 413 error in Apigee?

Can you please suggest to me how I can capture a 413 error in Apigee? I added the below conditions in proxy <faultRule>. Tried all 3 conditions, none of them worked.

error.errorcode = "protocol.http.TooBigBody"

error.state.code = 413

error.class="com.apigee.errors.http.user.requestTooLarge"

<FaultRule name="Too Large Request">

<Condition>(error.errorcode = "protocol.http.TooBigBody")</Condition>

<Step>

<Name>RF-TooLargeRequest</Name>

</Step>

</FaultRule>

Tried the same condition in <DefaultFaultRules>, it still didn't work. Appreciate your suggestion.

0 15 971
15 REPLIES 15

sujnana
Participant IV

Try this - error.status.code=413.

I tried that. It did not work.

I assumed the 413 error is from backend service. Looks like the 413 error happening in proxy.

Check the value of the flow variable fault.name in trace & add condition like below.

<Condition>(fault.name Matches "<value of fault.name>")</Condition>

it is happening in proxy, not even going to backend. I am not sure about the fault.name for this error, not able to find this in trace.

jaupadhyay
Participant IV

Hi @Soma Ghosh


In your proxy can you add javascript policy under FaultRules section. In that Javascript code can you add following lines.

print("Faultname " + context.getVariable('fault.name'));

print("Error Status Code " + context.getVariable('error.status.code'));

print("Error State " + context.getVariable('error.state'));

Start Tracing the proxy and make the call. If error is coming from proxy you will see values being populated for above flow variables.

Try and reproduce the error and see what's printed on you trace output from all transaction window.

Within FaultRules I am not able to print the 413 error status code.

I created a separate test proxy and added DefaultFaultRules with <AlwaysEnforce> as 'true'.

In DefaultFaultRules I am able to print fault.name='TooBigBody' and error.status.code=413.

Any idea why I am not able to capture the error in FaultRules?

jaupadhyay
Participant IV
@Soma Ghosh

According to the documentation: A DefaultFaultRule acts an exception handler for any error that is not explicitly handled by FaultRule.

https://docs.apigee.com/api-platform/fundamentals/fault-handling#creatingfaultrules-creatingadefault...

If you are interested in this topic

Couple of Apigee community page link explaining error handling in quite detail.

https://community.apigee.com/articles/23724/an-error-handling-pattern-for-apigee-proxies.html

https://community.apigee.com/articles/47312/an-improved-pattern-for-fault-handling.html

Also link to 4MV4D by @Anil Sagar @ Google explains basics of Default Fault rule.

https://www.youtube.com/watch?v=LGw4AP9cYHw

In Summary for your above example to capture 413 you need to use DefaultFaultRule as you tried out in test proxy.

pdouglas
Participant III

We are facing this 413 issue with a scenario involving sending a large file (> 10MB, very edge case). It blows up the message processor and no policies or fault rules in the proxy are hit or show up in the trace. In fact, even though the trace correctly shows the error:

{"fault":{"faultstring":"Body buffer overflow","detail":{"errorcode":"protocol.http.TooBigBody"}}}

The end result on the client is 502 Bad Gateway.

Even though the trace shows an event "Proxy Post Client Flow Started," again, no postflow faults or policies are traced. The workaround we are considering is turning streaming on and checking the file size, but it seems a pity for such an edge case. 99.9% of requests will not be anywhere close to that file size.

Testing with a POC proxy that raises a fault and does nothing else it works with an appropriate sized request; when I try a large request the proxy doesn't run the policy and returns a 502.

@Soma Ghosh, Jayesh Upadhyay did you find a solution for handling this?

This seems like a thing you should contact Apigee support about.

Sweet Dino, as ever we appreciate your insight!

Hi @Peter Douglas,

I added DefaultFaultRule in my proxy default.xml with the step to handle error in case of a large file size. Also added AlwaysEnforce= true. So my DefaultFaultRule looks like below.

As the documentation says, DefaultFaultRule acts as an exception handler of any error that is not explicitly handled by FaultRules conditions.

<DefaultFaultRule name="DefaultFaultRule">
<Step> <Condition>(fault.name = "TooBigBody")</Condition> <Name>AM-LargePayloadError</Name> </Step>
<AlwaysEnforce>true</AlwaysEnforce>
</DefaultFaultRule>

That worked, Soma! Thanks for everyone's attention and quick response! Verified solution.

Update: we have this working perfectly with requests that are marginally over the limit, eg., 10.1 MB. One test scenario includes uploading an extremely large file (20 MB) which persists in returning a 502 to the client in spite of the trace showing a 413. Streaming has been ruled out, we want to cap the file upload at 10 MB. We will release this as is - users are clearly instructed on max file size. We will still open a ticket with Apigee on the extremely large file returning 502 issue.

Our team seems to have found the solution. The error orignated in the proxyEndPoint and the default fault rule should be located here to catch the error. When inside the targetEndPoint flow the rule gets skipped. We caught it by putting a raisefault policy in the defaultfaultrule looking for error.code === 413.

Hope this solves your problem as well