-
Notifications
You must be signed in to change notification settings - Fork 12
feat(internal): add FailureClass enum and classifier helpers to HttpErrors (SDK-2789) #204
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/http/FailureClass.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.launchdarkly.sdk.internal.http; | ||
|
|
||
| import javax.net.ssl.SSLException; | ||
|
|
||
| import java.security.GeneralSecurityException; | ||
|
|
||
| /** | ||
| * Classifies a failure into one of two regimes: {@link #NORMAL} or | ||
| * {@link #UNEXPECTED}. Used by data sources and other network-facing components | ||
| * to decide whether a failure should trigger extended-regime backoff. | ||
| * <p> | ||
| * This class is for internal use only and should not be documented in the SDK API. | ||
| * It is not supported for any use outside of the LaunchDarkly SDKs, and is subject | ||
| * to change without notice. | ||
| */ | ||
| public enum FailureClass { | ||
| /** | ||
| * Ordinary transient failure. Use the normal-regime backoff. Includes HTTP | ||
| * 400 / 408 / 429, HTTP 5xx, any other HTTP status the SDK treats as a | ||
| * failure, and generic transport failures (connection refused, read timeout, | ||
| * DNS failure, etc.). | ||
| */ | ||
| NORMAL, | ||
|
|
||
| /** | ||
| * Unexpected failure indicative of a longer-lived condition. Use the | ||
| * extended-regime backoff. Includes HTTP 401 / 403 and any other 4xx not in | ||
| * the NORMAL list, plus TLS / certificate validation failures. | ||
| */ | ||
| UNEXPECTED; | ||
|
|
||
| /** | ||
| * Scans an exception chain for TLS / certificate validation causes. | ||
| */ | ||
| static boolean hasTlsOrCertificateCause(Throwable t) { | ||
| for (Throwable c = t; c != null; c = c.getCause()) { | ||
| if (c instanceof SSLException | ||
| || c instanceof GeneralSecurityException) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ternal/src/test/java/com/launchdarkly/sdk/internal/http/HttpErrorsClassificationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package com.launchdarkly.sdk.internal.http; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import javax.net.ssl.SSLHandshakeException; | ||
| import javax.net.ssl.SSLPeerUnverifiedException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.ConnectException; | ||
| import java.net.SocketTimeoutException; | ||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.CertificateExpiredException; | ||
|
|
||
| import static com.launchdarkly.sdk.internal.http.FailureClass.NORMAL; | ||
| import static com.launchdarkly.sdk.internal.http.FailureClass.UNEXPECTED; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Unit coverage for {@link HttpErrors#classifyHttpFailure(int)} and | ||
| * {@link HttpErrors#classifyTransportFailure(Throwable)}. | ||
| */ | ||
| @SuppressWarnings("javadoc") | ||
| public class HttpErrorsClassificationTest { | ||
|
|
||
| // 400, 408, 429 are NORMAL. | ||
| @Test public void http400IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(400)); } | ||
| @Test public void http408IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(408)); } | ||
| @Test public void http429IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(429)); } | ||
|
|
||
| // Other 4xx (including 401, 403) is UNEXPECTED. | ||
| @Test public void http401IsUnexpected() { assertEquals(UNEXPECTED, HttpErrors.classifyHttpFailure(401)); } | ||
| @Test public void http403IsUnexpected() { assertEquals(UNEXPECTED, HttpErrors.classifyHttpFailure(403)); } | ||
| @Test public void http404IsUnexpected() { assertEquals(UNEXPECTED, HttpErrors.classifyHttpFailure(404)); } | ||
| @Test public void http418IsUnexpected() { assertEquals(UNEXPECTED, HttpErrors.classifyHttpFailure(418)); } | ||
| @Test public void http451IsUnexpected() { assertEquals(UNEXPECTED, HttpErrors.classifyHttpFailure(451)); } | ||
|
|
||
| // 5xx is NORMAL. | ||
| @Test public void http500IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(500)); } | ||
| @Test public void http502IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(502)); } | ||
| @Test public void http503IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(503)); } | ||
| @Test public void http504IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(504)); } | ||
| @Test public void http599IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(599)); } | ||
|
|
||
| // Unusual non-4xx / non-5xx failure statuses are NORMAL. | ||
| @Test public void http300IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(300)); } | ||
| @Test public void http0IsNormal() { assertEquals(NORMAL, HttpErrors.classifyHttpFailure(0)); } | ||
|
|
||
| // Ordinary network I/O failures are NORMAL. | ||
| @Test public void connectExceptionIsNormal() { | ||
| assertEquals(NORMAL, HttpErrors.classifyTransportFailure(new ConnectException("connection refused"))); | ||
| } | ||
| @Test public void socketTimeoutIsNormal() { | ||
| assertEquals(NORMAL, HttpErrors.classifyTransportFailure(new SocketTimeoutException("timeout"))); | ||
| } | ||
| @Test public void ioExceptionIsNormal() { | ||
| assertEquals(NORMAL, HttpErrors.classifyTransportFailure(new IOException("something else"))); | ||
| } | ||
|
|
||
| // TLS / certificate validation failures are UNEXPECTED. | ||
| @Test public void sslHandshakeIsUnexpected() { | ||
| assertEquals(UNEXPECTED, HttpErrors.classifyTransportFailure(new SSLHandshakeException("handshake failed"))); | ||
| } | ||
| @Test public void sslPeerUnverifiedIsUnexpected() { | ||
| assertEquals(UNEXPECTED, HttpErrors.classifyTransportFailure(new SSLPeerUnverifiedException("peer not verified"))); | ||
| } | ||
| @Test public void certificateExceptionIsUnexpected() { | ||
| assertEquals(UNEXPECTED, HttpErrors.classifyTransportFailure(new CertificateException("cert invalid"))); | ||
| } | ||
| @Test public void certificateExpiredIsUnexpected() { | ||
| assertEquals(UNEXPECTED, HttpErrors.classifyTransportFailure(new CertificateExpiredException("expired"))); | ||
| } | ||
|
|
||
| // Cause-chain walk finds TLS deep in wrapper exceptions. | ||
| @Test public void sslCauseWrappedIsUnexpected() { | ||
| IOException wrapper = new IOException("wrapped", new SSLHandshakeException("real cause")); | ||
| assertEquals(UNEXPECTED, HttpErrors.classifyTransportFailure(wrapper)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.