/** * @param errors * The {@link ApiError}s that should represent the exception. This should not be null or empty. * * @return An instance with {@link #shouldHandleResponse} set to true and {@link #errors} set to a copy of the given * set of errors. */ public static ApiExceptionHandlerListenerResult handleResponse(SortedApiErrorSet errors) { return new ApiExceptionHandlerListenerResult(true, errors, null, null); }
@Override public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) { // We only care about ClientDataValidationErrors. if (ex instanceof ClientDataValidationError) { List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>(); SortedApiErrorSet apiErrors = processClientDataValidationError( (ClientDataValidationError)ex, extraDetailsForLogging ); return ApiExceptionHandlerListenerResult.handleResponse(apiErrors, extraDetailsForLogging); } // Not a ClientDataValidationError. Ignore. return ApiExceptionHandlerListenerResult.ignoreResponse(); }
/** * @return An {@link ApiExceptionHandlerListenerResult} indicating whether we should handle the given exception. * If {@link ApiExceptionHandlerListenerResult#shouldHandleResponse} is true then * {@link ApiExceptionHandlerListenerResult#errors} and * {@link ApiExceptionHandlerListenerResult#extraDetailsForLogging} must be filled in appropriately and * ready for passing in to * {@link #doHandleApiException(SortedApiErrorSet, List, List, Throwable, RequestInfoForLogging)}. If it is * false then the given exception will be ignored by this class (and should therefore ultimately be handled * by this project's implementation of {@link UnhandledExceptionHandlerBase}). */ protected ApiExceptionHandlerListenerResult shouldHandleApiException(Throwable ex) { // The original exception might be a "wrapper" exception. If so, unwrap it so we can send the core exception // through our list of listeners. Throwable coreEx = unwrapAndFindCoreException(ex); // Run through each listener looking for one that wants to handle the core exception. for (ApiExceptionHandlerListener listener : apiExceptionHandlerListenerList) { ApiExceptionHandlerListenerResult result = listener.shouldHandleException(coreEx); if (result.shouldHandleResponse) return result; } // We didn't have any handler that wanted to deal with this exception, so return an "ignore it" response. return ApiExceptionHandlerListenerResult.ignoreResponse(); }
@Override public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) { SortedApiErrorSet handledErrors = null; List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>(); // Look for exceptions we're supposed to handle. if (isTemporaryProblem(ex, extraDetailsForLogging)) { handledErrors = singletonSortedSetOf(projectApiErrors.getTemporaryServiceProblemApiError()); } else if (ex instanceof ServerHttpStatusCodeException) { handledErrors = processServerHttpStatusCodeException( (ServerHttpStatusCodeException) ex, extraDetailsForLogging ); } else if (ex instanceof ServerUnknownHttpStatusCodeException) { handledErrors = processServerUnknownHttpStatusCodeException( (ServerUnknownHttpStatusCodeException) ex, extraDetailsForLogging ); } if (handledErrors != null) return ApiExceptionHandlerListenerResult.handleResponse(handledErrors, extraDetailsForLogging); // Not an exception we care about - ignore it. return ApiExceptionHandlerListenerResult.ignoreResponse(); }
@Override public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) { // We only care about ApiExceptions. if (!(ex instanceof ApiException)) return ApiExceptionHandlerListenerResult.ignoreResponse(); ApiException apiException = ((ApiException)ex); // Add all the ApiErrors from the exception. SortedApiErrorSet errors = new SortedApiErrorSet(); errors.addAll(apiException.getApiErrors()); // Add all the extra details for logging from the exception. List<Pair<String, String>> messages = new ArrayList<>(); messages.addAll(apiException.getExtraDetailsForLogging()); // Add all the extra response headers from the exception. List<Pair<String, List<String>>> headers = new ArrayList<>(); headers.addAll(apiException.getExtraResponseHeaders()); // Include the ApiException's message as a logged key/value pair. if (StringUtils.isNotBlank(ex.getMessage())) messages.add(Pair.of("api_exception_message", ex.getMessage())); return ApiExceptionHandlerListenerResult.handleResponse(errors, messages, headers); } }
@Override public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) { ServersideValidationError sve = null; // Try to get the ServersideValidationError if (ex instanceof ServersideValidationError) sve = (ServersideValidationError)ex; else if ((ex instanceof DownstreamRequestOrResponseBodyFailedValidationException) && (ex.getCause() instanceof ServersideValidationError)) { sve = (ServersideValidationError) ex.getCause(); } if (sve != null) { // Process the ServersideValidationError to add logging details and get the appropriate SortedApiErrorSet. List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>(); SortedApiErrorSet apiErrors = processServersideValidationError(sve, extraDetailsForLogging); return ApiExceptionHandlerListenerResult.handleResponse(apiErrors, extraDetailsForLogging); } // Not an exception we know how to handle - ignore it. return ApiExceptionHandlerListenerResult.ignoreResponse(); }
result = ApiExceptionHandlerListenerResult.handleResponse(handledErrors, extraDetailsForLogging); result = ApiExceptionHandlerListenerResult.ignoreResponse();
/** * @return An instance with {@link #shouldHandleResponse} set to false. */ public static ApiExceptionHandlerListenerResult ignoreResponse() { return new ApiExceptionHandlerListenerResult(false, null, null, null); } }
return ApiExceptionHandlerListenerResult.handleResponse( singletonError(getApiErrorForCircuitBreakerException(cbe)), singletonList(Pair.of("circuit_breaker_id", String.valueOf(cbe.circuitBreakerId))) return ApiExceptionHandlerListenerResult.handleResponse( singletonError(projectApiErrors.getTemporaryServiceProblemApiError()), singletonList(Pair.of( return ApiExceptionHandlerListenerResult.handleResponse( singletonError(projectApiErrors.getTemporaryServiceProblemApiError()), Arrays.asList( return ApiExceptionHandlerListenerResult.handleResponse( singletonError(projectApiErrors.getTemporaryServiceProblemApiError()), singletonList(Pair.of("closed_channel_id", String.valueOf(dsClosedEx.channelId))) ? generateTooLongFrameApiError((TooLongFrameException)ex) : projectApiErrors.getMalformedRequestApiError(); return ApiExceptionHandlerListenerResult.handleResponse( singletonError(errorToUse), withBaseExceptionMessage( Pair.of("cause", "The request exceeded the maximum payload size allowed") ); return ApiExceptionHandlerListenerResult.handleResponse( singletonError(errorToUse), withBaseExceptionMessage( return ApiExceptionHandlerListenerResult.handleResponse(
/** * @param errors * The {@link ApiError}s that should represent the exception. This should not be null or empty. * @param extraDetailsForLogging * Any extra key/value pairs that should be logged when Backstopper logs the exception/error. * * @return An instance with {@link #shouldHandleResponse} set to true, {@link #errors} set to a copy of the given * set of errors, and {@link #extraDetailsForLogging} set to a copy of the given list of logging data. */ public static ApiExceptionHandlerListenerResult handleResponse(SortedApiErrorSet errors, List<Pair<String, String>> extraDetailsForLogging) { return new ApiExceptionHandlerListenerResult(true, errors, extraDetailsForLogging, null); }
/** * @param errors * The {@link ApiError}s that should represent the exception. This should not be null or empty. * @param extraDetailsForLogging * Any extra key/value pairs that should be logged when Backstopper logs the exception/error. * @param extraResponseHeaders * Any extra response headers that the framework should include in the response to the caller. * * @return An instance with {@link #shouldHandleResponse} set to true, {@link #errors} set to a copy of the given * set of errors, and {@link #extraDetailsForLogging} set to a copy of the given list of logging data. */ public static ApiExceptionHandlerListenerResult handleResponse( SortedApiErrorSet errors, List<Pair<String, String>> extraDetailsForLogging, List<Pair<String, List<String>>> extraResponseHeaders ) { return new ApiExceptionHandlerListenerResult(true, errors, extraDetailsForLogging, extraResponseHeaders); }