Skip to content

Commit af2a2f1

Browse files
Pass exception to log4j Logger
1 parent 4a17c9c commit af2a2f1

5 files changed

Lines changed: 21 additions & 19 deletions

File tree

src/main/java/com/codedx/plugins/bamboo/CodeDxScanTask.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private static Boolean collectFilesToUpload(ScanTaskState state) {
204204
try {
205205
state.filesToUpload.add(Archiver.archive(state.taskContext.getRootDirectory(), state.includePaths, state.excludePaths, "files_to_scan"));
206206
} catch (IOException e) {
207-
logError(state, "An error occurred while trying to archive source files: %s", e.toString());
207+
logError(state, e, "An error occurred while trying to archive source files");
208208
return false;
209209
}
210210

@@ -249,7 +249,7 @@ private static Boolean uploadFiles(ScanTaskState state) {
249249
logApiException(state, e);
250250
return false;
251251
} catch (IOException e) {
252-
logError(state, "An error occurred while trying to archive source files: %s", e.toString());
252+
logError(state, e, "An error occurred while trying to archive source files");
253253
return false;
254254
}
255255

@@ -265,7 +265,7 @@ private static Boolean waitForCodeDxToBeReadyForAnalysis(ScanTaskState state) {
265265
try {
266266
Thread.sleep(1000); // Consider adding maximum wait time
267267
} catch (InterruptedException e) {
268-
logError(state, "An unexpected threading issue occurred. This could occur if the build is manually cancelled. Exception: %s", e.toString());
268+
logError(state, e, "An unexpected threading issue occurred. This could occur if the build is manually cancelled.");
269269
return false;
270270
}
271271

@@ -354,7 +354,7 @@ private static Boolean waitForAnalysisToFinish(ScanTaskState state) {
354354
try {
355355
Thread.sleep(5000); // Consider adding maximum wait time
356356
} catch (InterruptedException e) {
357-
logError(state, "An unexpected threading issue occurred. This could occur if the build is manually cancelled. Exception: %s", e.toString());
357+
logError(state, e, "An unexpected threading issue occurred. This could occur if the build is manually cancelled.");
358358
return false;
359359
}
360360

@@ -468,13 +468,19 @@ private static void log(ScanTaskState state, String format, Object... args) {
468468
_logger.info(message);
469469
}
470470

471+
private static void logError(ScanTaskState state, Exception e, String format, Object... args) {
472+
String errorMessage = String.format(format, args);
473+
state.buildLogger.addErrorLogEntry(String.format("%s || Exception - %s", errorMessage, e.toString()));
474+
_logger.error(errorMessage, e);
475+
}
476+
471477
private static void logError(ScanTaskState state, String format, Object... args) {
472-
String error = String.format(format, args);
473-
state.buildLogger.addErrorLogEntry(error);
474-
_logger.error(error);
478+
String errorMessage = String.format(format, args);
479+
state.buildLogger.addErrorLogEntry(errorMessage);
480+
_logger.error(errorMessage);
475481
}
476482

477483
private static void logApiException(ScanTaskState state, Exception e) {
478-
logError(state, "An error occurred while trying to communicate with Code Dx's API: %s", e.toString());
484+
logError(state, e, "An error occurred while trying to communicate with Code Dx's API");
479485
}
480486
}

src/main/java/com/codedx/plugins/bamboo/CodeDxScanTaskConfigurator.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,12 @@ private static List<Project> getProjectList(Map<String, Object> context) {
173173
return projectsApi.getProjects().getProjects();
174174

175175
} catch (IllegalArgumentException e) {
176-
e.printStackTrace();
177-
_logger.error(e.toString());
176+
_logger.error(e);
178177
} catch (ApiException e) {
179-
e.printStackTrace();
180-
_logger.error(e.toString());
178+
_logger.error(e);
181179
} catch (ProcessingException e) {
182180
context.put("reachabilityMessage", "Connection refused. Please confirm that the URL is correct and that the Code Dx server is running.");
183-
_logger.error(e.toString());
181+
_logger.error(e);
184182
}
185183

186184
return new ArrayList<Project>();

src/main/java/com/codedx/plugins/bamboo/ProjectRefresherServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
8282
projects = projectsApi.getProjects();
8383
} catch (ApiException e) {
8484

85-
_logger.error(e.toString());
85+
_logger.error(e);
8686

8787
// Bad API Token?
8888
int responseCode = e.getCode();
@@ -109,7 +109,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
109109
_logger.error("Error message to send to client: " + message);
110110
return;
111111
} catch (ProcessingException e) {
112-
_logger.error(e.toString());
112+
_logger.error(e);
113113
resp.setStatus(404);
114114
resp.getOutputStream().print("Connection refused. Please confirm that the URL is correct and that the Code Dx server is running.");
115115
return;

src/main/java/com/codedx/plugins/bamboo/ServerConfigManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ public static ApiClient getConfiguredClient(String url, String apiKey, String fi
104104
.build();
105105
cdxApiClient.setHttpClient(client);
106106
} catch (Exception e) {
107-
_logger.error(e.toString());
108-
e.printStackTrace();
107+
_logger.error(e);
109108
}
110109
}
111110
return cdxApiClient;

src/main/java/com/codedx/plugins/bamboo/security/FingerprintStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public CertificateAcceptance checkAcceptance(Certificate genericCert, Certificat
3737
return CertificateAcceptance.ACCEPT_PERMANENTLY;
3838
}
3939
} catch (CertificateEncodingException exception) {
40-
_logger.error("Problem reading certificate: " + exception);
41-
exception.printStackTrace();
40+
_logger.error("Problem reading certificate", exception);
4241
}
4342
} else {
4443
_logger.error("Certificate presented was not X509: " + genericCert);

0 commit comments

Comments
 (0)