Skip to content

Commit 071798f

Browse files
authored
feat: add support for determining HTTP exception retryability (#82)
1 parent 208b9e8 commit 071798f

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

aws-crt-kotlin/api/android/aws-crt-kotlin.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public final class aws/sdk/kotlin/crt/CRT {
88
public final fun errorString (I)Ljava/lang/String;
99
public final fun initRuntime (Lkotlin/jvm/functions/Function1;)V
1010
public static synthetic fun initRuntime$default (Laws/sdk/kotlin/crt/CRT;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
11+
public final fun isHttpErrorRetryable (I)Z
1112
public final fun lastError ()I
1213
public final fun nativeMemory ()J
1314
}

aws-crt-kotlin/api/jvm/aws-crt-kotlin.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public final class aws/sdk/kotlin/crt/CRT {
88
public final fun errorString (I)Ljava/lang/String;
99
public final fun initRuntime (Lkotlin/jvm/functions/Function1;)V
1010
public static synthetic fun initRuntime$default (Laws/sdk/kotlin/crt/CRT;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
11+
public final fun isHttpErrorRetryable (I)Z
1112
public final fun lastError ()I
1213
public final fun nativeMemory ()J
1314
}

aws-crt-kotlin/common/src/aws/sdk/kotlin/crt/CRT.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@ public expect object CRT {
1818
public fun lastError(): Int
1919

2020
/**
21-
* Given an integer error code from an internal operation
21+
* Given an integer error code from an internal operation, return the associated error message.
2222
* @param errorCode An error code returned from an exception or other native function call
2323
* @return A user-friendly description of the error
2424
*/
2525
public fun errorString(errorCode: Int): String?
2626

2727
/**
28-
* Given an integer error code from an internal operation
28+
* Given an integer error code from an internal operation, return the associated error name.
2929
*
3030
* @param errorCode An error code returned from an exception or other native
3131
* function call
3232
* @return A string identifier for the error
3333
*/
3434
public fun errorName(errorCode: Int): String?
3535

36+
/**
37+
* Given an integer HTTP error code from an internal operation, return whether the error is retryable.
38+
* @param errorCode An error code returned from an exception or other native function call
39+
* @return True if the given HTTP error is retryable; otherwise, false.
40+
*/
41+
public fun isHttpErrorRetryable(errorCode: Int): Boolean
42+
3643
/**
3744
* @return The number of bytes allocated in native resources. If aws.crt.memory.tracing is 1 or 2, this will
3845
* be a non-zero value. Otherwise, no tracing will be done, and the value will always be 0

aws-crt-kotlin/jvm/src/aws/sdk/kotlin/crt/CRT.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package aws.sdk.kotlin.crt
77

88
import software.amazon.awssdk.crt.Log
9+
import software.amazon.awssdk.crt.http.HttpClientConnection
10+
import software.amazon.awssdk.crt.http.HttpException
911
import java.util.concurrent.atomic.AtomicInteger as AtomicInt
1012
import software.amazon.awssdk.crt.CRT as crtJni
1113

@@ -53,6 +55,17 @@ public actual object CRT {
5355
*/
5456
public actual fun errorName(errorCode: Int): String? = crtJni.awsErrorName(errorCode)
5557

58+
public actual fun isHttpErrorRetryable(errorCode: Int): Boolean {
59+
// An exception subtype that doesn't create a stack, which saves a bunch of time
60+
class StacklessHttpException(errorCode: Int) : HttpException(errorCode) {
61+
// Changing `fillInStackTrace` to a no-op skips filling in the stack
62+
override fun fillInStackTrace(): Throwable = this
63+
}
64+
65+
val phonyException = StacklessHttpException(errorCode)
66+
return HttpClientConnection.isErrorRetryable(phonyException)
67+
}
68+
5669
/**
5770
* @return The number of bytes allocated in native resources. If aws.crt.memory.tracing is 1 or 2, this will
5871
* be a non-zero value. Otherwise, no tracing will be done, and the value will always be 0

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/CRTNative.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ public actual object CRT {
3434
TODO("Not yet implemented")
3535
}
3636

37+
public actual fun isHttpErrorRetryable(errorCode: Int): Boolean {
38+
TODO("Not yet implemented")
39+
}
40+
3741
/**
3842
* @return The number of bytes allocated in native resources. If aws.crt.memory.tracing is 1 or 2, this will
3943
* be a non-zero value. Otherwise, no tracing will be done, and the value will always be 0
4044
*/
4145
public actual fun nativeMemory(): Long {
4246
TODO("Not yet implemented")
4347
}
44-
}
48+
}

0 commit comments

Comments
 (0)