CherryBelle
CherryBelle

Reputation: 1422

How to Get Error Body Response with Retrofit 2

I'm using Retrofit 2 and I need to handle response error in JSON format. Below is the example of the response body.

{
    "success": false,
    "error": {
        "message": {
            "name": [
                "This input is required."
            ]
        }
    }
}

message contains list of fields with errors which means the value is dynamic. Therefore, one of the possible solutions is by parsing the response body as JSON Object. I tried to get the error body using response.errorBody().string()

@Override
public void onResponse(final Call<Category> call, final Response<Category> response) {
    if (response.isSuccessful()) {

    } else {
        // Handle error
        String errorBody = response.errorBody().string();
    }
}

Unfortunately, printing the errorBody I can only get the following result {"success":false,"error":{"message":{"name":[""]}}} Is Retrofit limiting the errorBody object depth? What should I do to get the full response body that can be parsed?

Upvotes: 7

Views: 12322

Answers (4)

Jcorretjer
Jcorretjer

Reputation: 497

Here is an extension alternative, in Kotlin, to serialize the errorBody:

fun ResponseBody?.responseBodyToString() = this?.charStream()?.readText().toString()

An example of how to use this:

JSONObject(response.errorBody()?.responseBodyToString() ?: "")

Then, you can do something, similarly, suggested in a previous post.

Upvotes: 0

Mayur Sojitra
Mayur Sojitra

Reputation: 712

Try this snippet I used this in my retrofit. and getting dynamic error messages solution

@Override
public void onResponse(final Call<Category> call, final Response<Category> response) {
    if (response.isSuccessful()) {

    } else {
        try {
            String errorBody = response.errorBody().string();

            JSONObject jsonObject = new JSONObject(errorBody.trim());

            jsonObject = jsonObject.getJSONObject("error");

            jsonObject = jsonObject.getJSONObject("message");

            Iterator<String> keys = jsonObject.keys();
            String errors = "";
            while (keys.hasNext()) {
                String key = keys.next();
                JSONArray arr = jsonObject.getJSONArray(key);
                for (int i = 0; i < arr.length(); i++) {
                    errors += key + " : " + arr.getString(i) + "\n";
                }
            }
            Common.errorLog("ERRORXV", errors);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 8

NIKHIL MAURYA
NIKHIL MAURYA

Reputation: 304

I think you will get the full response if you tried response.body.toString().

Upvotes: 0

Onur D.
Onur D.

Reputation: 570

Just use response.message()

@Override
public void onResponse(final Call<Category> call, final Response<Category> response) {
    if (response.isSuccessful()) {

    } else {
        // Handle error
        Log.i("Response message: ", response.message());
    }
}

Upvotes: -2

Related Questions