Skip to content

Commit

Permalink
GnipUnprocessableEntityException now accept Powertrack V2 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jcodagnone committed Jun 16, 2016
1 parent e477644 commit 431b6f8
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 23 deletions.
Expand Up @@ -16,12 +16,16 @@
package com.zaubersoftware.gnip4j.api.exception;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zaubersoftware.gnip4j.api.model.Rule;
import com.zaubersoftware.gnip4j.api.support.http.Errors;
import com.zaubersoftware.gnip4j.api.support.http.Errors.Error;
import com.zaubersoftware.gnip4j.api.support.http.Errors.RuleErrorDetail;

/**
* Exception class that knows how to parse the 422 message error into a list of
Expand All @@ -38,7 +42,6 @@ public class GnipUnprocessableEntityException extends GnipException {

private final List<OffendingRule> offendingRules;

/** */
public GnipUnprocessableEntityException(final String message, final String serverMessage) {
super(message + "\n" + serverMessage);

Expand All @@ -47,29 +50,69 @@ public GnipUnprocessableEntityException(final String message, final String serve
}

// parse
offendingRules = new ArrayList<OffendingRule>();
parseMessage(serverMessage);
offendingRules = parseMessage(serverMessage);

// need to know that at least one offending rule was parsed
if (offendingRules.isEmpty()) {
throw new IllegalArgumentException("server message couldn't be parsed into offending rules");
}
}

/** */
public GnipUnprocessableEntityException(String message, List<OffendingRule> rules) {
super(message);
offendingRules = Objects.requireNonNull(rules);
private static String toMessage(final String message, final Errors errors) {
String ret;

if(message == null && errors == null) {
ret = "Error description was not provided";
} else if(message != null && errors == null) {
ret = message;
} else {
ret = message + "\n" + errors.toHumanMessage();
}

return ret;
}
public GnipUnprocessableEntityException(final String message, final Errors errors) {
super(toMessage(message, errors));

// v2 circa Jan 2016
final Error error = errors.getError();
if(error != null && error.getRules() != null) {
offendingRules = Objects.requireNonNull(error.getRules());
} else {
// v2 circa Jun 2016
final List<RuleErrorDetail> details = errors.getDetail();
if(details == null) {
final String msg = errors.toHumanMessage();
if(msg == null) {
offendingRules = new LinkedList<OffendingRule>();
} else {
offendingRules = parseMessage(msg);
}
} else {
offendingRules = new LinkedList<OffendingRule>();

for(final RuleErrorDetail detail : details) {
if(detail != null) {
final OffendingRule offendingRule = detail.toOffendingRule();
if(offendingRule != null) {
offendingRules.add(offendingRule);
}

}
}
}
}
}

public List<OffendingRule> getOffendingRules() {
return offendingRules;
}

/** parses the message using the pattern to obtain the offending rule */
private void parseMessage(final String serverMessage) {
private static List<OffendingRule> parseMessage(final String serverMessage) {
String offendingRule = null;
StringBuilder messageBuilder = new StringBuilder();
final List<OffendingRule> offendingRules = new LinkedList<OffendingRule>();

final String[] lines = serverMessage.split("\\n");
for (int i = 0, length = lines.length; i < length; i++) {
Expand All @@ -91,10 +134,12 @@ private void parseMessage(final String serverMessage) {
if (offendingRule != null) {
offendingRules.add(createOffendingRule(offendingRule, messageBuilder.toString()));
}

return offendingRules;
}

/** helper method to create the offending rule */
private OffendingRule createOffendingRule(final String rule, final String errorMessage) {
private static OffendingRule createOffendingRule(final String rule, final String errorMessage) {
final Rule offending = new Rule();
offending.setValue(rule);
return new OffendingRule(offending, errorMessage);
Expand Down
Expand Up @@ -41,10 +41,12 @@ public final void validateStatusLine(final URI uri, final int statusCode, final
// nothing to do
} else {
String msg = null;
Error error = null;
if (errorProvider != null && errorProvider.getError() != null) {
error = errorProvider.getError();
msg = error.getMessage();
Errors errors = null;
if (errorProvider != null) {
errors = errorProvider.getError();
if(errors != null) {
msg = errors.toHumanMessage();
}
}

if (msg == null) {
Expand All @@ -57,9 +59,8 @@ public final void validateStatusLine(final URI uri, final int statusCode, final
} else if (statusCode == 422) {
GnipUnprocessableEntityException exception = null;
try {
if (error != null && error.getRules() != null) {
exception = new GnipUnprocessableEntityException(String.format("Connection to %s", uri),
error.getRules());
if (errors != null) {
exception = new GnipUnprocessableEntityException(String.format("Connection to %s", uri), errors);
} else {
exception = new GnipUnprocessableEntityException(String.format("Connection to %s", uri), msg);
}
Expand Down
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/
package com.zaubersoftware.gnip4j.api.support.http;
import com.zaubersoftware.gnip4j.api.support.http.Errors.Error;
import com.zaubersoftware.gnip4j.api.support.http.Errors;

public interface ErrorProvider {
Error getError();
Errors getError();
}
Expand Up @@ -65,6 +65,47 @@ public Date getSent() {
public void setSent(final Date sent) {
this.sent = sent;
}

public final String toHumanMessage() {
String msg = null;

if(error != null) {
msg = error.getMessage();
}

if(detail != null) {
if(msg == null && detail.size() == 1) {
msg = detail.get(0).getMessage();
} else {
int n = msg == null ? 0 : msg.length() + 2;
for(final RuleErrorDetail deta : detail) {
final String s = deta.getMessage();
if(s != null) {
n += s.length() + 2;
}
}

final StringBuilder sb = new StringBuilder(n);
if(msg != null) {
sb.append(msg);
}
for(final RuleErrorDetail deta : detail) {
final String s = deta.getMessage();
if(s != null) {
if(deta.getRule() != null)
if(sb.length() != 0) {
sb.append('\n');
}
sb.append(s);
}
}
msg = sb.toString();
}
}


return msg;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Error {
Expand Down Expand Up @@ -117,6 +158,14 @@ public String getMessage() {
public Rule getRule() {
return rule;
}

public OffendingRule toOffendingRule() {
OffendingRule ret = null;
if(message != null) {
ret = new OffendingRule(rule, message);
}
return ret;
}
}

}
Expand Down
Expand Up @@ -239,15 +239,17 @@ public DefaultErrorProvider(final HttpURLConnection huc) {
}

@Override
public Error getError() {
public Errors getError() {
try {
final InputStream is = JRERemoteResourceProvider.getRealInputStream(huc, huc.getErrorStream());
if(huc.getContentType() != null && huc.getContentType().startsWith("application/json")) {
return m.readValue(is, Errors.class).getError();
return m.readValue(is, Errors.class);
} else {
final Errors errors = new Errors();
Error error = new Error();
error.setMessage(toInputStream(is));
return error;

return errors;
}
} catch (IOException e) {
logger.warn("Exception trying to read error message ", e);
Expand Down
Expand Up @@ -49,10 +49,11 @@ public final void test_v1() throws JsonParseException, JsonMappingException, IOE
final Errors errors = m.readValue(ExampleFileDirectory.POWERTRACK_RULE_ERROR_V1.asString(), Errors.class);
assertNotNull(errors);
assertNotNull(errors.getError());
assertEquals(
"Rule '-rule1' is invalid. Rules must contain a non-negation term (at position 1)\nRules must contain at least one positive, non-stopword clause (at position 1)\nRule '-rule14' is invalid. Rules must contain a non-negation term (at position 1)\nRules must contain at least one positive, non-stopword clause (at position 1)\n",
final String error = "Rule '-rule1' is invalid. Rules must contain a non-negation term (at position 1)\nRules must contain at least one positive, non-stopword clause (at position 1)\nRule '-rule14' is invalid. Rules must contain a non-negation term (at position 1)\nRules must contain at least one positive, non-stopword clause (at position 1)\n";
assertEquals(error,
errors.getError().getMessage());
assertNull(errors.getError().getRules());
assertEquals(error, errors.toHumanMessage());
}

@Test
Expand All @@ -76,6 +77,7 @@ public final void test_v2() throws JsonParseException, JsonMappingException, IOE
offendingRule.getErrorMessage());
assertEquals("-rule14", offendingRule.getOffendingRule().getValue());
assertNull(offendingRule.getOffendingRule().getTag());
assertEquals("One or more rules are invalid", errors.toHumanMessage());
}

@Test
Expand All @@ -101,6 +103,7 @@ public final void test_v2_bis() throws Exception {
assertEquals(msgs[i], r.getMessage());
assertNotNull(r.getRule());
}
assertEquals("no viable alternative at character '⚽' (at position 12)\n\n", errors.toHumanMessage());
}

enum ExampleFileDirectory {
Expand Down

0 comments on commit 431b6f8

Please sign in to comment.