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

Add methods for detecting retriable and fatal Ably exceptions #929

Merged
merged 3 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions common/src/main/java/com/ably/tracking/common/AblyHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,21 @@ private fun TokenAuthException.toAblyException(): AblyException =
is CouldNotFetchTokenException ->
AblyException.fromErrorInfo(ErrorInfo(message, 401, 100_002))
}

/**
* Indicates whether the exception from Ably is fatal and we should not attempt to retry it.
* Fatal errors have codes like 4xx (e.g. 400).
*/
fun ConnectionException.isFatal(): Boolean {
val firstErrorCodeDigit = errorInformation.code.toString().first()
paddybyers marked this conversation as resolved.
Show resolved Hide resolved
return firstErrorCodeDigit == '4'
}

/**
* Indicates whether the exception from Ably is retriable and we can attempt to retry it.
* Non-fatal errors have codes like 5xx (e.g. 500).
*/
fun ConnectionException.isRetriable(): Boolean {
val firstErrorCodeDigit = errorInformation.code.toString().first()
return firstErrorCodeDigit == '5'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.ably.tracking.common

import com.ably.tracking.ConnectionException
import com.ably.tracking.ErrorInformation
import org.junit.Assert
import org.junit.Test

class ConnectionExceptionTests {
@Test
fun `exception with error code starting with 4 should be treated as fatal`() {
// given
val exception = createConnectionException(400, 40000, "Fatal error")

// when
val result = exception.isFatal()

// then
Assert.assertTrue(result)
}

@Test
fun `exception with error code starting with 4 should not be treated as retriable`() {
// given
val exception = createConnectionException(400, 40000, "Fatal error")

// when
val result = exception.isRetriable()

// then
Assert.assertFalse(result)
}

@Test
fun `exception with error code starting with 5 should not be treated as fatal`() {
// given
val exception = createConnectionException(500, 50000, "Retriable error")

// when
val result = exception.isFatal()

// then
Assert.assertFalse(result)
}

@Test
fun `exception with error code starting with 5 should be treated as retriable`() {
// given
val exception = createConnectionException(500, 50000, "Retriable error")

// when
val result = exception.isRetriable()

// then
Assert.assertTrue(result)
}

private fun createConnectionException(code: Int, statusCode: Int, message: String): ConnectionException =
ConnectionException(ErrorInformation(code, statusCode, message, null, null))
}