Skip to content

Instantly share code, notes, and snippets.

@SamueldaCostaAraujoNunes
Created July 14, 2022 00:56
Show Gist options
  • Save SamueldaCostaAraujoNunes/80876a5a01ade269d5dce2cef1c18259 to your computer and use it in GitHub Desktop.
Save SamueldaCostaAraujoNunes/80876a5a01ade269d5dce2cef1c18259 to your computer and use it in GitHub Desktop.
@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