Skip to content

Commit

Permalink
MODOAIPMH-556 - Improve clarity of the error message (folio-org#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
siarhei-charniak authored Apr 24, 2024
1 parent fb6f92a commit 8648543
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/folio/oaipmh/ResponseConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import org.openarchives.oai._2_0.oai_identifier.OaiIdentifier;
import org.purl.dc.elements._1.ObjectFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
Expand Down Expand Up @@ -160,14 +162,20 @@ public Object bytesToObject(byte[] byteSource) {
jaxbUnmarshaller.setSchema(oaipmhSchema);
}
return jaxbUnmarshaller.unmarshal(inputStream);
} catch (JAXBException | IOException e) {
} catch (JAXBException | IOException e) {
// In case there is an issue to unmarshal byteSource, there is no way to handle it
throw new IllegalStateException("The byte array cannot be converted to JAXB object response.", e);
throw new IllegalStateException(processException(e), e);
} finally {
logExecutionTime("Array of bytes converted to Object", timer);
}
}

private String processException(Exception e) {
return e instanceof UnmarshalException ue && ue.getLinkedException() instanceof SAXParseException se ?
se.getLocalizedMessage() :
"The byte array cannot be converted to JAXB object response.";
}

/**
* @return Checks if the Jaxb context initialized successfully
*/
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/folio/oaipmh/ResponseConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import javax.xml.bind.JAXBException;
import java.time.Instant;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;


Expand Down Expand Up @@ -67,4 +69,12 @@ void successCase() {

assertThat(oaipmh, equalTo(oaipmhFromString));
}

@Test
void shouldProvideDetailedErrorMessage() {
var source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><marc:record xmlns:marc=\"http://www.loc.gov/MARC21/slim\"><marc:leader>01344nja a2200289 c 4500</marc:leader><marc:datafield tag=\"035\" ind1=\" \" ind2=\" \"><marc:subfield code=\"a\">(DE-599)GBV\u001f1011162431</marc:subfield></marc:record>\n".getBytes();
var converter = ResponseConverter.getInstance();
var exception = assertThrows(IllegalStateException.class, () -> converter.bytesToObject(source));
assertThat(exception.getMessage(), containsString("An invalid XML character (Unicode: 0x1f) was found in the element content of the document."));
}
}

0 comments on commit 8648543

Please sign in to comment.