@Override public void run() { try { while (true) { RequestAndCallback requestAndCallback = MockRequester.this.requestAndCallbackQueue.take(); long nanoTime = System.nanoTime(); long delayNanos = requestAndCallback.getProcessAfterNanos() - nanoTime; if (delayNanos > 0) { Thread.sleep(TimeUnit.NANOSECONDS.toMillis(delayNanos)); } try { PermitAllocation allocation = MockRequester.this.limiterServer.getSync(new ComplexResourceKey<>(requestAndCallback.getRequest(), new EmptyRecord())); Response<PermitAllocation> response = Mockito.mock(Response.class); Mockito.when(response.getEntity()).thenReturn(allocation); requestAndCallback.getCallback().onSuccess(response); } catch (RestLiServiceException rexc) { RestLiResponseException returnException = Mockito.mock(RestLiResponseException.class); Mockito.when(returnException.getStatus()).thenReturn(rexc.getStatus().getCode()); requestAndCallback.getCallback().onError(returnException); } } } catch (Throwable t) { log.error("Error", t); throw new RuntimeException(t); } } }
@Test public void testErrorDeploy() { final ProjectManagerResource resource = new ProjectManagerResource(); final Map<String, ValidationReport> reports = new LinkedHashMap<>(); for (int i = 0; i < 3; i++) { addMockError(reports, "Test error level info message."); } // We expect that a RestLiServiceException is thrown given a // report with errors. Uncaught exceptions will result in failure try { resource.checkReports(reports); Assert.fail(); } catch (final RestLiServiceException e) { //Ensure we have the right status code and exit assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST); } }
/** * Test that an error message is attached to the exception on an error */ @Test public void testErrorMessageDeploy() { final ProjectManagerResource resource = new ProjectManagerResource(); final Map<String, ValidationReport> reports = new LinkedHashMap<>(); addMockError(reports, "This should show up."); // We expect that a RestLiServiceException is thrown given a // report with errors. Uncaught exceptions will result in failure try { resource.checkReports(reports); Assert.fail(); } catch (final RestLiServiceException e) { //Ensure we have the right status code and exit assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST); assertEquals(e.getMessage(), "Validator Error reports errors: This should show up." + System.getProperty("line.separator")); } }
@Test public void testWarnErrorDeploy() { final ProjectManagerResource resource = new ProjectManagerResource(); final Map<String, ValidationReport> reports = new LinkedHashMap<>(); for (int i = 0; i < 7; i++) { // If i is even, make an error report, otherwise make a warning report if (i % 2 == 0) { addMockError(reports, "Test error level info message."); } else { addMockWarning(reports, "Test warn level info message."); } } // We expect that a RestLiServiceException is thrown given a // report with errors. Uncaught exceptions will result in failure try { resource.checkReports(reports); Assert.fail(); } catch (final RestLiServiceException e) { //Ensure we have the right status code and exit assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST); } }
/** * Constructs a CreateResponse containing error details. * * @param error A rest.li exception containing the appropriate HTTP response status and error details. */ public CreateResponse(RestLiServiceException error) { _id = null; _status = error.getStatus(); _error = error; }
/** * Gets the status of the response. * * @return the http status. */ public HttpStatus getStatus() { return _exception != null ? _exception.getStatus() : _status; }
/** * Instantiates a failed entry within a collection create response. * * @param exception the exception that triggered the failure. */ public CollectionCreateResponseItem(RestLiServiceException exception) { _exception = exception; _recordResponse = null; _httpStatus = exception.getStatus(); }
public IndividualResponseException(RestLiServiceException e, ErrorResponseBuilder errorResponseBuilder) { super(e); _response = createErrorIndividualResponse(e.getStatus(), errorResponseBuilder.buildErrorResponse(e)); }
public static IndividualResponse createErrorIndividualResponse(RestLiServiceException e, ErrorResponseBuilder errorResponseBuilder) { return createErrorIndividualResponse(e.getStatus(), errorResponseBuilder.buildErrorResponse(e)); }
protected RestLiResponseException buildPreRoutingError(Throwable throwable, Request request) { Map<String, String> requestHeaders = request.getHeaders(); Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString()); headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR); RestLiServiceException restLiServiceException = RestLiServiceException.fromThrowable(throwable); ErrorResponse errorResponse = _errorResponseBuilder.buildErrorResponse(restLiServiceException); RestLiResponse restLiResponse = new RestLiResponse.Builder() .status(restLiServiceException.getStatus()) .entity(errorResponse) .headers(headers) .cookies(Collections.emptyList()) .build(); return new RestLiResponseException(throwable, restLiResponse); }
private RestLiResponse buildErrorResponse(Throwable th, RestLiResponseData<?> responseData) { Map<String, String> responseHeaders = responseData.getHeaders(); responseHeaders.put(HeaderUtil.getErrorResponseHeaderName(responseHeaders), RestConstants.HEADER_VALUE_ERROR); RestLiServiceException ex; if (th instanceof RestLiServiceException) { ex = (RestLiServiceException) th; } else { ex = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, th.getMessage(), th); } return new RestLiResponse.Builder().headers(responseHeaders).cookies(responseData.getCookies()) .status(ex.getStatus()) .entity(_errorResponseBuilder.buildErrorResponse(ex)) .build(); }
if (errorResponseFormat.showStatusCodeInBody()) er.setStatus(result.getStatus().getCode());
@Override @SuppressWarnings("unchecked") public RestLiResponse buildResponse(RoutingResult routingResult, RestLiResponseData<BatchCreateResponseEnvelope> responseData) { List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateResponses = responseData.getResponseEnvelope().getCreateResponses(); List<CreateIdStatus<Object>> formattedResponses = new ArrayList<>(collectionCreateResponses.size()); // Iterate through the responses and generate the ErrorResponse with the appropriate override for exceptions. // Otherwise, add the result as is. for (BatchCreateResponseEnvelope.CollectionCreateResponseItem response : collectionCreateResponses) { if (response.isErrorResponse()) { RestLiServiceException exception = response.getException(); formattedResponses.add(new CreateIdStatus<>(exception.getStatus().getCode(), response.getId(), _errorResponseBuilder.buildErrorResponse(exception), ProtocolVersionUtil.extractProtocolVersion(responseData.getHeaders()))); } else { formattedResponses.add((CreateIdStatus<Object>) response.getRecord()); } } RestLiResponse.Builder builder = new RestLiResponse.Builder(); BatchCreateIdResponse<Object> batchCreateIdResponse = new BatchCreateIdResponse<>(formattedResponses); return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).entity(batchCreateIdResponse).build(); }