Skip to content

Commit

Permalink
show errors (in XML) for verb params #9275
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin authored and stevenwinship committed Jan 22, 2024
1 parent 6f3aee4 commit 34196df
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/release-notes/9275-harvest-invalid-query-params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OAI-PMH error handling has been improved to display a machine-readable error in XML rather than a 500 error with no further information.

- /oai?foo=bar will show "No argument 'verb' found"
- /oai?verb=foo&verb=bar will show "Verb must be singular, given: '[foo, bar]'"
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.MailUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;
import io.gdcc.xoai.exceptions.BadArgumentException;
import io.gdcc.xoai.exceptions.BadVerbException;
import io.gdcc.xoai.exceptions.OAIException;
import io.gdcc.xoai.model.oaipmh.Granularity;
import io.gdcc.xoai.model.oaipmh.verbs.Verb;
import io.gdcc.xoai.services.impl.SimpleResumptionTokenFormat;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -256,9 +259,18 @@ private void processRequest(HttpServletRequest httpServletRequest, HttpServletRe
"Sorry. OAI Service is disabled on this Dataverse node.");
return;
}

RawRequest rawRequest = RequestBuilder.buildRawRequest(httpServletRequest.getParameterMap());


RawRequest rawRequest = null;
try {
rawRequest = RequestBuilder.buildRawRequest(httpServletRequest.getParameterMap());
} catch (BadVerbException bve) {
// Verb.Type is required. Hard-code one.
rawRequest = new RawRequest(Verb.Type.Identify);
// Ideally, withError would accept a BadVerbException.
BadArgumentException bae = new BadArgumentException(bve.getLocalizedMessage());
rawRequest.withError(bae);
}

OAIPMH handle = dataProvider.handle(rawRequest);
response.setContentType("text/xml;charset=UTF-8");

Expand Down
34 changes: 29 additions & 5 deletions src/test/java/edu/harvard/iq/dataverse/api/HarvestingServerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,35 @@ public void testMultiRecordOaiSet() throws InterruptedException {

@Test
public void testInvalidQueryParams() {
// "foo" is not a valid verb
String oaiVerbPath = "/oai?foo=bar";
Response identifyResponse = given().get(oaiVerbPath);
// TODO Why is this 500? https://github.com/IQSS/dataverse/issues/9275
identifyResponse.then().assertThat().statusCode(500);

// The query parameter "verb" must appear.
Response noVerbArg = given().get("/oai?foo=bar");
noVerbArg.prettyPrint();
noVerbArg.then().assertThat()
.statusCode(OK.getStatusCode())
// This should be "badVerb"
.body("oai.error.@code", equalTo("badArgument"))
.body("oai.error", equalTo("No argument 'verb' found"));

// The query parameter "verb" cannot appear more than once.
Response repeated = given().get( "/oai?verb=foo&verb=bar");
repeated.prettyPrint();
repeated.then().assertThat()
.statusCode(OK.getStatusCode())
// This should be "badVerb"
.body("oai.error.@code", equalTo("badArgument"))
.body("oai.error", equalTo("Verb must be singular, given: '[foo, bar]'"));

}

@Test
public void testNoSuchSetError() {
Response noSuchSet = given().get("/oai?verb=ListIdentifiers&set=census&metadataPrefix=dc");
noSuchSet.prettyPrint();
noSuchSet.then().assertThat()
.statusCode(OK.getStatusCode())
.body("oai.error.@code", equalTo("noRecordsMatch"))
.body("oai.error", equalTo("Requested set 'census' does not exist"));
}

// TODO:
Expand Down

0 comments on commit 34196df

Please sign in to comment.