"feign error decoder not working" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "feign error decoder not working" answered properly. Developers are finding an appropriate answer about feign error decoder not working related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like feign error decoder not working. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on feign error decoder not working.
feign error decoder not working
package com.clientui.exceptions;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.CharStreams;
import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.*;
import java.io.*;
public class CustomErrorDecoder implements ErrorDecoder {
private final ErrorDecoder errorDecoder = new Default();
@Override
public Exception decode(String s, Response response) {
String message = null;
Reader reader = null;
try {
reader = response.body().asReader();
//Easy way to read the stream and get a String object
String result = CharStreams.toString(reader);
//use a Jackson ObjectMapper to convert the Json String into a
//Pojo
ObjectMapper mapper = new ObjectMapper();
//just in case you missed an attribute in the Pojo
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//init the Pojo
ExceptionMessage exceptionMessage = mapper.readValue(result,
ExceptionMessage.class);
message = exceptionMessage.message;
} catch (IOException e) {
e.printStackTrace();
}finally {
//It is the responsibility of the caller to close the stream.
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
switch (response.status()) {
case 404:
return new FileNotFoundException(message == null ? "File no found" :
message);
case 403:
return new ForbiddenAccessException(message == null ? "Forbidden
access" : message);
}
return errorDecoder.decode(s, response);
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public static class ExceptionMessage{
private String timestamp;
private int status;
private String error;
private String message;
private String path;
}
}
Source: stackoverflow.com
All those coders who are working on the Whatever based application and are stuck on feign error decoder not working can get a collection of related answers to their query. Programmers need to enter their query on feign error decoder not working related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about feign error decoder not working for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of feign error decoder not working while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "feign error decoder not working". Visit this developer's friendly online web community, CodeProZone, and get your queries like feign error decoder not working resolved professionally and stay updated to the latest Whatever updates.