-
-
Notifications
You must be signed in to change notification settings - Fork 792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ErrorReportConfiguration
#1067
Merged
cowtowncoder
merged 3 commits into
FasterXML:2.16
from
JooHyukKim:1043-1-add-new-ErrorReportConfiguration
Jul 29, 2023
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
src/main/java/com/fasterxml/jackson/core/ErrorReportConfiguration.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,191 @@ | ||
package com.fasterxml.jackson.core; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Container for configuration values used when handling errorneous token inputs. | ||
* For example, unquoted text segments. | ||
* <p> | ||
* Currently default settings are | ||
* <ul> | ||
* <li>Maximum length of token to include in error messages (see {@link #_maxErrorTokenLength}) | ||
* <li>Maximum length of raw content to include in error messages (see {@link #_maxRawContentLength}) | ||
* </ul> | ||
* | ||
* @since 2.16 | ||
*/ | ||
public class ErrorReportConfiguration | ||
implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Default value for {@link #_maxErrorTokenLength}. | ||
*/ | ||
public static final int DEFAULT_MAX_ERROR_TOKEN_LENGTH = 256; | ||
|
||
/** | ||
* Previous was {@link com.fasterxml.jackson.core.io.ContentReference#DEFAULT_MAX_CONTENT_SNIPPET}. | ||
* Default value for {@link #_maxRawContentLength}. | ||
*/ | ||
public static final int DEFAULT_MAX_RAW_CONTENT_LENGTH = 500; | ||
|
||
/** | ||
* Maximum length of token to include in error messages | ||
* | ||
* @see Builder#maxErrorTokenLength(int) | ||
*/ | ||
protected final int _maxErrorTokenLength; | ||
|
||
/** | ||
* Maximum length of raw content to include in error messages | ||
* | ||
* @see Builder#maxRawContentLength(int) | ||
*/ | ||
protected final int _maxRawContentLength; | ||
|
||
private static ErrorReportConfiguration DEFAULT = | ||
new ErrorReportConfiguration(DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULT_MAX_RAW_CONTENT_LENGTH); | ||
|
||
public static void overrideDefaultErrorReportConfiguration(final ErrorReportConfiguration errorReportConfiguration) { | ||
if (errorReportConfiguration == null) { | ||
DEFAULT = new ErrorReportConfiguration(DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULT_MAX_RAW_CONTENT_LENGTH); | ||
} else { | ||
DEFAULT = errorReportConfiguration; | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Builder | ||
/********************************************************************** | ||
*/ | ||
|
||
public static final class Builder { | ||
private int maxErrorTokenLength; | ||
private int maxRawContentLength; | ||
|
||
/** | ||
* @param maxErrorTokenLength Constraints | ||
* @return This factory instance (to allow call chaining) | ||
* @throws IllegalArgumentException if {@code maxErrorTokenLength} is less than 0 | ||
*/ | ||
public Builder maxErrorTokenLength(final int maxErrorTokenLength) { | ||
validateMaxErrorTokenLength(maxErrorTokenLength); | ||
this.maxErrorTokenLength = maxErrorTokenLength; | ||
return this; | ||
} | ||
|
||
/** | ||
* | ||
* @see ErrorReportConfiguration#_maxRawContentLength | ||
* @return This factory instance (to allow call chaining) | ||
*/ | ||
public Builder maxRawContentLength(final int maxRawContentLength) { | ||
validateMaxRawContentLength(maxRawContentLength); | ||
this.maxRawContentLength = maxRawContentLength; | ||
return this; | ||
} | ||
|
||
Builder() { | ||
this(DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULT_MAX_RAW_CONTENT_LENGTH); | ||
} | ||
|
||
Builder(final int maxErrorTokenLength, final int maxRawContentLength) { | ||
this.maxErrorTokenLength = maxErrorTokenLength; | ||
this.maxRawContentLength = maxRawContentLength; | ||
} | ||
|
||
Builder(ErrorReportConfiguration src) { | ||
this.maxErrorTokenLength = src._maxErrorTokenLength; | ||
this.maxRawContentLength = src._maxRawContentLength; | ||
} | ||
|
||
public ErrorReportConfiguration build() { | ||
return new ErrorReportConfiguration(maxErrorTokenLength, maxRawContentLength); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Life-cycle | ||
/********************************************************************** | ||
*/ | ||
|
||
protected ErrorReportConfiguration(final int maxErrorTokenLength, final int maxRawContentLength) { | ||
_maxErrorTokenLength = maxErrorTokenLength; | ||
_maxRawContentLength = maxRawContentLength; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* @return the default {@link ErrorReportConfiguration} (when none is set on the {@link JsonFactory} explicitly) | ||
* @see #overrideDefaultErrorReportConfiguration(ErrorReportConfiguration) | ||
*/ | ||
public static ErrorReportConfiguration defaults() { | ||
return DEFAULT; | ||
} | ||
|
||
/** | ||
* @return New {@link Builder} initialized with settings of configuration | ||
* instance | ||
*/ | ||
public Builder rebuild() { | ||
return new Builder(this); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Accessors | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* Accessor for {@link #_maxErrorTokenLength} | ||
* | ||
* @return Maximum length of token to include in error messages | ||
* @see Builder#maxErrorTokenLength(int) | ||
*/ | ||
public int getMaxErrorTokenLength() { | ||
return _maxErrorTokenLength; | ||
} | ||
|
||
/** | ||
* Accessor for {@link #_maxRawContentLength} | ||
* | ||
* @return Maximum length of token to include in error messages | ||
* @see Builder#maxRawContentLength | ||
*/ | ||
public int getMaxRawContentLength() { | ||
return _maxRawContentLength; | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Convenience methods for validation | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* Convenience method that can be used verify valid {@link #_maxErrorTokenLength}. | ||
* If invalid value is passed in, {@link IllegalArgumentException} is thrown. | ||
* | ||
* @param maxErrorTokenLength Maximum length of token to include in error messages | ||
*/ | ||
private static void validateMaxErrorTokenLength(int maxErrorTokenLength) throws IllegalArgumentException { | ||
if (maxErrorTokenLength < 0) { | ||
throw new IllegalArgumentException( | ||
String.format("Value of maxErrorTokenLength (%d) cannot be negative", maxErrorTokenLength)); | ||
} | ||
} | ||
|
||
private static void validateMaxRawContentLength(int maxRawContentLength) { | ||
if (maxRawContentLength < 0) { | ||
throw new IllegalArgumentException( | ||
String.format("Value of maxRawContentLength (%d) cannot be negative", maxRawContentLength)); | ||
} | ||
} | ||
|
||
} |
128 changes: 128 additions & 0 deletions
128
src/test/java/com/fasterxml/jackson/core/ErrorReportConfigurationTest.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,128 @@ | ||
package com.fasterxml.jackson.core; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for class {@link ErrorReportConfiguration}. | ||
* | ||
* @since 2.16 | ||
*/ | ||
public class ErrorReportConfigurationTest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unit-test. All the tests in the original PR #1043 had |
||
{ | ||
private final ErrorReportConfiguration DEFAULTS = ErrorReportConfiguration.defaults(); | ||
|
||
@Test | ||
public void testZeroLengths() | ||
{ | ||
ErrorReportConfiguration.Builder builder = new ErrorReportConfiguration.Builder(); | ||
builder.maxErrorTokenLength(0); | ||
builder.maxRawContentLength(0); | ||
ErrorReportConfiguration config = builder.build(); | ||
assertEquals(0, config.getMaxErrorTokenLength()); | ||
assertEquals(0, config.getMaxRawContentLength()); | ||
} | ||
|
||
@Test | ||
public void testInvalidMaxErrorTokenLength() | ||
{ | ||
ErrorReportConfiguration.Builder builder = new ErrorReportConfiguration.Builder(); | ||
try { | ||
builder.maxErrorTokenLength(-1); | ||
fail("Should not reach here as exception is expected"); | ||
} catch (IllegalArgumentException ex) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void testMaxRawContentLength() | ||
{ | ||
ErrorReportConfiguration.Builder builder = new ErrorReportConfiguration.Builder(); | ||
int maxLength = 256; | ||
builder.maxRawContentLength(maxLength); | ||
ErrorReportConfiguration config = builder.build(); | ||
assertEquals(maxLength, config.getMaxRawContentLength()); | ||
} | ||
|
||
@Test | ||
public void testInvalidMaxRawContentLength() | ||
{ | ||
ErrorReportConfiguration.Builder builder = new ErrorReportConfiguration.Builder(); | ||
try { | ||
builder.maxRawContentLength(-1); | ||
fail("Should not reach here as exception is expected"); | ||
} catch (IllegalArgumentException ex) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void testDefaults() | ||
{ | ||
// default value | ||
assertEquals(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULTS.getMaxErrorTokenLength()); | ||
assertEquals(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH, DEFAULTS.getMaxRawContentLength()); | ||
|
||
// equals | ||
assertEquals(ErrorReportConfiguration.defaults(), ErrorReportConfiguration.defaults()); | ||
} | ||
|
||
@Test | ||
public void testOverrideDefaultErrorReportConfiguration() | ||
{ | ||
// try with null, no change | ||
ErrorReportConfiguration nullDefaults = ErrorReportConfiguration.defaults(); | ||
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(null); | ||
assertEquals(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH, nullDefaults.getMaxErrorTokenLength()); | ||
assertEquals(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH, nullDefaults.getMaxRawContentLength()); | ||
|
||
// try override defaults | ||
ErrorReportConfiguration overrideDefaults = new ErrorReportConfiguration.Builder() | ||
.maxErrorTokenLength(128) | ||
.maxRawContentLength(256) | ||
.build(); | ||
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(overrideDefaults); | ||
assertEquals(128, overrideDefaults.getMaxErrorTokenLength()); | ||
assertEquals(256, overrideDefaults.getMaxRawContentLength()); | ||
|
||
// IMPORTANT : make sure to revert back, otherwise other tests will be affected | ||
ErrorReportConfiguration.overrideDefaultErrorReportConfiguration(new ErrorReportConfiguration.Builder() | ||
.maxErrorTokenLength(ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH) | ||
.maxRawContentLength(ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH) | ||
.build()); | ||
} | ||
|
||
@Test | ||
public void testRebuild() | ||
{ | ||
ErrorReportConfiguration config = new ErrorReportConfiguration.Builder().build(); | ||
ErrorReportConfiguration rebuiltConfig = config.rebuild().build(); | ||
assertEquals(config.getMaxErrorTokenLength(), rebuiltConfig.getMaxErrorTokenLength()); | ||
assertEquals(config.getMaxRawContentLength(), rebuiltConfig.getMaxRawContentLength()); | ||
} | ||
|
||
|
||
@Test | ||
public void testBuilderConstructorWithTwoParams() | ||
{ | ||
ErrorReportConfiguration.Builder builder = new ErrorReportConfiguration.Builder(128, 256); | ||
ErrorReportConfiguration config = builder.build(); | ||
assertEquals(128, config.getMaxErrorTokenLength()); | ||
assertEquals(256, config.getMaxRawContentLength()); | ||
} | ||
|
||
@Test | ||
public void testBuilderConstructorWithErrorReportConfiguration() | ||
{ | ||
ErrorReportConfiguration newConfig = new ErrorReportConfiguration.Builder() | ||
.maxErrorTokenLength(128) | ||
.maxRawContentLength(256) | ||
.build(); | ||
ErrorReportConfiguration.Builder builder = new ErrorReportConfiguration.Builder(newConfig); | ||
ErrorReportConfiguration config = builder.build(); | ||
assertEquals(newConfig.getMaxErrorTokenLength(), config.getMaxErrorTokenLength()); | ||
assertEquals(newConfig.getMaxRawContentLength(), config.getMaxRawContentLength()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you copy Javadoc from matching method in
StreamWriteConstaints
, warning against use of this method?In general it should not be used and specifically libraries should not change global defaults, ever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or actually I can do this.