It is divided into the following four parts. (Forgive me for the XX edition) Call API [Preparation] Call API [Core] Call API [Handling] Call API [Call]
Since I actually hit the API at the core layer, In this hierarchy, we will handle the errors that appear.
The error here is not only Exception It also includes the case where the processing result is invalid.
Facade.java
package facade;
import client.Client;
import exception.NantokaApiBadResultException;
import exception.NantokaApiUnknownException;
import model.RequestDto;
import model.ResponseDto;
public class Facade {
/**Processing result code normal*/
private static final String SUCCESS = "0";
/**API client*/
private Client client = new Client();
/**
*Get the result of executing the API
*
* @param request request DTO
* @return response DTO
* @throws NantokaApiUnknownException When a communication error or result does not exist
* @throws NantokaApiBadResultException When processing result is invalid
*/
public ResponseDto nantokaApiFacade(RequestDto request)
throws NantokaApiUnknownException, NantokaApiBadResultException {
ResponseDto response = new ResponseDto();
try {
response = client.nantokaApiClient(request);
} catch (Exception e) {
//In case of communication error
throw new NantokaApiUnknownException(e.getMessage());
}
if (response == null) {
throw new NantokaApiUnknownException("I didn't get anything back");
}
if (!SUCCESS.equals(response.getResultCode())) {
throw new NantokaApiBadResultException("Illegal processing result");
}
return response;
}
}
Even in the case of communication error type Exception, even if the processing result is null for some reason I also tried to return with ʻUnknownException`
Well, I don't really understand this feeling, so I'd like to summarize it. Even if the processing result is invalid, it is like an error from here, so I returned it as an Exception
At this point, it can be said that the function to call the API is completed. All you have to do is call this one, so let's actually use it next time.
Recommended Posts