-
Notifications
You must be signed in to change notification settings - Fork 163
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 error handling and logging #1311
Changes from all commits
08cede7
d7e9ffb
8e6ffc3
9df62a1
19e5cd1
808d796
5a81bbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,8 +158,7 @@ | |
byte[] imageBytes = baos.toByteArray(); | ||
return "data:" + imageType.mimeType + ";base64," + Base64.getEncoder().encodeToString(imageBytes); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return null; | ||
throw new RuntimeException("Failed to generate image %s of size %sx%s".formatted(imageType, width, height), e); | ||
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. I don't see why you'd want to throw an exception here. 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. Because it's much better to clearly show the problem instead of hiding it.
assertThat(faker.image().base64TIFF()).startsWith("data:image/tiff;base64,");
String image = faker.image().base64TIFF();
if (image == null) {
// WHAT SHOULD I DO HERE?..
throw new RuntimeException("WTF, the image is null, and I have no idea why.");
} Here is more about "Throw Early, Catch Late" Principle 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. How is writing that if(null) check different from:
I don't mind throwing exceptions early if the code is in my control, but this is a library, and how would a user know this code can throw an exception? The implicit contract in DF is that none of the methods will ever throw an exception,this changes that approach. 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. It differs a lot.
It sounds very strange to me. Anyway, in this case, such an exception actually cannot happen. We need to catch |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,41 +246,29 @@ | |
* @return a correctly formatted IPv4 address. | ||
*/ | ||
public String ipV4Address() { | ||
try { | ||
return getIpV4Address().getHostAddress(); | ||
} catch (UnknownHostException e) { | ||
return "127.0.0.1"; | ||
} | ||
return getIpV4Address().getHostAddress(); | ||
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. I don't think this is ideal, I believe the code can throw exceptions now sometimes, and I don't think we should. 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. I also had a doubt, but no, this code doesn't fail. This is a test that tries through all possible values: @Test
void allPossibleAddresses() {
for (int first = 2; first < 256; first++) {
for (int second = 2; second < 256; second++) {
for (int third = 2; third < 256; third++) {
for (int fourth = 2; fourth < 256; fourth++) {
InetAddress inetAddress = inet4Address((byte) first, (byte) second, (byte) third, (byte) fourth);
assertThat(inetAddress).isNotNull();
}
}
}
}
} |
||
} | ||
|
||
/** | ||
* returns an IPv4 address. | ||
* | ||
* @return an IPv4 address. | ||
*/ | ||
public InetAddress getIpV4Address() throws UnknownHostException { | ||
return Inet4Address.getByAddress(new byte[]{ | ||
(byte) (faker.random().nextInt(254) + 2), | ||
(byte) (faker.random().nextInt(254) + 2), | ||
(byte) (faker.random().nextInt(254) + 2), | ||
(byte) (faker.random().nextInt(254) + 2)}); | ||
public InetAddress getIpV4Address() { | ||
return inet4Address((byte) (faker.random().nextInt(254) + 2), (byte) (faker.random().nextInt(254) + 2), (byte) (faker.random().nextInt(254) + 2), (byte) (faker.random().nextInt(254) + 2)); | ||
} | ||
|
||
/** | ||
* @return a valid private IPV4 address in dot notation | ||
*/ | ||
public String privateIpV4Address() { | ||
try { | ||
return getPrivateIpV4Address().getHostAddress(); | ||
} catch (UnknownHostException e) { | ||
return "127.0.0.1"; | ||
} | ||
return getPrivateIpV4Address().getHostAddress(); | ||
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. again, I don't think this is correct. 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. same as above: it's correct :) |
||
} | ||
|
||
/** | ||
* @return a private IPV4 address | ||
*/ | ||
public InetAddress getPrivateIpV4Address() throws UnknownHostException { | ||
public InetAddress getPrivateIpV4Address() { | ||
final Byte[] PRIVATE_FIRST_OCTET = {10, 127, (byte) 169, (byte) 192, (byte) 172}; | ||
final Byte[] PRIVATE_SECOND_OCTET_172 = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; | ||
|
||
|
@@ -295,24 +283,20 @@ | |
case (byte) 192 -> second = (byte) 168; | ||
case (byte) 169 -> second = (byte) 254; | ||
} | ||
return Inet4Address.getByAddress(new byte[]{first, second, third, fourth}); | ||
return inet4Address(first, second, third, fourth); | ||
} | ||
|
||
/** | ||
* @return a valid public IPV4 address in dot notation | ||
*/ | ||
public String publicIpV4Address() { | ||
try { | ||
return getPublicIpV4Address().getHostAddress(); | ||
} catch (UnknownHostException e) { | ||
return "127.0.0.1"; | ||
} | ||
return getPublicIpV4Address().getHostAddress(); | ||
} | ||
|
||
/** | ||
* @return a valid public IPV4 address | ||
*/ | ||
public InetAddress getPublicIpV4Address() throws UnknownHostException { | ||
public InetAddress getPublicIpV4Address() { | ||
final RandomService r = faker.random(); | ||
|
||
final byte[] PRIVATE_FIRST_OCTET = {10, 127, (byte) 169, (byte) 192, (byte) 172}; | ||
|
@@ -325,7 +309,7 @@ | |
while (Arrays.binarySearch(PRIVATE_FIRST_OCTET, first) > 0) { | ||
first = (byte) r.nextInt(256); | ||
} | ||
return Inet4Address.getByAddress(new byte[]{first, second, third, fourth}); | ||
return inet4Address(first, second, third, fourth); | ||
} | ||
|
||
/** | ||
|
@@ -343,19 +327,15 @@ | |
* @return a correctly formatted IPv6 address. | ||
*/ | ||
public String ipV6Address() { | ||
try { | ||
return getIpV6Address().getHostAddress(); | ||
} catch (UnknownHostException e) { | ||
return "0:0:0:0:0:0:0:1"; | ||
} | ||
return getIpV6Address().getHostAddress(); | ||
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. same as above. 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. same as above. :) |
||
} | ||
|
||
/** | ||
* <p>Returns an IPv6 address in hh:hh:hh:hh:hh:hh:hh:hh format.</p> | ||
* | ||
* @return a IPV6 address. | ||
*/ | ||
public InetAddress getIpV6Address() throws UnknownHostException { | ||
public InetAddress getIpV6Address() { | ||
final RandomService random = faker.random(); | ||
final char[] res = new char[4 * 8 + 7]; | ||
for (int i = 0; i < 8; i++) { | ||
|
@@ -366,7 +346,7 @@ | |
char[] hex = random.hex(4, false).toCharArray(); | ||
System.arraycopy(hex, 0, res, i + offset, hex.length); | ||
} | ||
return Inet6Address.getByName(String.valueOf(res)); | ||
return inet6Address(String.valueOf(res)); | ||
} | ||
|
||
/** | ||
|
@@ -550,4 +530,20 @@ | |
return browserName; | ||
} | ||
} | ||
|
||
private static InetAddress inet4Address(byte first, byte second, byte third, byte fourth) { | ||
try { | ||
return Inet4Address.getByAddress(new byte[]{first, second, third, fourth}); | ||
} catch (UnknownHostException e) { | ||
throw new RuntimeException("Failed to create Inet4Address from %s %s %s %s".formatted(first, second, third, fourth), e); | ||
} | ||
} | ||
|
||
private static InetAddress inet6Address(String host) { | ||
try { | ||
return Inet6Address.getByName(host); | ||
} catch (UnknownHostException e) { | ||
throw new RuntimeException("Failed to create Inet6Address from host '%s'".formatted(host), e); | ||
} | ||
} | ||
} |
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.
isn't this the default toString?
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.
Almost. Except that it's shorter due to
getSimpleName
instead ofgetName
.I would like to make it even more shorter, but it doesn't have any human-readable fields...
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.
but why do you need it?
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.
In debug logs.
To make output of such logs a bit better readable (than the default):