Revize 092ba8cb
Přidáno uživatelem Tomáš Šimandl před asi 6 roky(ů)
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/servlets/DataAccessError.java | ||
---|---|---|
1 |
package cz.zcu.kiv.offscreen.servlets; |
|
2 |
|
|
3 |
import com.google.gson.Gson; |
|
4 |
import com.google.gson.JsonObject; |
|
5 |
|
|
6 |
import javax.servlet.ServletException; |
|
7 |
import javax.servlet.http.HttpServlet; |
|
8 |
import javax.servlet.http.HttpServletRequest; |
|
9 |
import javax.servlet.http.HttpServletResponse; |
|
10 |
import java.io.IOException; |
|
11 |
import java.io.PrintWriter; |
|
12 |
|
|
13 |
public class DataAccessError extends HttpServlet { |
|
14 |
|
|
15 |
@Override |
|
16 |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
|
17 |
handleRequest(req, resp); |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
|
22 |
handleRequest(req, resp); |
|
23 |
} |
|
24 |
|
|
25 |
|
|
26 |
private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { |
|
27 |
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception"); |
|
28 |
response.setContentType("application/json"); |
|
29 |
PrintWriter out = response.getWriter(); |
|
30 |
|
|
31 |
JsonObject json = new JsonObject(); |
|
32 |
json.addProperty("exception", throwable.getMessage()); |
|
33 |
|
|
34 |
out.println(new Gson().toJson(json)); |
|
35 |
} |
|
36 |
} |
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/user/DB.java | ||
---|---|---|
34 | 34 |
connection.setAutoCommit(true); |
35 | 35 |
} catch (ClassNotFoundException | SQLException e) { |
36 | 36 |
logger.error("Can not open database connection: ", e); |
37 |
throw new DataAccessException(e); |
|
37 | 38 |
} |
38 | 39 |
} |
39 | 40 |
|
... | ... | |
43 | 44 |
* @param query query with ? on values. |
44 | 45 |
* @param returnGeneratedKeys true - RETURN_GENERATED_KEYS flag is set, false - no flag is set |
45 | 46 |
*/ |
46 |
PreparedStatement getPreparedStatement(String query, boolean returnGeneratedKeys) throws SQLException { |
|
47 |
if (returnGeneratedKeys) { |
|
48 |
return connection.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); |
|
49 |
} else { |
|
50 |
return connection.prepareStatement(query); |
|
47 |
PreparedStatement getPreparedStatement(String query, boolean returnGeneratedKeys) { |
|
48 |
try { |
|
49 |
if (returnGeneratedKeys) { |
|
50 |
return connection.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); |
|
51 |
} else { |
|
52 |
return connection.prepareStatement(query); |
|
53 |
} |
|
54 |
}catch (SQLException e){ |
|
55 |
logger.error("Can not execute query: " + query); |
|
56 |
throw new DataAccessException(e); |
|
51 | 57 |
} |
52 | 58 |
} |
53 | 59 |
|
... | ... | |
63 | 69 |
return preparedStatement.getResultSet(); |
64 | 70 |
} catch (SQLException e) { |
65 | 71 |
logger.error("Can not execute database query: ", e); |
72 |
throw new DataAccessException(e); |
|
66 | 73 |
} |
67 |
return null; |
|
68 | 74 |
} |
69 | 75 |
|
70 | 76 |
/** |
... | ... | |
78 | 84 |
return preparedStatement.getGeneratedKeys(); |
79 | 85 |
} catch (SQLException e) { |
80 | 86 |
logger.error("Can not execute database query: ", e); |
87 |
throw new DataAccessException(e); |
|
81 | 88 |
} |
82 |
return null; |
|
83 | 89 |
} |
84 | 90 |
|
85 | 91 |
/** |
... | ... | |
95 | 101 |
|
96 | 102 |
} catch(SQLException | NullPointerException e) { |
97 | 103 |
logger.error("Can not execute database query: ", e); |
104 |
throw new DataAccessException(e); |
|
98 | 105 |
} |
99 |
return null; |
|
100 | 106 |
} |
101 | 107 |
|
102 | 108 |
/** |
... | ... | |
107 | 113 |
int executeStatement(PreparedStatement preparedStatement) { |
108 | 114 |
try { |
109 | 115 |
return preparedStatement.executeUpdate(); |
110 |
} catch(SQLException e) { |
|
111 |
logger.error("Can not execute database query: ", e); |
|
112 |
return -1; |
|
113 |
} catch (NullPointerException e) { |
|
116 |
} catch(NullPointerException | SQLException e) { |
|
114 | 117 |
logger.error("Can not execute database query: ", e); |
115 |
return -2;
|
|
118 |
throw new DataAccessException(e);
|
|
116 | 119 |
} |
117 | 120 |
} |
118 | 121 |
|
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/user/DataAccessException.java | ||
---|---|---|
1 |
package cz.zcu.kiv.offscreen.user; |
|
2 |
|
|
3 |
public class DataAccessException extends RuntimeException { |
|
4 |
|
|
5 |
public DataAccessException(Throwable cause) { |
|
6 |
super(cause); |
|
7 |
} |
|
8 |
} |
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/user/Diagram.java | ||
---|---|---|
58 | 58 |
} |
59 | 59 |
} catch (SQLException e){ |
60 | 60 |
logger.error("Can not get owner id: ", e); |
61 |
throw new DataAccessException(e); |
|
61 | 62 |
} |
62 | 63 |
return -1; |
63 | 64 |
} |
... | ... | |
78 | 79 |
} |
79 | 80 |
} catch (SQLException e){ |
80 | 81 |
logger.error("Can not check if diagram is public: ", e); |
82 |
throw new DataAccessException(e); |
|
81 | 83 |
} |
82 | 84 |
return false; |
83 | 85 |
} |
... | ... | |
97 | 99 |
} |
98 | 100 |
} catch (SQLException e){ |
99 | 101 |
logger.error("Can not get json of diagram: ", e); |
102 |
throw new DataAccessException(e); |
|
100 | 103 |
} |
101 | 104 |
return ""; |
102 | 105 |
} |
... | ... | |
120 | 123 |
|
121 | 124 |
} catch (SQLException e) { |
122 | 125 |
logger.error("Can not get diagram: ", e); |
126 |
throw new DataAccessException(e); |
|
123 | 127 |
} |
124 | 128 |
|
125 | 129 |
return Collections.emptyMap(); |
... | ... | |
172 | 176 |
} |
173 | 177 |
} catch (SQLException e) { |
174 | 178 |
logger.error("Can not update diagram: ", e); |
179 |
throw new DataAccessException(e); |
|
175 | 180 |
} |
176 | 181 |
} |
177 | 182 |
|
... | ... | |
190 | 195 |
|
191 | 196 |
} catch (SQLException e) { |
192 | 197 |
logger.error("Can not delete diagram: ", e); |
198 |
throw new DataAccessException(e); |
|
193 | 199 |
} |
194 | 200 |
} |
195 | 201 |
|
... | ... | |
235 | 241 |
|
236 | 242 |
} catch (SQLException e){ |
237 | 243 |
logger.error("Can not create map from input result set: ", e); |
244 |
throw new DataAccessException(e); |
|
238 | 245 |
} |
239 |
|
|
240 |
return new ArrayList<>(); |
|
241 | 246 |
} |
242 | 247 |
|
243 | 248 |
/** |
sources/imiger-core/src/main/java/cz/zcu/kiv/offscreen/user/User.java | ||
---|---|---|
62 | 62 |
} |
63 | 63 |
} catch (SQLException | IllegalArgumentException e) { |
64 | 64 |
logger.error("Can not login user: ", e); |
65 |
throw new DataAccessException(e); |
|
65 | 66 |
} |
66 | 67 |
|
67 | 68 |
return false; |
... | ... | |
98 | 99 |
|
99 | 100 |
} catch (SQLException e) { |
100 | 101 |
logger.error("Can not register user: ", e); |
102 |
throw new DataAccessException(e); |
|
101 | 103 |
} |
102 | 104 |
} |
103 | 105 |
|
... | ... | |
130 | 132 |
} |
131 | 133 |
} catch (SQLException e) { |
132 | 134 |
logger.error("Can not get user: ", e); |
135 |
throw new DataAccessException(e); |
|
133 | 136 |
} |
134 | 137 |
|
135 | 138 |
return Collections.emptyMap(); |
... | ... | |
159 | 162 |
|
160 | 163 |
} catch (SQLException e) { |
161 | 164 |
logger.error("Can not get user session: ", e); |
165 |
throw new DataAccessException(e); |
|
162 | 166 |
} |
163 | 167 |
} |
164 | 168 |
} |
... | ... | |
181 | 185 |
db.executeStatement(pst); |
182 | 186 |
} catch (SQLException e) { |
183 | 187 |
logger.error("Can not update user: ", e); |
188 |
throw new DataAccessException(e); |
|
184 | 189 |
} |
185 | 190 |
} |
186 | 191 |
} |
... | ... | |
199 | 204 |
} |
200 | 205 |
} catch (SQLException e){ |
201 | 206 |
logger.error("Can not get users nick name: ", e); |
207 |
throw new DataAccessException(e); |
|
202 | 208 |
} |
203 | 209 |
} |
204 | 210 |
|
... | ... | |
241 | 247 |
} |
242 | 248 |
} catch (SQLException e) { |
243 | 249 |
logger.error("Can not check if user exits: ", e); |
250 |
throw new DataAccessException(e); |
|
244 | 251 |
} |
245 | 252 |
|
246 | 253 |
return false; |
sources/imiger-core/src/main/webapp/WEB-INF/web.xml | ||
---|---|---|
80 | 80 |
<servlet-name>RestRawInput</servlet-name> |
81 | 81 |
<servlet-class>cz.zcu.kiv.offscreen.servlets.rest.RawInput</servlet-class> |
82 | 82 |
</servlet> |
83 |
<servlet> |
|
84 |
<servlet-name>DataAccessError</servlet-name> |
|
85 |
<servlet-class>cz.zcu.kiv.offscreen.servlets.DataAccessError</servlet-class> |
|
86 |
</servlet> |
|
83 | 87 |
|
84 | 88 |
<!-- |
85 | 89 |
URL-to-servlet mappings |
... | ... | |
132 | 136 |
<servlet-name>RestRawInput</servlet-name> |
133 | 137 |
<url-pattern>/rest/raw-input</url-pattern> |
134 | 138 |
</servlet-mapping> |
139 |
<servlet-mapping> |
|
140 |
<servlet-name>DataAccessError</servlet-name> |
|
141 |
<url-pattern>/data-access-error</url-pattern> |
|
142 |
</servlet-mapping> |
|
143 |
|
|
144 |
<error-page> |
|
145 |
<exception-type>cz.zcu.kiv.offscreen.user.DataAccessException</exception-type> |
|
146 |
<location>/data-access-error</location> |
|
147 |
</error-page> |
|
135 | 148 |
|
136 | 149 |
<session-config> |
137 | 150 |
<session-timeout>120</session-timeout> |
Také k dispozici: Unified diff
#24 throws exceptions early, catch them late
- throw own exception and handle it with error page