Existe essa possibilidade verificar o login vindo da api, se ela vem null , eu personalizo a exception dentro de um metodo estatico?
classe main
public class PrincipalChallengeTryCatch {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc3 = new Scanner(System.in);
System.out.println("Please enter the Github Username to find the account:");
String username = sc3.nextLine();
String usernameEncoder = URLEncoder.encode(username);
String url = "https://api.github.com/users/" + usernameEncoder;
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseGithub = response.body();
verifyIfGithubUsernameIsValid(responseGithub);
System.out.println(responseGithub);
} catch (ErroConsultGitHubException e) {
System.out.println(e.getMessage());
}
sc3.close();
}
private static void verifyIfGithubUsernameIsValid(String responseGithub) {
Gson gson = new Gson();
GithubDTO json = gson.fromJson(responseGithub, GithubDTO.class);
if (json.login() == null) {
throw new ErroConsultGitHubException("Username not exist on Github!");
}
}
}
**classe Record **
public record GithubDTO(String login) {
}
Classe para tratamento de erro personalizado
public class ErroConsultGitHubException extends RuntimeException {
public ErroConsultGitHubException(String message) {
super(message);
}
}