Work on error handling
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / errors / ErrorHelper.java
1 package com.indexdata.pz2utils4jsf.errors;\r
2 \r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
4 \r
5 import java.io.Serializable;\r
6 import java.util.ArrayList;\r
7 import java.util.regex.Matcher;\r
8 import java.util.regex.Pattern;\r
9 \r
10 import org.apache.log4j.Logger;\r
11 \r
12 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
13 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error;\r
14 import com.indexdata.pz2utils4jsf.utils.Utils;\r
15 \r
16 public class ErrorHelper implements Serializable {\r
17 \r
18   public enum ErrorCode {PAZPAR2_404, \r
19                          PAZPAR2_UNEXPECTED_RESPONSE,\r
20                          PAZPAR2_12,\r
21                          PAZPAR2_ERRORS,\r
22                          LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
23                          REMOTE_SERVICE_DEF_NOT_FOUND,\r
24                          LOCAL_SETTINGS_FILE_NOT_FOUND,\r
25                          MASTERKEY_CONFIG_FILE_NOT_FOUND,\r
26                          MISSING_MANDATORY_PARAMETER,\r
27                          NOT_RESOLVED,\r
28                          SKIP_SUGGESTIONS};\r
29 \r
30   private static final long serialVersionUID = 2860804561068279131L;\r
31   private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
32   \r
33   private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
34   \r
35   private Pz2Configurator configurator = null;\r
36   \r
37   public ErrorHelper(Pz2Configurator configurator) {\r
38     this.configurator = configurator;\r
39   }\r
40   \r
41   public ErrorHelper.ErrorCode getErrorCode(ErrorInterface appError) {\r
42     if (appError.hasPazpar2Error()) {\r
43       Pazpar2Error pz2err = appError.getPazpar2Error();\r
44       String pz2errcode = pz2err.getCode();\r
45       switch (pz2errcode) {\r
46       case "12": \r
47         return ErrorCode.PAZPAR2_12;\r
48       case "0":    \r
49         if (pz2err.getMsg().contains("target settings from file")) {\r
50           return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;\r
51         } else {\r
52           return ErrorCode.PAZPAR2_ERRORS;\r
53         }\r
54       default: \r
55         return ErrorCode.PAZPAR2_ERRORS;\r
56       }\r
57     } else if (appError.getMessage().startsWith("Unexpected HTTP response")) {\r
58       Matcher m = httpResponsePattern.matcher(appError.getMessage());\r
59       if (m.matches()) {\r
60         String errorCode = m.group(1);\r
61         if (errorCode.equals("404")) {\r
62           return ErrorCode.PAZPAR2_404;\r
63         } else {\r
64           return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;\r
65         }\r
66       }       \r
67     } else if (appError.getMessage().contains("Configuration file") & appError.getMessage().contains("properties")) {\r
68       return ErrorCode.MASTERKEY_CONFIG_FILE_NOT_FOUND; \r
69     } else if (appError.getMessage().contains("Error reading service definition XML")) {\r
70       return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;    \r
71     } else if (appError.getMessage().contains("Cannot query Pazpar2 while there are configuration errors")) {\r
72       return ErrorCode.SKIP_SUGGESTIONS;\r
73     } else if (appError.getMessage().contains("Missing mandatory parameter")) {\r
74       return ErrorCode.MISSING_MANDATORY_PARAMETER;\r
75     }\r
76     return ErrorCode.NOT_RESOLVED;\r
77   }\r
78     \r
79   public ArrayList<String> getSuggestions(ErrorInterface error) {\r
80     ArrayList<String> suggestions = new ArrayList<String>();\r
81     ErrorCode code = getErrorCode(error);\r
82     switch (code) {\r
83     case PAZPAR2_404:\r
84       suggestions.add("Pazpar2 service not found (404). ");\r
85       suggestions.add("Please check the PAZPAR2_URL configuration and verify "\r
86           + "that a pazpar2 service is running at the given address.");\r
87       addConfigurationDocumentation(suggestions);      \r
88       break;\r
89     case PAZPAR2_UNEXPECTED_RESPONSE:\r
90       suggestions.add("Unexpected response code from Pazpar2. " + nl\r
91           + "Please check the PAZPAR2_URL configuration and verify "\r
92           + "that a pazpar2 service is running at the given address." + nl);\r
93       break;     \r
94     case MASTERKEY_CONFIG_FILE_NOT_FOUND: \r
95       suggestions.add("The main configuration file that is looked up using parameters" +\r
96                 " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" +\r
97                 " could not be found. Please check the web.xml parameters and the expected file system location. ");      \r
98       break;\r
99     case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:\r
100       suggestions.add("The service definition file could not be loaded.");\r
101       suggestions.add("Please check the configuration and verify that the file exists");\r
102       addConfigurationDocumentation(suggestions);     \r
103       break;\r
104     case REMOTE_SERVICE_DEF_NOT_FOUND:\r
105       break;\r
106     case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
107       suggestions.add("A configuration using local target settings file was found, but " +\r
108                 " the file itself could not be found. Please check the configuration.");\r
109       addConfigurationDocumentation(suggestions);\r
110       break;\r
111     case MISSING_MANDATORY_PARAMETER:\r
112       suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" +\r
113                 " file used. Please check the property file for the parameter given in the error message ");\r
114       addConfigurationDocumentation(suggestions);\r
115       break;\r
116     case NOT_RESOLVED:\r
117       suggestions.add("Unforeseen error situation. No suggestions prepared.");\r
118       break;\r
119     case SKIP_SUGGESTIONS:\r
120       break;\r
121     case PAZPAR2_12: \r
122       suggestions.add("The Pazpar2 service does not have a service definition with the requested ID ");\r
123       suggestions.add("Please check the service ID set in the configuration and compare it with the " +\r
124                 " pazpar2 (server side) configuration.");\r
125       addConfigurationDocumentation(suggestions);    \r
126       break;\r
127     case PAZPAR2_ERRORS:\r
128       if (error.hasPazpar2Error()) {\r
129         if (error.getPazpar2Error().getCode().equals("0")) {\r
130           \r
131         }\r
132         suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");\r
133       } else {\r
134         logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");\r
135       }\r
136       break;\r
137     }\r
138     return suggestions;\r
139   }\r
140   \r
141   private void addConfigurationDocumentation (ArrayList<String> suggestions) {\r
142     suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));\r
143     suggestions.add("This configurator reports that following configuration was used: ");\r
144     suggestions.addAll(configurator.document());\r
145   }\r
146 }\r