Skip to content

Commit 60960f5

Browse files
committed
Rename StreamError to Error
1 parent 7c50b90 commit 60960f5

File tree

15 files changed

+152
-152
lines changed

15 files changed

+152
-152
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ dependencies {
5252
This is a basic model to represent a normalized result from business work. This looks similar to [Kotlin's Result](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/), but Stream Result was designed to include more information about success and error and support more convenient functionalities to handle results. Result is basically consist of two detailed types below:
5353

5454
- **Result.Success**: This represents your business's successful result, including a `value` property, a generic type of Result.
55-
- **Result.Failure**: This represents the failed result of your business result and includes a `value` property, the `StreamError` type.
55+
- **Result.Failure**: This represents the failed result of your business result and includes a `value` property, the `Error` type.
5656

5757
You can simply create each instance of `Result` like the example below:
5858

5959
```kotlin
6060
val result0: Result<String> = Result.Success(value = "result")
6161

6262
val result1: Result<String> = Result.Failure(
63-
value = StreamError.GenericError(message = "failure")
63+
value = Error.GenericError(message = "failure")
6464
)
6565

6666
val result = result0 then { result1 }
@@ -71,31 +71,31 @@ result.onSuccess {
7171
}
7272
```
7373

74-
## StreamError
74+
## Error
7575

76-
`Result.Failure` has `StreamError` as a value property, which contains error details of your business work. Basically, `StreamError` consists of three different types of errors below:
76+
`Result.Failure` has `Error` as a value property, which contains error details of your business work. Basically, `Error` consists of three different types of errors below:
7777

78-
- **StreamError.GenericError**: Represents a normal type of error and only contains an error message.
79-
- **StreamError.ThrowableError**: Represents an exceptional type of error and contains a message and cause information.
80-
- **StreamError.NetworkError**: Represents a network error and contains status code, message, and cause information.
78+
- **Error.GenericError**: Represents a normal type of error and only contains an error message.
79+
- **Error.ThrowableError**: Represents an exceptional type of error and contains a message and cause information.
80+
- **Error.NetworkError**: Represents a network error and contains status code, message, and cause information.
8181

8282
You can create each instance like the example below:
8383

8484
```kotlin
85-
val streamError: StreamError = StreamError.GenericError(message = "error")
85+
val error: Error = Error.GenericError(message = "error")
8686

8787
try {
8888
..
8989
} catch (e: Exception) {
90-
val streamError: StreamError = StreamError.ThrowableError(
90+
val error: Error = Error.ThrowableError(
9191
message = e.localizedMessage ?: e.stackTraceToString(),
9292
cause = e
9393
)
9494
}
9595

96-
val streamError: StreamError = StreamError.NetworkError(
96+
val error: Error = Error.NetworkError(
9797
message = "error",
98-
streamCode = code,
98+
serverErrorCode = code,
9999
statusCode = statusCode
100100
)
101101
```
@@ -268,9 +268,9 @@ Retry a network request following your `RetryPolicy`.
268268

269269
```kotlin
270270
private val retryPolicy = object : RetryPolicy {
271-
override fun shouldRetry(attempt: Int, error: StreamError): Boolean = attempt <= 3
271+
override fun shouldRetry(attempt: Int, error: Error): Boolean = attempt <= 3
272272

273-
override fun retryTimeout(attempt: Int, error: StreamError): Int = 3000
273+
override fun retryTimeout(attempt: Int, error: Error): Int = 3000
274274
}
275275

276276

@@ -282,7 +282,7 @@ val result = posterService.fetchPosterList()
282282

283283
### Custom Error Parser
284284

285-
You can customize the creating of `StreamError` from an error response according to your backend service by implementing your `ErrorParser` class. You can provide your custom `ErrorParser` to `RetrofitCallAdapterFactory`. If not, it will use a default `ErrorParser`, which uses [Kotlin Serialization](https://kotlinlang.org/docs/serialization.html) to decode json formats.
285+
You can customize the creating of `Error` from an error response according to your backend service by implementing your `ErrorParser` class. You can provide your custom `ErrorParser` to `RetrofitCallAdapterFactory`. If not, it will use a default `ErrorParser`, which uses [Kotlin Serialization](https://kotlinlang.org/docs/serialization.html) to decode json formats.
286286

287287
```kotlin
288288
internal class MyErrorParser : ErrorParser<DefaultErrorResponse> {
@@ -292,12 +292,12 @@ internal class MyErrorParser : ErrorParser<DefaultErrorResponse> {
292292
// use moshi or something that you can serialize from json response.
293293
}
294294

295-
override fun toError(okHttpResponse: Response): StreamError {
296-
// build StreamError with a given okHttpResponse.
295+
override fun toError(okHttpResponse: Response): Error {
296+
// build Error with a given okHttpResponse.
297297
}
298298

299-
override fun toError(errorResponseBody: ResponseBody): StreamError {
300-
// build StreamError with a given errorResponseBody.
299+
override fun toError(errorResponseBody: ResponseBody): Error {
300+
// build Error with a given errorResponseBody.
301301
}
302302
}
303303

app/src/main/kotlin/io/getstream/resultdemo/MainViewModel.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import androidx.lifecycle.ViewModel
1919
import androidx.lifecycle.viewModelScope
2020
import io.getstream.log.StreamLog
2121
import io.getstream.log.streamLog
22-
import io.getstream.result.StreamError
22+
import io.getstream.result.Error
2323
import io.getstream.result.call.doOnResult
2424
import io.getstream.result.call.doOnStart
2525
import io.getstream.result.call.map
@@ -60,8 +60,8 @@ public class MainViewModel constructor(
6060
}
6161

6262
private val retryPolicy = object : RetryPolicy {
63-
override fun shouldRetry(attempt: Int, error: StreamError): Boolean = attempt <= 3
63+
override fun shouldRetry(attempt: Int, error: Error): Boolean = attempt <= 3
6464

65-
override fun retryTimeout(attempt: Int, error: StreamError): Int = 3000
65+
override fun retryTimeout(attempt: Int, error: Error): Int = 3000
6666
}
6767
}

stream-result-call-retrofit/api/stream-result-call-retrofit.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public abstract interface class io/getstream/result/call/retrofit/ErrorParser {
22
public abstract fun fromJson (Ljava/lang/String;)Ljava/lang/Object;
3-
public abstract fun toError (Lokhttp3/Response;)Lio/getstream/result/StreamError;
4-
public abstract fun toError (Lokhttp3/ResponseBody;)Lio/getstream/result/StreamError;
3+
public abstract fun toError (Lokhttp3/Response;)Lio/getstream/result/Error;
4+
public abstract fun toError (Lokhttp3/ResponseBody;)Lio/getstream/result/Error;
55
}
66

77
public final class io/getstream/result/call/retrofit/ErrorParser$DefaultImpls {
8-
public static fun toError (Lio/getstream/result/call/retrofit/ErrorParser;Lokhttp3/Response;)Lio/getstream/result/StreamError;
9-
public static fun toError (Lio/getstream/result/call/retrofit/ErrorParser;Lokhttp3/ResponseBody;)Lio/getstream/result/StreamError;
8+
public static fun toError (Lio/getstream/result/call/retrofit/ErrorParser;Lokhttp3/Response;)Lio/getstream/result/Error;
9+
public static fun toError (Lio/getstream/result/call/retrofit/ErrorParser;Lokhttp3/ResponseBody;)Lio/getstream/result/Error;
1010
}
1111

1212
public final class io/getstream/result/call/retrofit/RetrofitCall : io/getstream/result/call/Call {

stream-result-call-retrofit/src/main/kotlin/io/getstream/result/call/retrofit/ErrorParser.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,54 @@
1515
*/
1616
package io.getstream.result.call.retrofit
1717

18-
import io.getstream.result.StreamError
18+
import io.getstream.result.Error
1919
import okhttp3.Response
2020
import okhttp3.ResponseBody
2121

2222
public interface ErrorParser<T> {
2323

2424
public fun <T : Any> fromJson(raw: String): T
2525

26-
public fun toError(okHttpResponse: Response): StreamError {
26+
public fun toError(okHttpResponse: Response): Error {
2727
val statusCode: Int = okHttpResponse.code
2828

2929
return try {
3030
// Try to parse default Stream error body
3131
val body = okHttpResponse.peekBody(Long.MAX_VALUE).string()
3232

3333
if (body.isEmpty()) {
34-
StreamError.NetworkError(
34+
Error.NetworkError(
3535
message = okHttpResponse.message,
36-
streamCode = statusCode,
36+
serverErrorCode = statusCode,
3737
statusCode = statusCode
3838
)
3939
} else {
40-
StreamError.NetworkError(
41-
streamCode = statusCode,
40+
Error.NetworkError(
4241
message = okHttpResponse.message,
42+
serverErrorCode = statusCode,
4343
statusCode = statusCode
4444
)
4545
}
4646
} catch (expected: Throwable) {
47-
StreamError.ThrowableError(
47+
Error.ThrowableError(
4848
message = expected.message ?: expected.stackTraceToString(),
4949
cause = expected
5050
)
5151
}
5252
}
5353

54-
public fun toError(errorResponseBody: ResponseBody): StreamError {
54+
public fun toError(errorResponseBody: ResponseBody): Error {
5555
return try {
5656
val errorResponse: DefaultErrorResponse = fromJson(errorResponseBody.string())
5757
val (code, message, statusCode) = errorResponse
5858

59-
StreamError.NetworkError(
60-
streamCode = code,
59+
Error.NetworkError(
60+
serverErrorCode = code,
6161
message = message,
6262
statusCode = statusCode
6363
)
6464
} catch (expected: Throwable) {
65-
StreamError.ThrowableError(
65+
Error.ThrowableError(
6666
message = expected.message ?: expected.stackTraceToString(),
6767
cause = expected
6868
)

stream-result-call-retrofit/src/main/kotlin/io/getstream/result/call/retrofit/RetrofitCall.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package io.getstream.result.call.retrofit
1717

18+
import io.getstream.result.Error
1819
import io.getstream.result.Result
19-
import io.getstream.result.StreamError
2020
import io.getstream.result.call.Call
2121
import io.getstream.result.call.dispatcher.CallDispatcherProvider
2222
import kotlinx.coroutines.CoroutineScope
@@ -65,7 +65,7 @@ public class RetrofitCall<T : Any>(
6565

6666
private fun Throwable.toFailedError(
6767
message: String
68-
): StreamError = StreamError.ThrowableError(cause = this, message = message)
68+
): Error = Error.ThrowableError(cause = this, message = message)
6969

7070
@Suppress("TooGenericExceptionCaught")
7171
private suspend fun retrofit2.Call<T>.getResult(): Result<T> =

stream-result-call/api/stream-result-call.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public final class io/getstream/result/call/dispatcher/CallDispatcherProvider {
8888
}
8989

9090
public abstract interface class io/getstream/result/call/retry/RetryPolicy {
91-
public abstract fun retryTimeout (ILio/getstream/result/StreamError;)I
92-
public abstract fun shouldRetry (ILio/getstream/result/StreamError;)Z
91+
public abstract fun retryTimeout (ILio/getstream/result/Error;)I
92+
public abstract fun shouldRetry (ILio/getstream/result/Error;)Z
9393
}
9494

stream-result-call/src/main/kotlin/io/getstream/result/call/Call.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package io.getstream.result.call
1717

18+
import io.getstream.result.Error
1819
import io.getstream.result.Result
19-
import io.getstream.result.StreamError
2020
import io.getstream.result.call.retry.CallRetryService
2121
import io.getstream.result.call.retry.RetryCall
2222
import io.getstream.result.call.retry.RetryPolicy
@@ -76,7 +76,7 @@ public interface Call<T : Any> {
7676

7777
public companion object {
7878
public fun <T : Any> callCanceledError(): Result<T> =
79-
Result.Failure(StreamError.GenericError(message = "The call was canceled before complete its execution."))
79+
Result.Failure(Error.GenericError(message = "The call was canceled before complete its execution."))
8080

8181
@SuppressWarnings("TooGenericExceptionCaught")
8282
public suspend fun <T : Any> runCatching(
@@ -90,7 +90,7 @@ public interface Call<T : Any> {
9090

9191
private fun <T : Any> Throwable.toResult(): Result<T> = when (this) {
9292
is CancellationException -> callCanceledError()
93-
else -> Result.Failure(StreamError.ThrowableError(message = "", cause = this))
93+
else -> Result.Failure(Error.ThrowableError(message = "", cause = this))
9494
}
9595
}
9696
}
@@ -169,7 +169,7 @@ public fun <T : Any> Call<T>.withPrecondition(
169169
*/
170170
public fun <T : Any> Call<T>.onErrorReturn(
171171
scope: CoroutineScope,
172-
function: suspend (originalError: StreamError) -> Result<T>
172+
function: suspend (originalError: Error) -> Result<T>
173173
): ReturnOnErrorCall<T> = ReturnOnErrorCall(this, scope, function)
174174

175175
/**
@@ -186,11 +186,11 @@ public fun <T : Any> Call<T>.share(
186186
public fun Call<*>.toUnitCall(): Call<Unit> = map {}
187187

188188
private val onSuccessStub: (Any) -> Unit = {}
189-
private val onErrorStub: (StreamError) -> Unit = {}
189+
private val onErrorStub: (Error) -> Unit = {}
190190

191191
public fun <T : Any> Call<T>.enqueue(
192192
onSuccess: (T) -> Unit = onSuccessStub,
193-
onError: (StreamError) -> Unit = onErrorStub
193+
onError: (Error) -> Unit = onErrorStub
194194
) {
195195
enqueue { result ->
196196
when (result) {

stream-result-call/src/main/kotlin/io/getstream/result/call/ErrorCall.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
*/
1616
package io.getstream.result.call
1717

18+
import io.getstream.result.Error
1819
import io.getstream.result.Result
19-
import io.getstream.result.StreamError
2020
import io.getstream.result.call.dispatcher.CallDispatcherProvider
2121
import kotlinx.coroutines.CoroutineScope
2222
import kotlinx.coroutines.launch
2323
import kotlinx.coroutines.withContext
2424

2525
internal class ErrorCall<T : Any>(
2626
private val scope: CoroutineScope,
27-
private val e: StreamError
27+
private val e: Error
2828
) : Call<T> {
2929
override fun cancel() {
3030
// Not supported

stream-result-call/src/main/kotlin/io/getstream/result/call/ReturnOnErrorCall.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package io.getstream.result.call
1717

18+
import io.getstream.result.Error
1819
import io.getstream.result.Result
19-
import io.getstream.result.StreamError
2020
import io.getstream.result.call.dispatcher.CallDispatcherProvider
2121
import kotlinx.coroutines.CoroutineScope
2222
import kotlinx.coroutines.SupervisorJob
@@ -33,7 +33,7 @@ import kotlinx.coroutines.withContext
3333
public class ReturnOnErrorCall<T : Any>(
3434
private val originalCall: Call<T>,
3535
scope: CoroutineScope,
36-
private val onErrorReturn: suspend (originalError: StreamError) -> Result<T>
36+
private val onErrorReturn: suspend (originalError: Error) -> Result<T>
3737
) : Call<T> {
3838

3939
private val callScope = scope + SupervisorJob(scope.coroutineContext.job)

stream-result-call/src/main/kotlin/io/getstream/result/call/ZipCall.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package io.getstream.result.call
1717

18+
import io.getstream.result.Error
1819
import io.getstream.result.Result
19-
import io.getstream.result.StreamError
2020
import io.getstream.result.call.dispatcher.CallDispatcherProvider
2121
import kotlinx.coroutines.async
2222
import kotlinx.coroutines.runBlocking
@@ -67,7 +67,7 @@ internal class ZipCall<A : Any, B : Any>(
6767
return if (this is Result.Success && result is Result.Success) {
6868
Result.Success(Pair(this.value, result.value))
6969
} else {
70-
Result.Failure(StreamError.GenericError("Cannot combine results because one of them failed."))
70+
Result.Failure(Error.GenericError("Cannot combine results because one of them failed."))
7171
}
7272
}
7373

0 commit comments

Comments
 (0)