Improve some error handling to clean up the catalina log.

This commit is contained in:
Jim Blake 2015-04-02 17:05:47 -04:00
parent 6aa29378e5
commit ced99d7703
2 changed files with 20 additions and 7 deletions

View file

@ -3,6 +3,7 @@
package edu.cornell.mannlib.vitro.webapp.controller.api; package edu.cornell.mannlib.vitro.webapp.controller.api;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Collection; import java.util.Collection;
@ -46,13 +47,13 @@ public class VitroApiServlet extends HttpServlet {
Authenticator auth = Authenticator.getInstance(req); Authenticator auth = Authenticator.getInstance(req);
UserAccount account = auth.getAccountForInternalAuth(email); UserAccount account = auth.getAccountForInternalAuth(email);
if (auth.accountRequiresEditing(account)) { if (auth.accountRequiresEditing(account)) {
log.debug("Account " + email + " requires editing."); log.debug("Account " + email + " requires editing.");
throw new AuthException("user account must include first and " throw new AuthException("user account must include first and "
+ "last names and a valid email address."); + "last names and a valid email address.");
} }
if (!auth.isCurrentPassword(account, password)) { if (!auth.isCurrentPassword(account, password)) {
log.debug("Invalid: '" + email + "'/'" + password + "'"); log.debug("Invalid: '" + email + "'/'" + password + "'");
throw new AuthException("email/password combination is not valid"); throw new AuthException("email/password combination is not valid");
@ -68,7 +69,7 @@ public class VitroApiServlet extends HttpServlet {
log.debug("Account " + email + " requires a new password."); log.debug("Account " + email + " requires a new password.");
throw new AuthException("user account requires a new password."); throw new AuthException("user account requires a new password.");
} }
log.debug("Authorized for '" + email + "'"); log.debug("Authorized for '" + email + "'");
} }
@ -86,19 +87,29 @@ public class VitroApiServlet extends HttpServlet {
protected void sendShortResponse(int statusCode, String message, protected void sendShortResponse(int statusCode, String message,
HttpServletResponse resp) throws IOException { HttpServletResponse resp) throws IOException {
resp.setStatus(statusCode); resp.setStatus(statusCode);
PrintWriter writer = resp.getWriter(); PrintWriter writer = getWriter(resp);
writer.println("<H1>" + statusCode + " " + message + "</H1>"); writer.println("<H1>" + statusCode + " " + message + "</H1>");
} }
protected void sendShortResponse(int statusCode, String message, protected void sendShortResponse(int statusCode, String message,
Throwable e, HttpServletResponse resp) throws IOException { Throwable e, HttpServletResponse resp) throws IOException {
log.warn("Unexpected exception: " + e, e);
sendShortResponse(statusCode, message, resp); sendShortResponse(statusCode, message, resp);
PrintWriter writer = resp.getWriter(); PrintWriter writer = getWriter(resp);
writer.println("<pre>"); writer.println("<pre>");
e.printStackTrace(writer); e.printStackTrace(writer);
writer.println("</pre>"); writer.println("</pre>");
} }
private PrintWriter getWriter(HttpServletResponse resp) throws IOException {
try {
return resp.getWriter();
} catch (IllegalStateException e) {
return new PrintWriter(new OutputStreamWriter(
resp.getOutputStream()));
}
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Helper classes // Helper classes
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View file

@ -117,13 +117,15 @@ public class FileServingServlet extends VitroHttpServlet {
try { try {
in.close(); in.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.warn("Serving " + request.getRequestURI()
+ ". Failed to close input stream.", e);
} }
if (out != null) { if (out != null) {
try { try {
out.close(); out.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.warn("Serving " + request.getRequestURI()
+ ". Failed to close output stream.", e);
} }
} }
} }