package com.gh4a;

import com.meisolsson.githubsdk.core.ServiceGenerator;
import com.meisolsson.githubsdk.model.ClientErrorResponse;

import java.io.IOException;

import retrofit2.Response;

public class ApiRequestException extends RuntimeException {
    private static final long serialVersionUID = -4331443972707730572L;

    private final ClientErrorResponse mResponse;
    private final int mStatus;

    public ApiRequestException(Response response) {
        mStatus = response.code();

        ClientErrorResponse error = null;
        try {
            error = ServiceGenerator.moshi
                    .adapter(ClientErrorResponse.class)
                    .fromJson(response.errorBody().source());
        } catch (IOException e) {
            // ignored
        }
        mResponse = error;
    }

    public int getStatus() {
        return mStatus;
    }

    public ClientErrorResponse getResponse() {
        return mResponse;
    }
}

@@ -12,16 +12,48 @@
 .gh4a;%0A%0A
+import android.text.TextUtils;%0A%0A
 import c
@@ -99,16 +99,16 @@
 erator;%0A
-
 import c
@@ -186,16 +186,67 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -990,24 +990,24 @@
 esponse() %7B%0A
-
         retu
@@ -1025,11 +1025,1951 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public String getMessage() %7B%0A        if (mResponse == null) %7B%0A            return super.getMessage();%0A        %7D%0A%0A        String message = mResponse.message();%0A        List%3CString%3E errors = new ArrayList%3C%3E();%0A%0A        List%3CClientErrorResponse.FieldError%3E fieldErrors = mResponse.errors();%0A        if (fieldErrors != null && !fieldErrors.isEmpty()) %7B%0A            for (ClientErrorResponse.FieldError e : fieldErrors) %7B%0A                String errorMessage = formatFieldError(e);%0A                if (!TextUtils.isEmpty(errorMessage)) %7B%0A                    errors.add(errorMessage);%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (!TextUtils.isEmpty(message) && !errors.isEmpty()) %7B%0A            return String.format(%22%251$s (%252$d) %5B%253$s%5D%22,%0A                    message, mStatus, TextUtils.join(%22, %22, errors));%0A        %7D else if (!TextUtils.isEmpty(message)) %7B%0A            return String.format(%22%251$s (%252$d)%22, message, mStatus);%0A        %7D else %7B%0A            return %22HTTP status %22 + mStatus;%0A        %7D%0A    %7D%0A%0A    private String formatFieldError(ClientErrorResponse.FieldError error) %7B%0A        switch (error.reason()) %7B%0A            case Invalid:%0A                return String.format(%22Value for field %251$s is invalid%22, error.field());%0A            case MissingField:%0A                return String.format(%22Value for required field %251$s is missing%22, error.field());%0A            case MissingResource:%0A                return String.format(%22Resource %251$s does not exist%22, error.resource());%0A            case AlreadyExists:%0A                return String.format(%0A                        %22A resource of type '%251$s' with the same value in field %252$s already exists%22,%0A                        error.resource(), error.field());%0A            case TooLarge:%0A                return String.format(%22The field %251$s was too large%22, error.field());%0A            case Custom:%0A                return error.message();%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

Add error message formatting to ApiRequestException.