Skip to content

Commit

Permalink
clean up error handling #9275
Browse files Browse the repository at this point in the history
dataProvider.handle(params) allows us to return the correct error.
  • Loading branch information
pdurbin authored and stevenwinship committed Jan 22, 2024
1 parent 34196df commit 3d1f59a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
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 All @@ -51,6 +49,7 @@
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
Expand Down Expand Up @@ -260,18 +259,15 @@ private void processRequest(HttpServletRequest httpServletRequest, HttpServletRe
return;
}

RawRequest rawRequest = null;
Map<String, String[]> params = httpServletRequest.getParameterMap();
OAIPMH handle;
try {
rawRequest = RequestBuilder.buildRawRequest(httpServletRequest.getParameterMap());
RawRequest rawRequest = RequestBuilder.buildRawRequest(params);
handle = dataProvider.handle(rawRequest);
} 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);
handle = dataProvider.handle(params);
}

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

try (XmlWriter xmlWriter = new XmlWriter(response.getOutputStream(), repositoryConfiguration);) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,15 @@ public void testInvalidQueryParams() {
noVerbArg.prettyPrint();
noVerbArg.then().assertThat()
.statusCode(OK.getStatusCode())
// This should be "badVerb"
.body("oai.error.@code", equalTo("badArgument"))
.body("oai.error.@code", equalTo("badVerb"))
.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.@code", equalTo("badVerb"))
.body("oai.error", equalTo("Verb must be singular, given: '[foo, bar]'"));

}
Expand Down

0 comments on commit 3d1f59a

Please sign in to comment.