public Throwable instantiateException()

in sdk/core/azure-core-rest/src/main/java/com/azure/android/core/rest/implementation/HttpResponseExceptionInfo.java [49:115]


    public Throwable instantiateException(final JacksonSerder jacksonSerder,
                                          final HttpResponse httpResponse,
                                          final ClientLogger logger) {
        final byte[] responseContent = httpResponse.getBodyAsByteArray();
        final InputStream contentStream = (responseContent == null || responseContent.length == 0)
            ? null
            : new ByteArrayInputStream(responseContent);

        Object responseDecodedContent = null;
        try {
            responseDecodedContent = jacksonSerder.deserialize(contentStream,
                this.exceptionBodyType,
                SerdeEncoding.fromHeaders(httpResponse.getHeaders().toMap()));
        }  catch (IOException | SerdeParseException ex) {
            // Though we're unable to represent the wire-error as a POJO, we will communicate
            // the wire-error as exception error-message, hence logged as warning without throw.
            //
            logger.warning("Failed to deserialize the error entity.", ex);
        }

        final int responseStatusCode = httpResponse.getStatusCode();
        final String contentType = httpResponse.getHeaderValue("Content-Type");
        final String bodyRepresentation;
        if ("application/octet-stream".equalsIgnoreCase(contentType)) {
            bodyRepresentation = "(" + httpResponse.getHeaderValue("Content-Length") + "-byte body)";
        } else {
            bodyRepresentation = responseContent == null || responseContent.length == 0
                ? "(empty body)"
                : "\"" + new String(responseContent, Charset.forName("UTF-8")) + "\"";
        }

        Throwable result;
        try {
            final Constructor<? extends HttpResponseException> exceptionConstructor =
                this.exceptionType.getConstructor(String.class, HttpResponse.class,
                    this.exceptionBodyType);
            result = exceptionConstructor.newInstance("Status code "
                    + responseStatusCode + ", " + bodyRepresentation,
                httpResponse,
                responseDecodedContent);
        } catch (InstantiationException e) {
            String message = "Status code " + responseStatusCode + ", but an instance of "
                + this.exceptionType.getCanonicalName() + " cannot be created."
                + " Response body: " + bodyRepresentation;

            result = new IOException(message, e);
        } catch (IllegalAccessException e) {
            String message = "Status code " + responseStatusCode + ", but an instance of "
                + this.exceptionType.getCanonicalName() + " cannot be created."
                + " Response body: " + bodyRepresentation;

            result = new IOException(message, e);
        } catch (InvocationTargetException e) {
            String message = "Status code " + responseStatusCode + ", but an instance of "
                + this.exceptionType.getCanonicalName() + " cannot be created."
                + " Response body: " + bodyRepresentation;

            result = new IOException(message, e);
        } catch (NoSuchMethodException e) {
            String message = "Status code " + responseStatusCode + ", but an instance of "
                + this.exceptionType.getCanonicalName() + " cannot be created."
                + " Response body: " + bodyRepresentation;

            result = new IOException(message, e);
        }
        return result;
    }