-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from nedtwigg/feature/strictExceptions
Any exception should break the build
- Loading branch information
Showing
29 changed files
with
481 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
lib/src/main/java/com/diffplug/spotless/FormatExceptionPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless; | ||
|
||
import java.io.Serializable; | ||
|
||
/** 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, String relativePath); | ||
|
||
/** | ||
* Returns a byte array representation of everything inside this `FormatExceptionPolicy`. | ||
* | ||
* The main purpose of this method is to ensure one can't instantiate this class with lambda | ||
* expressions, which are notoriously difficult to serialize and deserialize properly. | ||
*/ | ||
public byte[] toBytes(); | ||
|
||
/** | ||
* A policy which rethrows subclasses of `Error` and logs other kinds of Exception. | ||
*/ | ||
public static FormatExceptionPolicy failOnlyOnError() { | ||
return new FormatExceptionPolicyLegacy(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
lib/src/main/java/com/diffplug/spotless/FormatExceptionPolicyLegacy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
class FormatExceptionPolicyLegacy extends NoLambda.EqualityBasedOnSerialization implements FormatExceptionPolicy { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Logger logger = Logger.getLogger(Formatter.class.getName()); | ||
|
||
@Override | ||
public void handleError(Throwable e, FormatterStep step, String relativePath) { | ||
if (e instanceof Error) { | ||
error(e, step, relativePath); | ||
throw ((Error) e); | ||
} else { | ||
warning(e, step, relativePath); | ||
} | ||
} | ||
|
||
static void error(Throwable e, FormatterStep step, String relativePath) { | ||
logger.log(Level.SEVERE, "Step '" + step.getName() + "' found problem in '" + relativePath + "':\n" + e.getMessage(), e); | ||
} | ||
|
||
static void warning(Throwable e, FormatterStep step, String relativePath) { | ||
logger.log(Level.WARNING, "Unable to apply step '" + step.getName() + "' to '" + relativePath + "'", e); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
lib/src/main/java/com/diffplug/spotless/FormatExceptionPolicyStrict.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless; | ||
|
||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* A policy for handling exceptions in the format. Any exceptions will | ||
* halt the build except for a specifically excluded path or step. | ||
*/ | ||
public class FormatExceptionPolicyStrict extends NoLambda.EqualityBasedOnSerialization implements FormatExceptionPolicy { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final Set<String> excludeSteps = new TreeSet<>(); | ||
private final Set<String> excludePaths = new TreeSet<>(); | ||
|
||
/** Adds a step name to exclude. */ | ||
public void excludeStep(String stepName) { | ||
excludeSteps.add(stepName); | ||
} | ||
|
||
/** Adds a realtive pathx to exclude. */ | ||
public void excludePath(String relativePath) { | ||
excludePaths.add(relativePath); | ||
} | ||
|
||
@Override | ||
public void handleError(Throwable e, FormatterStep step, String relativePath) { | ||
if (excludeSteps.contains(step.getName())) { | ||
FormatExceptionPolicyLegacy.warning(e, step, relativePath); | ||
} else { | ||
if (excludePaths.contains(relativePath)) { | ||
FormatExceptionPolicyLegacy.warning(e, step, relativePath); | ||
} else { | ||
FormatExceptionPolicyLegacy.error(e, step, relativePath); | ||
throw ThrowingEx.asRuntimeRethrowError(e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.