public PartnerException handleFailedResponse()

in src/main/java/com/microsoft/store/partnercenter/errorhandling/DefaultPartnerServiceErrorHandler.java [46:102]


    public PartnerException handleFailedResponse(Response response, IRequestContext context)
    {
        if (response == null)
        {
            throw new IllegalArgumentException("Response is null");
        }

        if (response.code() < 400)
        {
            throw new IllegalArgumentException("DefaultPartnerServiceErrorHandler: response is successful.");
        }

        String responsePayload;

        try
        {
        	if (response.body() == null || response.body().contentLength() == 0)
        	{
        		throw new IllegalArgumentException("Response entity is null");
            }
            
            responsePayload = response.body().string();
        }
        catch (Exception e)
        {
            responsePayload = "";
        }

        ApiFault apiFault = null;
        PartnerException partnerException = null;

        // log the failed response
        PartnerLog.getInstance().logError(MessageFormat.format("Partner service failed response:{0}", responsePayload));

        // attempt to deserialize the response into an ApiFault object as this is what the partner service is
        // expected to do when it errors out
        ObjectMapper mapper = new ObjectMapper();

        try
        {
            apiFault = mapper.readValue(responsePayload, ApiFault.class);
        }
        catch (IOException e)
        {
            PartnerLog.getInstance().logError("Could not parse error response: " + e.toString());
        }

        PartnerErrorCategory errorCategory;

        errorCategory = toPartnerErrorCategory(response.code());
        partnerException = apiFault != null ? new PartnerException(apiFault, context, errorCategory)
                        : new PartnerException(StringHelper.isNullOrWhiteSpace(responsePayload)
                                        ? response.message() : responsePayload, context,
                                                errorCategory);

        return partnerException;
    }