public void validateResponseErrorXml(String xml) { ErrorResponse errorResponse = (ErrorResponse) xstream.fromXML(xml, new ErrorResponse()); Assert.assertTrue("Error response is empty.", errorResponse.getErrors().size() > 0); for (Iterator<ErrorMessage> iter = errorResponse.getErrors().iterator(); iter.hasNext(); ) { ErrorMessage error = iter.next(); Assert.assertFalse("Response Error message is empty.", StringUtils.isEmpty(error.getMsg())); } }
private static <E> PlexusResourceException validationError(String name, Class<E> enumClass) { ErrorMessage err = new ErrorMessage(); err.setId("*"); err.setMsg("No enum const " + enumClass + "." + name); ErrorResponse ner = new ErrorResponse(); ner.addError(err); return new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Configuration error.", ner); }
@SuppressWarnings("unchecked") public Object sendMessage(Method method, String url, Representation representation) throws NexusConnectionException { // get the response Response response = this.sendRequest(method, url, representation); // always expect a success if (!response.getStatus().isSuccess()) { String errorMessage = "Error in response from server: " + response.getStatus() + "."; List<ErrorMessage> errors = new ArrayList<ErrorMessage>(); try { if (response.getEntity() != null) { String responseText = response.getEntity().getText(); // this is kinda hackish, but this class is already tied to xstream if (responseText.contains("<error")) // quick check before we parse the string { // try to parse the response ErrorResponse errorResponse = (ErrorResponse) this.xstream.fromXML(responseText, new ErrorResponse()); // if we made it this far we can stick the ErrorMessages in the Exception errors = errorResponse.getErrors(); } else { // the response text might be helpful in debugging, so we will add it errorMessage += "\nResponse: " + (!StringUtils.isEmpty(responseText) ? "\n" + responseText : "<empty>"); } } else { errorMessage = response.getStatus().getName(); } } catch (Exception e) // we really don't want our fancy exception to cause another problem. { logger.warn("Error getting the response text: " + e.getMessage(), e); } // now finally throw it... throw new NexusConnectionException(errorMessage, errors); } Object result = null; try { String responseText = response.getEntity().getText(); if (StringUtils.isNotEmpty(responseText)) { result = this.xstream.fromXML(responseText); } } catch (IOException e) { throw new NexusConnectionException("Error getting response text: " + e.getMessage(), e); } return result; }
private void test(String query, String expected) throws IOException { String serviceURIpart = "service/local/lucene/search?q=" + URLEncoder.encode(query, "UTF-8"); log.info("Testing query {}: {}", query, serviceURIpart); String errorPayload = RequestFacade.doGetForText(serviceURIpart, respondsWithStatusCode(400)); log.info("Received 'Bad Request' error: " + errorPayload); MediaType type = MediaType.APPLICATION_XML; XStreamRepresentation representation = new XStreamRepresentation(getXMLXStream(), errorPayload, type); ErrorResponse payload = (ErrorResponse) representation.getPayload(new ErrorResponse()); List errors = payload.getErrors(); assertThat((Collection<?>) errors, hasSize(1)); ErrorMessage error = (ErrorMessage) errors.get(0); String msg = error.getMsg(); msg = msg.replaceAll("Cannot parse '([^']*)':.*", "$1"); log.info("msg: " + msg); assertThat(msg, equalTo(expected)); }