Skip to content

Commit

Permalink
fix: parse error serialisation
Browse files Browse the repository at this point in the history
Convert parse errors into strings manually to avoid JSON serialisation
failure due to circular references.

Fixes #406
  • Loading branch information
stevenh committed Aug 27, 2024
1 parent 6ec453b commit ebe6662
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion groovy/src/main/com/nvuillam/CodeNarcServer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class CodeNarcServer {
WRITER.writeValue(out, response)
}
} catch (Exception e) {
LOGGER.error('Write response {}', e)
LOGGER.error('Write response', e)
} finally {
if (requestKey) {
threads.remove(requestKey)
Expand Down
21 changes: 17 additions & 4 deletions groovy/src/main/com/nvuillam/Request.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class Request {
* @param fileList the list of files to parse
* @return the map of files to errors
*/
private Map<String, List<Error>> parseFiles(List<String> fileList) {
Map<String, List<Error>> parseErrors = [:]
private Map<String, List<String>> parseFiles(List<String> fileList) {
Map<String, List<String>> parseErrors = [:]
LOGGER.debug('parseFiles: parse={}, fileList={}', parse, fileList)
if (parse) {
fileList.each { file ->
Expand Down Expand Up @@ -210,8 +210,21 @@ class Request {
return files
}

// Try to parse file to get compilation errors
private List<Error> parseFile(File file) {
// Try to parse file to get compilation errors as strings.
private List<String> parseFile(File file) {
List<String> errors = []
for (err in parseFileErrors(file)) {
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
err.write(writer)
errors << out.toString()
}

return errors
}

// Try to parse file to get compilation errors.
private List<Error> parseFileErrors(File file) {
try {
// We don't use GroovyShell.parse as it calls InvokerHelper.createScript
// which fails for files which contain a class which only have non-zero
Expand Down
2 changes: 1 addition & 1 deletion groovy/src/main/com/nvuillam/Response.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Response {
String errorMessage
String errorDtl
String exceptionType
Map<String, List<Error>> parseErrors
Map<String, List<String>> parseErrors

// Lint result.
@JsonRawValue
Expand Down
Binary file modified lib/java/CodeNarcServer.jar
Binary file not shown.

0 comments on commit ebe6662

Please sign in to comment.