@Override public Optional<RemoteException> handle(Response response) { if (response.body() == null || response.body().byteStream() == null || response.isSuccessful() || response.code() == MoreHttpCodes.SWITCHING_PROTOCOLS) { return Optional.empty(); } Collection<String> contentTypes = response.headers("Content-Type"); if (contentTypes.contains(MediaType.APPLICATION_JSON) && !response.request().method().equals("HEAD")) { final String body; try { body = toString(response.body().byteStream()); } catch (IOException e) { return Optional.empty(); } try { SerializableError serializableError = MAPPER.readValue(body, SerializableError.class); return Optional.of(new RemoteException(serializableError, response.code())); } catch (Exception e) { log.warn("Failed to deserialize JSON, could not deserialize SerializableError", SafeArg.of("code", response.code()), UnsafeArg.of("body", body), e); } } return Optional.empty(); }
private static Optional<RemoteException> decode(String contentType, int status, @CheckForNull String body) { return handler.handle(response(status, contentType, body)); }
@Override public Optional<RemoteException> handle(Response response) { if (response.body() == null || response.body().byteStream() == null || response.isSuccessful() || response.code() == MoreHttpCodes.SWITCHING_PROTOCOLS) { return Optional.empty(); } Collection<String> contentTypes = response.headers("Content-Type"); if (contentTypes.contains(MediaType.APPLICATION_JSON) && !response.request().method().equals("HEAD")) { final String body; try { body = toString(response.body().byteStream()); } catch (IOException e) { return Optional.empty(); } try { SerializableError serializableError = MAPPER.readValue(body, SerializableError.class); return Optional.of(new RemoteException(serializableError, response.code())); } catch (Exception e) { log.warn("Failed to deserialize JSON, could not deserialize SerializableError", SafeArg.of("code", response.code()), UnsafeArg.of("body", body), e); } } return Optional.empty(); }