@@ -92,6 +92,12 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
9292 client .consumerRegistry .addOrUpdateConsumer (consumer );
9393 final String consumerIdentifier = consumer != null ? consumer .getIdentifier () : "" ;
9494
95+ // Get captured exception
96+ Object capturedException = request .getAttribute ("apitallyCapturedException" );
97+ if (exception == null && capturedException != null && capturedException instanceof Exception ) {
98+ exception = (Exception ) capturedException ;
99+ }
100+
95101 // Add request to counter
96102 final long requestContentLength = request .getContentLengthLong ();
97103 final long requestSize = requestContentLength >= 0 ? requestContentLength
@@ -119,12 +125,12 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
119125 new Request (startTime / 1000.0 , consumerIdentifier , request .getMethod (), path ,
120126 request .getRequestURL ().toString (), requestHeaders , requestSize , requestBody ),
121127 new Response (response .getStatus (), responseTimeInMillis / 1000.0 , responseHeaders ,
122- responseSize , responseBody ));
128+ responseSize , responseBody ),
129+ exception );
123130 }
124131
125132 // Add validation error to counter
126133 if (response .getStatus () >= 400 && response .getStatus () < 500 ) {
127- Object capturedException = request .getAttribute ("apitallyCapturedException" );
128134 if (capturedException instanceof ConstraintViolationException e ) {
129135 for (ConstraintViolation <?> violation : e .getConstraintViolations ()) {
130136 client .validationErrorCounter .addValidationError (
@@ -144,15 +150,8 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
144150 }
145151
146152 // Add server error to counter
147- if (response .getStatus () == 500 ) {
148- Object capturedException = request .getAttribute ("apitallyCapturedException" );
149- if (exception == null && capturedException != null && capturedException instanceof Exception ) {
150- exception = (Exception ) capturedException ;
151- }
152- if (exception != null ) {
153- client .serverErrorCounter .addServerError (
154- consumerIdentifier , request .getMethod (), path , exception );
155- }
153+ if (response .getStatus () == 500 && exception != null ) {
154+ client .serverErrorCounter .addServerError (consumerIdentifier , request .getMethod (), path , exception );
156155 }
157156 } catch (Exception e ) {
158157 logger .error ("Error in Apitally filter" , e );
0 commit comments