Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.time.DateTimeException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -212,6 +213,7 @@
import javax.sql.DataSource;
import org.apache.http.entity.ContentType;
import org.jetbrains.annotations.NotNull;
import org.jooq.exception.DataAccessException;
import org.owasp.html.HtmlPolicyBuilder;
import org.owasp.html.PolicyFactory;

Expand Down Expand Up @@ -380,8 +382,17 @@ public void init() {
CdaError re = new CdaError(e.getMessage());
ctx.status(HttpServletResponse.SC_BAD_REQUEST).json(re);
})
.exception(DataAccessException.class, (e, ctx) -> {
String message = getSanitizedDataAccessException(e);
CdaError errResponse = new CdaError("System Error", Map.of("Error message", message));
logger.atWarning().withCause(e).log("error on request[%s]: %s",
errResponse.getIncidentIdentifier(), ctx.req.getRequestURI());
ctx.status(500);
ctx.contentType(ContentType.APPLICATION_JSON.toString());
ctx.json(errResponse);
})
.exception(Exception.class, (e, ctx) -> {
CdaError errResponse = new CdaError("System Error");
CdaError errResponse = new CdaError("System Error", Map.of("Error message", e.getMessage()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will escape unexpected oracle stack traces. Should trap specific exceptions, see https://github.com/USACE/cwms-data-api/blob/develop/cwms-data-api/src/main/java/cwms/cda/data/dao/RateDao.java#L169

logger.atWarning().withCause(e).log("error on request[%s]: %s",
errResponse.getIncidentIdentifier(), ctx.req.getRequestURI());
ctx.status(500);
Expand All @@ -400,6 +411,21 @@ public void init() {
logger.atInfo().log("Javalin initialized.");
}

private static String getSanitizedDataAccessException(DataAccessException e) {
Throwable cause = e.getCause();
String message = "";
if (cause instanceof SQLException) {
String localizedMessage = cause.getLocalizedMessage();
String[] parts = localizedMessage.split("\n");
message = parts[0];
int index = message.indexOf(":");
if (index >= 0) {
message = message.substring(index + 1);
}
}
return message;
}

private String obtainFullVersion(ServletConfig servletConfig) throws ServletException {
String relativeWARPath = "/META-INF/MANIFEST.MF";
String absoluteDiskPath = servletConfig.getServletContext().getRealPath(relativeWARPath);
Expand Down
Loading