Skip to content
Snippets Groups Projects

update: add support for alternate error format from accounts API

Merged Martin Lowe requested to merge malowe/main/115-2 into main
All threads resolved!
4 files
+ 84
11
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -11,13 +11,17 @@
**********************************************************************/
package org.eclipsefoundation.profile.helpers;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.rest.client.inject.RestClient;
@@ -58,6 +62,8 @@ public class ProfileHelper {
private static final Pattern ALTERNATE_EMAIL_PATTERN = Pattern.compile("alt-email\\s*:\\s*([\\w\\-\\.]+@[\\w\\-]+(\\.[\\w\\-]{2,4})+)");
private static final int GERRIT_RESPONSE_START_INDEX = 4;
private static final String ERROR_MSG = "Issue connecting to Accounts";
public static final String USER_NOT_FOUND_MESSAGE = "User not found.";
@RestClient
PeopleAPI peopleAPI;
@@ -416,18 +422,43 @@ public class ProfileHelper {
if (r.getStatus() != Status.NOT_FOUND.getStatusCode()) {
return false;
}
// check the list value to check if it matches the expected output value
List<String> listEntity = Collections.emptyList();
try(InputStream in = ((ResponseImpl) r).getEntityStream()) {
// it can't be true, as this should always have a value
if (in == null) {
return false;
String errorMessage = "";
try (InputStream in = ((ResponseImpl) r).getEntityStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buff = new BufferedReader(reader)) {
// needs to be read in first, as you can't reuse an input stream
String rawJson = buff.lines().collect(Collectors.joining("\n"));
LOGGER.trace("Checking 404 body for matched error format: {}", rawJson);
// check object-based error message format
errorMessage = attemptObjectBasedErrorReading(rawJson);
// check legacy format
if (StringUtils.isBlank(errorMessage)) {
List<String> listEntity = objectMapper.readerForListOf(String.class).readValue(rawJson);
if (listEntity.size() == 1) {
errorMessage = listEntity.get(0);
}
}
} catch (Exception e) {
// we want to swallow this exception in most cases, as it isn't useful and is noisy
LOGGER.trace("Could not read value as legacy format", e);
}
return USER_NOT_FOUND_MESSAGE.equalsIgnoreCase(errorMessage);
}
private String attemptObjectBasedErrorReading(String in) {
try {
Map<String,String> error = objectMapper.readerFor(Map.class).readValue(in);
if (error != null) {
return error.get("message");
}
listEntity = objectMapper.readerForListOf(String.class).readValue(in);
} catch (Exception e) {
// we want to swallow this exception, as it isn't useful
// we want to swallow this exception in most cases, as it isn't useful
LOGGER.trace("Could not read value as modern format", e);
}
return listEntity.size() == 1 && listEntity.get(0).equalsIgnoreCase("User not found.");
return null;
}
/**
Loading