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

Deserialising to error object if success object fails #324

Open
hubertgrzeskowiak opened this issue Feb 6, 2021 · 0 comments
Open

Deserialising to error object if success object fails #324

hubertgrzeskowiak opened this issue Feb 6, 2021 · 0 comments

Comments

@hubertgrzeskowiak
Copy link

In case of errors, most APIs will return an error JSON object describing details of what happened.
How would you go about getting that? My attempt looks very clunky (slightly rewritten to show the problem).

private fun parseResponse(response: ResponseBody): Either<FailureResponse, SuccessResponse> {
        return try {
            success(serializer.parse<SuccessResponse>(response))
        } catch (e1: KlaxonException) {
            // if planned success parsing failed, try parsing to a FailureResponse
            return try {
                failure(serializer.parse<FailureResponse>(response)))
            } catch (e2: KlaxonException) {
                failure(SerialisationFailure.fromException(e1))
            }
        } catch (e: IllegalArgumentException) {
            // Likely caused by Enum.valueOf() when deserializing to enums. Make sure enum is complete.
            failure(SerialisationFailure.fromException(e))
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant