Embeds pz2 error XML, if any, in the app error XML
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / errors / ErrorHelper.java
index 9aa8fec..f74407d 100644 (file)
@@ -1,5 +1,7 @@
 package com.indexdata.pz2utils4jsf.errors;\r
 \r
+import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
+\r
 import java.io.Serializable;\r
 import java.util.ArrayList;\r
 import java.util.regex.Matcher;\r
@@ -8,21 +10,24 @@ import java.util.regex.Pattern;
 import org.apache.log4j.Logger;\r
 \r
 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error;\r
 import com.indexdata.pz2utils4jsf.utils.Utils;\r
-import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
 \r
 public class ErrorHelper implements Serializable {\r
 \r
   public enum ErrorCode {PAZPAR2_404, \r
                          PAZPAR2_UNEXPECTED_RESPONSE,\r
+                         PAZPAR2_12,\r
+                         PAZPAR2_ERRORS,\r
                          LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
                          REMOTE_SERVICE_DEF_NOT_FOUND,\r
                          LOCAL_SETTINGS_FILE_NOT_FOUND,\r
-                         NOT_RESOLVED};\r
+                         NOT_RESOLVED,\r
+                         SKIP_SUGGESTIONS};\r
 \r
   private static final long serialVersionUID = 2860804561068279131L;\r
   private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
-  private static Pattern missingLocalServiceDefFile = Pattern.compile(".*Error reading service definition XML.*");\r
+  \r
   private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
   \r
   private Pz2Configurator configurator = null;\r
@@ -31,9 +36,24 @@ public class ErrorHelper implements Serializable {
     this.configurator = configurator;\r
   }\r
   \r
-  public ErrorHelper.ErrorCode getErrorCode(ApplicationError error) {    \r
-    if (error.getMessage().startsWith("Unexpected HTTP response")) {\r
-      Matcher m = httpResponsePattern.matcher(error.getMessage());\r
+  public ErrorHelper.ErrorCode getErrorCode(ApplicationError appError) {\r
+    if (appError.hasPazpar2Error()) {\r
+      Pazpar2Error pz2err = appError.getPazpar2Error();\r
+      String pz2errcode = pz2err.getCode();\r
+      switch (pz2errcode) {\r
+      case "12": \r
+        return ErrorCode.PAZPAR2_12;\r
+      case "0":    \r
+        if (pz2err.getMsg().contains("target settings from file")) {\r
+          return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;\r
+        } else {\r
+          return ErrorCode.PAZPAR2_ERRORS;\r
+        }\r
+      default: \r
+        return ErrorCode.PAZPAR2_ERRORS;\r
+      }\r
+    } else if (appError.getMessage().startsWith("Unexpected HTTP response")) {\r
+      Matcher m = httpResponsePattern.matcher(appError.getMessage());\r
       if (m.matches()) {\r
         String errorCode = m.group(1);\r
         if (errorCode.equals("404")) {\r
@@ -42,8 +62,10 @@ public class ErrorHelper implements Serializable {
           return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;\r
         }\r
       }       \r
-    } else if (error.getMessage().contains("Error reading service definition XML")) {\r
-      return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;\r
+    } else if (appError.getMessage().contains("Error reading service definition XML")) {\r
+      return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;    \r
+    } else if (appError.getMessage().contains("Cannot query Pazpar2 while there are configuration errors")) {\r
+      return ErrorCode.SKIP_SUGGESTIONS;\r
     }\r
     return ErrorCode.NOT_RESOLVED;\r
   }\r
@@ -56,9 +78,7 @@ public class ErrorHelper implements Serializable {
       suggestions.add("Pazpar2 service not found (404). ");\r
       suggestions.add("Please check the PAZPAR2_URL configuration and verify "\r
           + "that a pazpar2 service is running at the given address.");\r
-      suggestions.add("The application was configured using " + Utils.baseObjectName(configurator));\r
-      suggestions.add("The configurator reports following configuration was used: ");\r
-      suggestions.addAll(configurator.document());\r
+      addConfigurationDocumentation(suggestions);      \r
       break;\r
     case PAZPAR2_UNEXPECTED_RESPONSE:\r
       suggestions.add("Unexpected response code from Pazpar2. " + nl\r
@@ -68,16 +88,43 @@ public class ErrorHelper implements Serializable {
     case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:\r
       suggestions.add("The service definition file could not be loaded.");\r
       suggestions.add("Please check the configuration and verify that the file exists");\r
-      suggestions.add("The configurator reports following configuration was used: ");\r
-      suggestions.addAll(configurator.document());    \r
+      addConfigurationDocumentation(suggestions);     \r
       break;\r
     case REMOTE_SERVICE_DEF_NOT_FOUND:\r
       break;\r
     case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
+      suggestions.add("A configuration using local target settings file was found, but " +\r
+               " the file itself could not be found. Please check the configuration.");\r
+      addConfigurationDocumentation(suggestions);\r
       break;\r
     case NOT_RESOLVED:\r
+      suggestions.add("Unforeseen error situation. No suggestions prepared.");\r
+      break;\r
+    case SKIP_SUGGESTIONS:\r
+      break;\r
+    case PAZPAR2_12: \r
+      suggestions.add("The Pazpar2 service does not have a service definition with the requested ID ");\r
+      suggestions.add("Please check the service ID set in the configuration and compare it with the " +\r
+               " pazpar2 (server side) configuration.");\r
+      addConfigurationDocumentation(suggestions);    \r
+      break;\r
+    case PAZPAR2_ERRORS:\r
+      if (error.hasPazpar2Error()) {\r
+        if (error.getPazpar2Error().getCode().equals("0")) {\r
+          \r
+        }\r
+        suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");\r
+      } else {\r
+        logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");\r
+      }\r
       break;\r
     }\r
     return suggestions;\r
   }\r
+  \r
+  private void addConfigurationDocumentation (ArrayList<String> suggestions) {\r
+    suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));\r
+    suggestions.add("This configurator reports that following configuration was used: ");\r
+    suggestions.addAll(configurator.document());\r
+  }\r
 }\r