Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In the controller, something bad happens -
- So we call the utils class !
- if (StringUtils.isBlank(patientId) && StringUtils.isBlank(encounterUuid))
- return ExceptionUtil.generateMessage(request, response, ErrorDetailsEnum.ERROR_1);
- =================================================
- This is what the exception utils class looks like-
- Note that this is where we assign the content types to the responses. (im putting it here to prevent myself repeating in on the *controller class)
- public class ExceptionUtil {
- public static Object generateMessage(HttpServletRequest request, HttpServletResponse response, ErrorDetailsEnum error){
- boolean isPipeDelimited = false;
- String acceptHeader = request.getHeader("Accept");
- if (acceptHeader == null || !acceptHeader.contains("text/xml"))
- isPipeDelimited = true;
- if(isPipeDelimited){
- response.setContentType("application/json");
- return WriteJsonMessage(error);
- }else{
- response.setContentType("text/xml");
- return WriteXmlMessage(error);
- }
- }
- private static Object WriteJsonMessage(ErrorDetailsEnum error) {
- ObjectMapper mapper = new ObjectMapper();
- ErrorMessageTemplate errorMessageTemplate = new ErrorMessageTemplate();
- errorMessageTemplate.setErrorMessage(error);
- errorMessageTemplate.setErrorCode(Integer.toString(error.getCode()));
- //errorMessageTemplate.setErrorDetails(errorDetails);
- Writer strWriter = new StringWriter();
- try {
- mapper.writeValue(strWriter, errorMessageTemplate);
- } catch (JsonGenerationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JsonMappingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String userDataJSON = strWriter.toString();
- System.out.println(userDataJSON);
- return userDataJSON;
- }
- private static Object WriteXmlMessage(ErrorDetailsEnum error) {
- String xmlString = null;
- try {
- DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
- Document doc = docBuilder.newDocument();
- //create the root element and add it to the document
- Element root = doc.createElement("error");
- doc.appendChild(root);
- //create child element, add an attribute, and add to root
- Element errorMsg = doc.createElement("errorMessage");
- root.appendChild(errorMsg);
- //add a text element to the child
- Text errorText = doc.createTextNode(error.toString());
- errorMsg.appendChild(errorText);
- //create child element, add an attribute, and add to root
- Element errorCode = doc.createElement("errorCode");
- root.appendChild(errorCode);
- //add a text element to the child
- Text errorCodeText = doc.createTextNode(Integer.toString(error.getCode()));
- errorCode.appendChild(errorCodeText);
- //set up a transformer
- TransformerFactory transfac = TransformerFactory.newInstance();
- Transformer trans = transfac.newTransformer();
- trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- trans.setOutputProperty(OutputKeys.INDENT, "yes");
- //create string from xml tree
- StringWriter sw = new StringWriter();
- StreamResult result = new StreamResult(sw);
- DOMSource source = new DOMSource(doc);
- trans.transform(source, result);
- xmlString = sw.toString();
- System.out.println("Here's the xml:\n\n" + xmlString);
- } catch (Exception e) {
- System.out.println(e);
- }
- return xmlString;
- }
- }
- ======================================
- And now, the ErrorDetailsEnum, which contains the error message and the code for every potential issue-
- package org.openmrs.module.hl7query.util;
- public enum ErrorDetailsEnum {
- ERROR_1(400), ERROR_2(401), ERROR_3(402);
- private int code;
- private ErrorDetailsEnum(int c) {
- code = c;
- }
- public int getCode() {
- return code;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment