Hello everyone, I'm sure those of you who found this article can be sure are a developer who is familiar with retrofit.
Now I want to share my experience while working with error handling tasks. At first I continued the legacy project from my client in Indonesia. There are 2 main tasks that I need to complete, namely related to UI/UX and Error Handling.
Well let's start from a case I never expected before, yes frankly this is the first time I'm concerned about error code 400 that retrofit identified.
At first I wasn't sure about the error, because when I tested it in postman, the expected error model from the server was like this.

In the end I confirm this finding to the client to decide whether this error message will be shown to the user or not.
It turned out that the client left the decision entirely to me, well it doesn't matter. Okay, you need to know that I previously informed you that the 400 error message cannot be customized from the server side because retrofit has a special error model for code 400.

And the code listed below is the solution I offer.
if (response.isSuccessful) {
// TODO SOMETHING WITH SUCCESS RESULT
} else {
if (response.code() in 400..499) { //todo this for filtering error code 4xx
context?.let { ctx -> diTools.errorDialog(ctx, parentView, response.message()) } //todo this for notice to user
diTools.crashAnalytic().recordException(RuntimeException("${response.raw()}")) //todo this for reporting to firebase crashlytics
} else {
val converter: Converter<ResponseBody, ErrorResponse> =
request.responseBodyConverter(
ErrorResponse::class.java,
arrayOfNulls<Annotation>(0)
)
response.errorBody()?.let { error_response ->
val errorObject: ErrorResponse? = converter.convert(error_response)
try {
val msg = errorObject?.errors?.first().toString()
context?.let { ctx -> diTools.errorDialog(ctx, parentView, msg) }
} catch (e: Exception) {
diTools.crashAnalytic().recordException(e)
}
}
val moveVerivikasi = Intent(activity, VerifikasiKtpActivity::class.java)
startActivity(moveVerivikasi)
}
}
Done!