Created
July 14, 2022 00:56
-
-
Save SamueldaCostaAraujoNunes/80876a5a01ade269d5dce2cef1c18259 to your computer and use it in GitHub Desktop.
This file contains 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
@Target(AnnotationTarget.FUNCTION) | |
@Retention(AnnotationRetention.RUNTIME) | |
@Repeatable | |
annotation class ErrorType(val httpStatusCodeError: HttpStatusCodeError, val kClass: KClass<out Result.Error.ErrorResponse>) | |
fun parseError(statusCode: Int, responseBody: ResponseBody?, annotations: Array<out Annotation>): Result.Error.ErrorResponse? { | |
val errorBody = responseBody?.charStream()?.readText() ?: return null | |
val errorType: ErrorType = annotations.find { | |
it is ErrorType && it.httpStatusCodeError.statusCode == statusCode | |
} as? ErrorType ?: return null | |
return Gson().fromJson(errorBody, errorType.kClass.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment