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

Java: throw a more appropriate exception. #199

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/xdrgen/generators/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def render_enum(enum, out)
end
out.puts <<-EOS.strip_heredoc
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/xdrgen/generators/java/XdrString.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class XdrString implements XdrElement {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
Copy link
Member

@leighmcculloch leighmcculloch Jul 10, 2024

Choose a reason for hiding this comment

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

Because it's the size inside the stream, and not the maxSize variable, that is illegal, I'm not sure I'd class this as an illegal argument exception (IAE) because the arguments are actually okay it's an issue with data available on the stream and not the function arguments.

BufferOverflowException (BOE) might be more appropriate?

Happy to merge this if you think IAE is the most appropriate exception over BOE. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @leighmcculloch, BOE is also a suitable choice, but I still prefer to use IAE here.

  • In the given code, the exception is triggered when one parameter (size) exceeds another parameter (maxSize). This aligns well with the definition of IAE, which is used when a method receives an inappropriate or invalid argument. In this case, the size being greater than maxSize is due to an incorrect size parameter contained within the passed-in stream.
  • IAE is a very common and familiar exception type in Java, widely used to represent various invalid arguments or inappropriate conditions. By using IAE, other developers can quickly understand and handle the exception.

}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static AccountFlags decode(XdrDataInputStream stream) throws IOException
switch (value) {
case 1: return AUTH_REQUIRED_FLAG;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/const.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Color decode(XdrDataInputStream stream) throws IOException {
case 1: return GREEN;
case 2: return BLUE;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/Color2.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Color2 decode(XdrDataInputStream stream) throws IOException {
case 1: return GREEN2;
case 2: return BLUE2;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static MessageType decode(XdrDataInputStream stream) throws IOException {
case 12: return FBA_QUORUMSET;
case 13: return FBA_MESSAGE;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/nesting.x/UnionKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static UnionKey decode(XdrDataInputStream stream) throws IOException {
case 2: return TWO;
case 3: return OFFER;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/nesting.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/optional.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/struct.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Color decode(XdrDataInputStream stream) throws IOException {
case 5: return BLUE;
case 6: return GREEN;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Nester.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static NestedEnum decode(XdrDataInputStream stream) throws IOException {
case 0: return BLAH_1;
case 1: return BLAH_2;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/union.x/UnionKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static UnionKey decode(XdrDataInputStream stream) throws IOException {
case 0: return ERROR;
case 1: return MULTI;
default:
throw new RuntimeException("Unknown enum value: " + value);
throw new IllegalArgumentException("Unknown enum value: " + value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/union.x/XdrString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
Expand Down
Loading