From: Niels Erik G. Nielsen Date: Mon, 13 May 2013 15:41:51 +0000 (-0400) Subject: Tweaks error catching and messages. X-Git-Tag: v0.0.7~111 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=6da388f43fedda3e61e630f826244608d2da6301;p=mkjsf-moved-to-github.git Tweaks error catching and messages. --- diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java index 3656828..7ab5ec5 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java @@ -108,11 +108,11 @@ public class Pz2Client implements SearchClient { String htmlStrippedOfTags = resp.replaceAll("\\<[^>]*>",""); String errorXml = ""; if (htmlStrippedOfTags.toLowerCase().contains("domain")) { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Error: Expected XML response from Pazpar2, got HTML with the word domain. Could be unrecognized address for Pazpar2.", htmlStrippedOfTags); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2", "Error: Expected XML response from Pazpar2 but got HTML. The HTML contains the word domain suggesting that the Pazpar2 address was not found.", htmlStrippedOfTags); } else { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Error: Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2: " + pz2HttpResponse.getContentType(),"Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags); } - commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,"text/xml"); + commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,pz2HttpResponse.getContentType()); } else { String resp = baos.toString("UTF-8"); logger.error("Pazpar2 status code was " + pz2HttpResponse.getStatusCode() + ": " + resp); @@ -121,12 +121,12 @@ public class Pz2Client implements SearchClient { } catch (IOException e) { logger.error(e.getMessage()); e.printStackTrace(); - commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "io", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "IO exception", e.getMessage(), ""),"text/xml"); } catch (Pazpar2ErrorException e) { logger.error(e.getMessage()); e.printStackTrace(); logger.error("Creating error XML"); - commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "-1", "Pazpar2Error", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(500,CommandError.createErrorXml(command.getCommandName(), "", "Pazpar2Error", e.getMessage(),""),"text/xml"); } long end = System.currentTimeMillis(); logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java index 51afab2..152ee33 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java @@ -111,19 +111,19 @@ public class ServiceProxyClient implements SearchClient { if (handler.getContentType().contains("html")) { String htmlStrippedOfTags = (new String(response,"UTF-8")).replaceAll("\\<[^>]*>",""); if (htmlStrippedOfTags.toLowerCase().contains("domain")) { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Error: Expected XML response from Service Proxy, got HTML with word 'domain' in, probably domain not found.", htmlStrippedOfTags); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML. It contains the word domain suggesting that the service address was not found.", htmlStrippedOfTags); } else { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Error: Expected XML response from Service Proxy, got HTML", htmlStrippedOfTags); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML", htmlStrippedOfTags); } } else { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Error: Expected XML response from Service Proxy, got: "+handler.getContentType(), new String(response,"UTF-8")); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy: "+handler.getContentType(), "Could not process non-XML response from Service Proxy", new String(response,"UTF-8")); } commandResponse = new ClientCommandResponse(handler.getStatusCode(),errorXml,handler.getContentType()); } } } catch (Exception e) { e.printStackTrace(); - commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : "")),handler.getContentType()); + commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : ""), e.getStackTrace().toString()),handler.getContentType()); } return commandResponse; } @@ -238,11 +238,11 @@ public class ServiceProxyClient implements SearchClient { } catch (ClientProtocolException e) { logger.error(e.getMessage()); e.printStackTrace(); - commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "client protocol exception", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "Client protocol exception", e.getMessage(), e.getStackTrace().toString()),"text/xml"); } catch (IOException e) { logger.error(e.getMessage()); e.printStackTrace(); - commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO exception", e.getMessage(),e.getStackTrace().toString()),"text/xml"); } return commandResponse; } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java index 9fbe0d0..7680ae5 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java @@ -66,18 +66,19 @@ public class CommandError extends ResponseDataObject implements ErrorInterface { * Creates an XML string error message, embedded in an XML string document named by the command * This is the XML that ResponseParser will turn into a CommandError object. * @param commandName - * @param exceptionName + * @param exception * @param errorMessage * @return */ - public static String createErrorXml (String commandName, String statusCode, String exceptionName, String errorMessage) { + public static String createErrorXml (String commandName, String statusCode, String exception, String errorMessage, String response) { StringBuilder errorXml = new StringBuilder(""); errorXml.append("<" + commandName + ">"+nl); errorXml.append(" "+nl); errorXml.append(" " + commandName + ""+nl); errorXml.append(" " + statusCode + ""+nl); - errorXml.append(" " + (exceptionName != null ? XmlUtils.escape(exceptionName) : "") + ""+nl); - errorXml.append(" " + (errorMessage != null ? XmlUtils.escape(errorMessage) : "") + ""+nl); + errorXml.append(" " + (exception != null ? XmlUtils.escape(exception) : "") + ""+nl); + errorXml.append(" " + (errorMessage != null ? XmlUtils.escape(errorMessage) : "") + ""+nl); + errorXml.append(" " + response + "" + nl); errorXml.append(" "+nl); errorXml.append(""+nl); return errorXml.toString(); @@ -90,18 +91,18 @@ public class CommandError extends ResponseDataObject implements ErrorInterface { * * * @param commandName The name of the command during which's execution the error was encountered - * @param exceptionName The (possibly loosely defined) name of the exception that was thrown + * @param exception The (possibly loosely defined) name of the exception that was thrown * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or * by the Pazpar2 client itself. * @return */ - public static String insertErrorXml (String commandName, String statusCode, String exceptionName, String pazpar2ErrorXml) { + public static String insertErrorXml (String commandName, String statusCode, String exception, String pazpar2ErrorXml) { StringBuilder errorXml = new StringBuilder(""); errorXml.append("<" + commandName + ">"+nl); errorXml.append(" "+nl); errorXml.append(" " + commandName + ""+nl); errorXml.append(" " + statusCode + ""+nl); - errorXml.append(" " + XmlUtils.escape(exceptionName) + ""+nl); + errorXml.append(" " + XmlUtils.escape(exception) + ""+nl); errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl); errorXml.append(" "+nl); errorXml.append(""+nl);