Skip to content
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

Improve Exception messages when working on "leader" #573

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public final class Marc21Encoder extends
private State state = State.IN_STREAM;

private boolean generateIdField;
private boolean validateLeader = true;

/**
* Initializes the encoder with MARC 21 constants and charset.
Expand Down Expand Up @@ -108,6 +109,18 @@ public void setGenerateIdField(final boolean generateIdField) {
this.generateIdField = generateIdField;
}

/**
* Controls whether the leader should be validated.
* <p>
* The default value of {@code validateLeader} is true.
* <p>
*
* @param validateLeader if false the leader is not validated
*/
public void setValidateLeader(final boolean validateLeader) {
this.validateLeader = validateLeader;
}

/**
* Gets the flag to decide whether the ID field is generated.
*
Expand Down Expand Up @@ -214,7 +227,7 @@ private void processLeaderAsOneLiteral(final String value) {
private void processLeaderAsSubfields(final String name, final String value) {
if (value.length() != 1) {
throw new FormatException(
"literal must only contain a single character:" + name);
"leader literal must only contain a single character:" + name);
}
processLeaderAsSubfields(name, value.charAt(0));
}
Expand Down Expand Up @@ -259,12 +272,14 @@ private void processLeaderAsSubfields(final String name, final char code) {
}

private void requireValidCode(final char code, final char[] validCodes) {
for (final char validCode: validCodes) {
if (validCode == code) {
return;
if (validateLeader) {
for (final char validCode : validCodes) {
if (validCode == code) {
return;
}
}
throw new FormatException("invalid code in leader'" + code + "'; allowed codes are: " + Arrays.toString(validCodes));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dr0i is it possible to add the info about the leader element? like this:

Suggested change
throw new FormatException("invalid code in leader'" + code + "'; allowed codes are: " + Arrays.toString(validCodes));
throw new FormatException("invalid code in leader literal literal'" + name + code + "'; allowed codes are: " + Arrays.toString(validCodes));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've proposed to add literal literal as message. And a variable name which is not declared in the method nor globally (meaning: you cannot use it here).
What you probably meant to output was something like e.g. TYPE_OF_CONTROL_CODES or ENCODING_LEVEL_CODES so that a librarian knows what leader position is wrong? We could do this, but have to pass this information somehow into the method via a parameter.

}
throw new FormatException("invalid code '" + code + "'; allowed codes are: " + Arrays.toString(validCodes));
}

private void processTopLevelLiteral(final String name, final String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
*/
public final class Marc21EncoderTest {

private static final String BAD_LEADER = "00600ny a22002053n 4500";

private Marc21Encoder marc21Encoder;

@Mock
Expand Down Expand Up @@ -147,4 +149,21 @@ public void issue524ShouldComputeValidLeader() {
verify(receiver).process(matches("00055pam a2200037 c 4500021001700000\u001e.*\u001d"));
}

@Test(expected = FormatException.class)
public void issue567ShouldFailValidateLeaderAsDefault() {
marc21Encoder.startRecord("");
marc21Encoder.literal(LEADER_ENTITY, BAD_LEADER);
marc21Encoder.endRecord();
}

@Test
public void issue567ShouldNotValidateLeader() {
marc21Encoder.setValidateLeader(false);
marc21Encoder.startRecord("");
marc21Encoder.literal(LEADER_ENTITY, BAD_LEADER );
marc21Encoder.endRecord();

verify(receiver).process(matches("00026ny a22000253n 4500\u001e\u001d"));
}

}
Loading