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

Wrap parse exceptions in Response object. #4

Merged
merged 5 commits into from
Feb 11, 2024
Merged
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
Prev Previous commit
Next Next commit
Add kdoc to Response.
  • Loading branch information
Bram-- committed Feb 11, 2024
commit e65537c1ecd4510f77c4ab90eef6b972d67831b7
5 changes: 5 additions & 0 deletions src/main/kotlin/org/audux/bgg/response/Response.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import com.fasterxml.jackson.core.JacksonException
import com.fasterxml.jackson.databind.ObjectMapper
import io.ktor.client.statement.bodyAsText

/**
* Wraps a successful or erroneous response, if a valid response was given it can be found in [data]
* otherwise the response can be found as a string in [error].
*/
data class Response<T>(
val error: String? = null,
val data: T? = null,
Expand All @@ -29,6 +33,7 @@ data class Response<T>(
fun isError() = !isSuccess()

companion object {
/** Create a new response from the given response string, using the [mapper]. */
inline fun <reified T> from(bodyAsText: String, mapper: ObjectMapper): Response<T> {
return try {
Response(data = mapper.readValue(bodyAsText, T::class.java))
Expand Down
Loading