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

More Error Info #683

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 55 additions & 0 deletions src/main/java/software/amazon/awssdk/crt/CrtErrorInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
package software.amazon.awssdk.crt;

import static software.amazon.awssdk.crt.CRT.awsErrorName;
import static software.amazon.awssdk.crt.CRT.awsErrorString;

/**
* This enum contains errors that need to be explicitly handled on the Java side of the JNI boundary.
* Any time someone needs to respond to specific errorCodes, they should be added here
* , so they'll at least
* be in a central place.
*/
public enum CrtErrorInfo {
Success(0),
TLSNegotiationFailure(1029),
Copy link
Contributor

Choose a reason for hiding this comment

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

We should code-gen these!

Waqar took a swing at doing this for aws-crt-swift a few months back. It didn't get merged for various reasons, but I'd love to take the cod-gen approach for errors in all CRTs:

Here's Waqar's code-genned enum list:
https://github.com/awslabs/aws-crt-swift/blob/fix-error-handling-2/Source/AwsCommonRuntimeKit/crt/CRTErrorGenerated.swift

Here's his code-gen script:
https://github.com/awslabs/aws-crt-swift/blob/fix-error-handling-2/Source/Script/CRTErrorGenerator.swift

We could have a CI step that confirms it's up to date (run script, fail if file changes)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deferring this convo to a later time. For now I can get by with the errir code being made public on the exception class

/**
* We didn't know what it was, and need a way to know that.
*/
UnKnownError(Integer.MAX_VALUE);
waahm7 marked this conversation as resolved.
Show resolved Hide resolved

private int errorCode = 0;

CrtErrorInfo(int errorCode) {
this.errorCode = errorCode;
}

public int getErrorCode() {
return errorCode;
}

public String getAwsErrorString() {
return awsErrorString(errorCode);
}

public String getAwsErrorName() {
return awsErrorName(errorCode);
}

public boolean isModeledError() {
return errorCode != UnKnownError.getErrorCode();
}

public static CrtErrorInfo fromErrorCode(int errorCode) {
for(CrtErrorInfo error : CrtErrorInfo.values()) {
if (error.getErrorCode() == errorCode) {
return error;
}
}

return UnKnownError;
}
}
21 changes: 18 additions & 3 deletions src/main/java/software/amazon/awssdk/crt/http/HttpException.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
package software.amazon.awssdk.crt.http;

import software.amazon.awssdk.crt.CRT;
import software.amazon.awssdk.crt.CrtErrorInfo;

/**
* This exception will be thrown by any exceptional cases encountered within the
* JNI bindings to the AWS Common Runtime
*/
public class HttpException extends RuntimeException {
Copy link
Contributor

Choose a reason for hiding this comment

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

My real dream for aws-crt-java would be to code-gen actual exception classes (not just enums) so Java programmers could do the idiomatic thing and:

catch (TlsNegotiationFailure e) {
    ...special...
}
catch (Exception e) {
    ...everything else...
}

instead of:

catch (HttpException e) {
    switch(e.getErrorInfo()) {
        case CrtErrorInfo.TLSNegotiationFailure:
            ...special...
            break;
        default:
            ...everything else (block 1)...
            break;
    }
}
catch (Exception e) {
    ...everything else (block 2)...
}

But that would technically be a breaking change, since we'd be changing the exact exception classes that get raised 😓

Would it be worth breaking those eggs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deferring this convo to a later time. For now I can get by with the errir code being made public on the exception class

private int errorCode;
private final int errorCode;

/**
* Constructs a new HttpException
Expand All @@ -24,11 +25,25 @@ public HttpException(int errorCode) {
}

/**
* Returns the error code captured when the exception occurred. This can be fed to {@link CRT.awsErrorString} to
* Returns the error code captured when the exception occurred. This can be fed to {@link CRT.awsErrorName()} to
* get a user-friendly error string
* @return The error code associated with this exception
*/
int getErrorCode() {
public int getErrorCode() {
return errorCode;
}

/**
* Returns more detailed error info if it has been modeled.
*/
public CrtErrorInfo getErrorInfo() {
return CrtErrorInfo.fromErrorCode(errorCode);
}

/**
* Returns true if this exception is retryable.
*/
public boolean isRetryable() {
return HttpClientConnection.isErrorRetryable(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ public void benchmarkS3Get() {
// resolution
final int vipsNeeded = (int) Math.ceil(expectedGbps / 0.5 / 10);
final int sampleDelay = Integer.parseInt(System.getProperty("aws.crt.s3.benchmark.warmup",
new Integer((int) Math.ceil(vipsNeeded / 5)).toString()));
Integer.toString((int) Math.ceil(vipsNeeded / 5))));
System.out.println(String.format("REGION=%s, WARMUP=%s", region, sampleDelay));

// Ignore stats during warm up time, they skew results
Expand Down Expand Up @@ -1402,7 +1402,7 @@ public void benchmarkS3Put() {
// resolution
final int vipsNeeded = (int) Math.ceil(expectedGbps / 0.5 / 10);
final int sampleDelay = Integer.parseInt(System.getProperty("aws.crt.s3.benchmark.warmup",
new Integer((int) Math.ceil(vipsNeeded / 5)).toString()));
Integer.toString((int) Math.ceil(vipsNeeded / 5))));
System.out.println(String.format("REGION=%s, WARMUP=%s", region, sampleDelay));

// Ignore stats during warm up time, they skew results
Expand Down
Loading