Skip to content

Commit

Permalink
FormatExceptionPolicy now takes (String relativePath) rather than (Fi…
Browse files Browse the repository at this point in the history
…le file, Path rootDir).
  • Loading branch information
nedtwigg committed Jan 18, 2017
1 parent 672051d commit 67ea815
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
package com.diffplug.spotless;

import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;

/** A policy for handling exceptions in the format. */
public interface FormatExceptionPolicy extends Serializable, NoLambda {
/** Called for every error in the formatter. */
void handleError(Throwable e, FormatterStep step, File file, Path rootDir);
void handleError(Throwable e, FormatterStep step, String relativePath);

/**
* Returns a byte array representation of everything inside this `FormatExceptionPolicy`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.diffplug.spotless;

import java.io.File;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -26,20 +24,16 @@ class FormatExceptionPolicyLegacy extends NoLambda.EqualityBasedOnSerialization
private static final Logger logger = Logger.getLogger(Formatter.class.getName());

@Override
public void handleError(Throwable e, FormatterStep step, File file, Path rootDir) {
public void handleError(Throwable e, FormatterStep step, String relativePath) {
if (e instanceof Error) {
error(e, step, file, rootDir);
log(Level.SEVERE, e, step, relativePath);
throw ((Error) e);
} else {
warning(e, step, file, rootDir);
log(Level.WARNING, e, step, relativePath);
}
}

static void error(Throwable e, FormatterStep step, File file, Path rootDir) {
logger.severe("Step '" + step.getName() + "' found problem in '" + rootDir.relativize(file.toPath()) + "':\n" + e.getMessage());
}

static void warning(Throwable e, FormatterStep step, File file, Path rootDir) {
logger.log(Level.WARNING, "Unable to apply step '" + step.getName() + "' to '" + rootDir.relativize(file.toPath()), e);
static void log(Level level, Throwable e, FormatterStep step, String relativePath) {
logger.log(level, "Unable to apply step '" + step.getName() + "' to '" + relativePath + "'", e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package com.diffplug.spotless;

import java.io.File;
import java.nio.file.Path;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;

/**
* A policy for handling exceptions in the format. Any exceptions will
Expand All @@ -41,15 +40,14 @@ public void excludePath(String relativePath) {
}

@Override
public void handleError(Throwable e, FormatterStep step, File file, Path rootDir) {
public void handleError(Throwable e, FormatterStep step, String relativePath) {
if (excludeSteps.contains(step.getName())) {
FormatExceptionPolicyLegacy.warning(e, step, file, rootDir);
FormatExceptionPolicyLegacy.log(Level.WARNING, e, step, relativePath);
} else {
String path = rootDir.relativize(file.toPath()).toString();
if (excludePaths.contains(path)) {
FormatExceptionPolicyLegacy.warning(e, step, file, rootDir);
if (excludePaths.contains(relativePath)) {
FormatExceptionPolicyLegacy.log(Level.WARNING, e, step, relativePath);
} else {
FormatExceptionPolicyLegacy.error(e, step, file, rootDir);
FormatExceptionPolicyLegacy.log(Level.SEVERE, e, step, relativePath);
throw ThrowingEx.asRuntimeRethrowError(e);
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ public String compute(String unix, File file) throws Error {
unix = LineEnding.toUnix(formatted);
}
} catch (Throwable e) {
exceptionPolicy.handleError(e, step, file, rootDir);
String relativePath = rootDir.relativize(file.toPath()).toString();
exceptionPolicy.handleError(e, step, relativePath);
}
}
return unix;
Expand Down

0 comments on commit 67ea815

Please sign in to comment.