From c6b02eac06fa279177b30839c38e8e99f5dc445f Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Mon, 11 Mar 2013 07:01:24 -0400 Subject: [PATCH] Work on error handling Fix throw of missing mandatory parameter and write suggestion Documentation Some renaming --- .../indexdata/pz2utils4jsf/config/Pz2Config.java | 2 +- .../pz2utils4jsf/errors/ApplicationError.java | 22 ------------ .../pz2utils4jsf/errors/ConfigurationError.java | 2 +- .../indexdata/pz2utils4jsf/errors/ErrorHelper.java | 13 ++++++-- .../pz2utils4jsf/errors/ErrorInterface.java | 22 ++++++++++++ .../indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java | 6 ++-- .../pz2utils4jsf/pazpar2/Pz2Interface.java | 6 ++-- .../indexdata/pz2utils4jsf/pazpar2/Pz2Session.java | 16 ++++----- .../pz2utils4jsf/pazpar2/data/CommandError.java | 35 ++++++++++++++++---- 9 files changed, 75 insertions(+), 49 deletions(-) delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorInterface.java diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java index b182777..c293649 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java @@ -82,7 +82,7 @@ public class Pz2Config implements ModuleConfigurationGetter, Serializable { if (properties.containsKey(key)) { return properties.get(key); } - throw new Error("Missing mandatory parameter: " + key); + throw new MissingMandatoryParameterException("Missing mandatory parameter: " + key); } @Override diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java deleted file mode 100644 index 7ececce..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.indexdata.pz2utils4jsf.errors; - -import java.io.Serializable; -import java.util.List; - -import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; -import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; - - -public interface ApplicationError extends Serializable { - - public String getLabel(); - public String getMessage(); - public String getException(); - public void setApplicationErrorCode(ErrorCode code); - public ErrorCode getApplicationErrorCode(); - public List getSuggestions(); - public void setErrorHelper(ErrorHelper helper); - public boolean hasPazpar2Error(); - public Pazpar2Error getPazpar2Error(); - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java index 2cafa40..7d3f7ba 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java @@ -6,7 +6,7 @@ import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; -public class ConfigurationError implements ApplicationError { +public class ConfigurationError implements ErrorInterface { private static final long serialVersionUID = -6599667223782130838L; private String label; diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java index 0abad05..4f05b77 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java @@ -10,7 +10,6 @@ import java.util.regex.Pattern; import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.Pz2Configurator; -import com.indexdata.pz2utils4jsf.config.Pz2ConfigureByMk2Config; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; import com.indexdata.pz2utils4jsf.utils.Utils; @@ -24,6 +23,7 @@ public class ErrorHelper implements Serializable { REMOTE_SERVICE_DEF_NOT_FOUND, LOCAL_SETTINGS_FILE_NOT_FOUND, MASTERKEY_CONFIG_FILE_NOT_FOUND, + MISSING_MANDATORY_PARAMETER, NOT_RESOLVED, SKIP_SUGGESTIONS}; @@ -38,7 +38,7 @@ public class ErrorHelper implements Serializable { this.configurator = configurator; } - public ErrorHelper.ErrorCode getErrorCode(ApplicationError appError) { + public ErrorHelper.ErrorCode getErrorCode(ErrorInterface appError) { if (appError.hasPazpar2Error()) { Pazpar2Error pz2err = appError.getPazpar2Error(); String pz2errcode = pz2err.getCode(); @@ -70,11 +70,13 @@ public class ErrorHelper implements Serializable { return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND; } else if (appError.getMessage().contains("Cannot query Pazpar2 while there are configuration errors")) { return ErrorCode.SKIP_SUGGESTIONS; + } else if (appError.getMessage().contains("Missing mandatory parameter")) { + return ErrorCode.MISSING_MANDATORY_PARAMETER; } return ErrorCode.NOT_RESOLVED; } - public ArrayList getSuggestions(ApplicationError error) { + public ArrayList getSuggestions(ErrorInterface error) { ArrayList suggestions = new ArrayList(); ErrorCode code = getErrorCode(error); switch (code) { @@ -106,6 +108,11 @@ public class ErrorHelper implements Serializable { " the file itself could not be found. Please check the configuration."); addConfigurationDocumentation(suggestions); break; + case MISSING_MANDATORY_PARAMETER: + suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" + + " file used. Please check the property file for the parameter given in the error message "); + addConfigurationDocumentation(suggestions); + break; case NOT_RESOLVED: suggestions.add("Unforeseen error situation. No suggestions prepared."); break; diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorInterface.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorInterface.java new file mode 100644 index 0000000..c529651 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorInterface.java @@ -0,0 +1,22 @@ +package com.indexdata.pz2utils4jsf.errors; + +import java.io.Serializable; +import java.util.List; + +import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; + + +public interface ErrorInterface extends Serializable { + + public String getLabel(); + public String getMessage(); + public String getException(); + public void setApplicationErrorCode(ErrorCode code); + public ErrorCode getApplicationErrorCode(); + public List getSuggestions(); + public void setErrorHelper(ErrorHelper helper); + public boolean hasPazpar2Error(); + public Pazpar2Error getPazpar2Error(); + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index dcf75a7..fd3e478 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -12,7 +12,7 @@ import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.Pz2Configurator; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorInterface; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; @@ -278,11 +278,11 @@ public class Pz2Bean implements Pz2Interface, Serializable { return pz2.hasErrors(); } - public ApplicationError getCommandError() { + public ErrorInterface getCommandError() { return pz2.getCommandError(); } - public List getConfigurationErrors () { + public List getConfigurationErrors () { return pz2.getConfigurationErrors(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java index 3de5b68..dbe5f44 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java @@ -4,7 +4,7 @@ import java.io.Serializable; import java.util.List; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorInterface; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; @@ -323,7 +323,7 @@ public interface Pz2Interface extends Serializable { * * @return */ - public ApplicationError getCommandError(); + public ErrorInterface getCommandError(); /** * Returns all errors encountered during configuration of the application, in particular @@ -331,6 +331,6 @@ public interface Pz2Interface extends Serializable { * * @return */ - public List getConfigurationErrors(); + public List getConfigurationErrors(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 886a0b8..3034a74 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -18,7 +18,7 @@ import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric; import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException; import com.indexdata.pz2utils4jsf.config.Pz2Configurator; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorInterface; import com.indexdata.pz2utils4jsf.errors.ErrorHelper; import com.indexdata.pz2utils4jsf.errors.ConfigurationError; import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; @@ -48,7 +48,7 @@ public class Pz2Session implements Pz2Interface { private TargetFilter targetFilter = null; private ResultsPager pager = null; private ErrorHelper errorHelper = null; - private List configurationErrors = null; + private List configurationErrors = null; public Pz2Session () { logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); @@ -56,7 +56,7 @@ public class Pz2Session implements Pz2Interface { public void init(Pz2Configurator pz2conf) { if (client==null) { - configurationErrors = new ArrayList(); + configurationErrors = new ArrayList(); errorHelper = new ErrorHelper(pz2conf); logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf)); try { @@ -144,11 +144,7 @@ public class Pz2Session implements Pz2Interface { return "0"; } } else { - configurationErrors.add( - new ConfigurationError("Querying while errors", - "App halted", - "Cannot query Pazpar2 while there are configuration errors.", - errorHelper)); + logger.error("Did not attempt to execute query since there are configuration errors."); return "0"; } @@ -324,7 +320,7 @@ public class Pz2Session implements Pz2Interface { return hasConfigurationErrors() || hasCommandErrors(); } - public List getConfigurationErrors() { + public List getConfigurationErrors() { return configurationErrors; } @@ -333,7 +329,7 @@ public class Pz2Session implements Pz2Interface { * error found for an arbitrary command, if any, otherwise * an empty dummy error. */ - public ApplicationError getCommandError() { + public ErrorInterface getCommandError() { CommandError error = new CommandError(); if (dataObjects.get("search").hasApplicationError()) { error = dataObjects.get("search").getApplicationError(); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java index 016f68e..2b5fae2 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java @@ -5,20 +5,26 @@ import static com.indexdata.pz2utils4jsf.utils.Utils.nl; import java.util.ArrayList; import java.util.List; -import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorInterface; import com.indexdata.pz2utils4jsf.errors.ErrorHelper; import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; import com.indexdata.utils.XmlUtils; /** - * Captures errors encountered during the execution of a command. - * Is parsed by Pazpar2ResponseParser, piggybacked in a (seemingly) - * regular command respond. + * Holds an error encountered during the execution of a command. + * + * An error can be received by a command thread as an exception message + * or as an error XML. In both cases the error (string or xml) will be embedded + * in a new 'applicationerror' element which in turn will be embedded in a + * command XML (i.e. a 'search' or a 'show' response XML) + * + * The command response XML is subsequently parsed by Pazpar2ResponseParser, + * which will then create the CommandError object. * * @author Niels Erik * */ -public class CommandError extends Pazpar2ResponseData implements ApplicationError { +public class CommandError extends Pazpar2ResponseData implements ErrorInterface { private static final long serialVersionUID = 8878776025779714122L; private ErrorCode applicationErrorCode; @@ -56,6 +62,7 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro /** * Creates an XML string error message, embedded in an XML string document named by the command + * This is the XML that Pazpar2ResponseParser will turn into a CommandError object. * @param commandName * @param exceptionName * @param errorMessage @@ -73,6 +80,18 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro return errorXml.toString(); } + /** + * Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of + * a command response document (like 'search' or 'show'). + * This is the XML that Pazpar2ResponseParser will turn into a CommandError object. + * + * + * @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 pazpar2ErrorXml The error document as created by Pazpar2 -- or, for some errors, by the + * Pazpar2 client. + * @return + */ public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) { StringBuilder errorXml = new StringBuilder(""); errorXml.append("<" + commandName + ">"+nl); @@ -85,7 +104,11 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro return errorXml.toString(); } - + + /** + * Sets the object that should be used to analyze the error + * + */ public void setErrorHelper (ErrorHelper errorHelper) { this.errorHelper = errorHelper; } -- 1.7.10.4