private Exception instantiateUnexpectedException(SwaggerMethodParser methodParser, HttpResponse response, String responseContent) { final int responseStatusCode = response.statusCode(); final Class<? extends RestException> exceptionType = methodParser.exceptionType(); final Class<?> exceptionBodyType = methodParser.exceptionBodyType(); String contentType = response.headerValue("Content-Type"); String bodyRepresentation; if ("application/octet-stream".equalsIgnoreCase(contentType)) { bodyRepresentation = "(" + response.headerValue("Content-Length") + "-byte body)"; } else { bodyRepresentation = responseContent.isEmpty() ? "(empty body)" : "\"" + responseContent + "\""; } Exception result; try { final Constructor<? extends RestException> exceptionConstructor = exceptionType.getConstructor(String.class, HttpResponse.class, exceptionBodyType); result = exceptionConstructor.newInstance("Status code " + responseStatusCode + ", " + bodyRepresentation, response, response.deserializedBody()); } catch (ReflectiveOperationException e) { String message = "Status code " + responseStatusCode + ", but an instance of " + exceptionType.getCanonicalName() + " cannot be created." + " Response body: " + bodyRepresentation; result = new IOException(message, e); } return result; }