Java - How to catch all exceptions/errors from xml validating file - java

I woudlike to get all errors from my validating xml. Actually my method stop after catch the first error :
public List<String> validation(File file) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
List<String> statut = new ArrayList<>();
try {
assert xsd != null;
Schema schema = schemaFactory.newSchema(xsd);
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(file.getPath() + file.getname())));
statut.add(file.getname() + " is valid");
} catch (SAXException | IOException e) {
statut.add(file.getname() + " not valid :\n");
statut.add(e.getMessage());
}
return statut;
}
How can I get all exception detected ?

You can use Validator.setErrorHandler(ErrorHandler) to register your own error handler.
public abstract void setErrorHandler(ErrorHandler errorHandler)
Sets the ErrorHandler to receive errors encountered during the validate method invocation.
Error handler can be used to customize the error handling process during a validation. When an ErrorHandler is set, errors found during the validation will be first sent to the ErrorHandler.
The error handler can abort further validation immediately by throwing SAXException from the handler. Or for example it can print an error to the screen and try to continue the validation by returning normally from the ErrorHandler
If any Throwable is thrown from an ErrorHandler, the caller of the validate method will be thrown the same Throwable object.
Validator is not allowed to throw SAXException without first reporting it to ErrorHandler.
In your own error handler, you can aggregate the exceptions and then after validation, process as needed/wanted.
Specifically, it allows you to continue validation if errors are encountered enabling you to catch them all in the handler.

Related

Parse exception in try catch block to assign a response code - 2xx, 4xx or 5xx

I have Jenkins plugin written in Java. I am capturing all the workflows of execution of plugin in a integer variable in three ways 0(2xx workflows), 1(4xx workflows), 2(5xx workflows) and sending them to SignalFX for metrics. Since this is not an API and errors will be mainly caught in try catch workflow.
I wanted to ask how to read error codes from exception class and categorize them in 2xx, 4xx or 5xx. Are there some rules which I can follow by?
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e) {
sendToSignalFX(0,data); // 0 means successful state
}
Some of the exceptions classes I will be using are -
Exception, IOException, InterruptedException, ParserConfigurationException, SAXException
I believe you may have to add a method to identify the failure reason from e.getMessage() for one OR have a custom exception to help with your case.
Also if it’s an HTTP request-related exception (from the error response code mentioned in the question details) or something, you may want to add a custom exception, instead of throwing the original exception. In the custom exception, you can add a custom field to get errorCode from the response code.
// MyCustomException.java
public class MyCustomException extends Exception {
String errorReason;
int errorCode;
public MyCustomException(Throwable throwable, String errorReason, int errorCode) {
super(errorReason, throwable);
this.errorReason = errorReason;
this.errorCode = errorCode;
}
}
And in your request handler code:
try {
// otherCode which might cause IOException
// ...
Response response = myHttpRequest();
if (!response.isSuccessful()) {
// identify the error code and set corresponding errorCode to MyCustomException. errorCode
int errorCode = 0;
// parse response.getStatusCode() or equivalent of the library and reassign the value of errorCode
throw new MyCustomException(e, e.getMessage(), errorCode);
}
// ...
// otherCode which might cause IOException
} catch (Exception | IOException e) {
throw new MyCustomException(e, e.getMessage(), 0);
}

Throwing a exception with custom message from Mockito Junit testing

try {
response = restTemplate.postForEntity(endPoint, request, Object.class);
lOGGER.info("response is " + response);
} catch (Exception e) {
lOGGER.error("Exception :" + e.getMessage());
if ((e.getMessage().contains("401") || e.getMessage().contains("Unauthorized")) ) {
ServiceImpl.evictTokenCache("authToken");
getData(requestDto);
} else {
throw new CustomException(e.getMessage());
}
above is my service and trying to write test case for catch clause,and my test case is
#Test()
public void getExceptionTest() throws Exception {
RequestDto requestDto = new RequestDto();
requestDto.setPage("1");
AuthConfig authDTO = new AuthConfig();
authDTO.setUrl("Url");
Mockito.when(restTemplate.postForEntity(Mockito.anyString(), Mockito.any(), Mockito.any())).thenThrow((new Exception("Unauthorized")));
ResponseDTO response = restCallUtil.getData(requestDto);
assertNull(response);
}
what i am trying to do incatch block is,when ever i get Unauthorized exception i am clearing the cache and calling the same method again. So in order to test catch block from my test class i am trying to throw exception with message as "Unauthorized ",but when i run test case i am getting
org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: java.lang.Exception: Unauthorized
The first thing to note is that an instance of Exception which is not also an instance of RuntimeException is a checked exception. Thus, the only way the for a method to throw a generic Exception is for the method signature to contain "throws Exception" or "throws Throwable".
So.. why are you getting this error? You are stubbing the postForEntity method which throws RestClientException, and Exception is not an instance of RestClientException. Thus, your stubbing is invalid.
If you want to stub the method such that the catch block for the generic case is entered, you will need to throw a RuntimeException, which is unchecked, and therefore not subject to the "throws" restriction.
That said, it would be probably be wiser to simply catch only the RestClientException in the original method. If there is some RuntimeException you want to catch, then you would hopefully already be aware of what that is and catch it specifically.

Struts2 Exception handling usage?

I have my Action class below in which getTspNameIdMap throws ReqMgmtException exception (custom exception).
public String findTspNameIdMap(){
SlsReqMgmtCommonRemote slsReqMgmtCommonRemote = null;
tspNameIdMap = new HashMap<String, String>();
try{
slsReqMgmtCommonRemote = getSlsReqMgmtCommonRemote();
tspNameIdMap = slsReqMgmtCommonRemote.getTspNameIdMap(gmaThresholdParameters.getId().getCircleId());
}
catch(ReqMgmtException rEx){
addActionError(rEx.getError());
result = "error";
return ERROR;
}
catch (Exception e){
addActionError("Error in processing your request. Contact Administrator");
e.printStackTrace();
System.out.println("[ConfigureTspThresholdAction: findTspNameIdMap Function]:In catch Inside Constructor!!");
result = "error";
return ERROR;
}
return SUCCESS;
}
I know there is exception handling in Struts2 too, however presently I am not using it.
Should I use Struts2 exception handling? What would be its usage?
You should use exception handling mechanism in Struts2, that's what exception interceptor provides. Also you should handle exceptions in the action method like in your question. If it handles all exceptions good, if not the exception handler could handle it. Also in some methods which doesn't have throws Exception signature you can only catch the exception but cannot return ERROR result. So, rethrowing the exception and handling it by the interceptor is the workaround.
References:
Exception handling in struts2 and hibernate
Which Exceptions to declare/handle in struts xml
Struts 2 what to do when user inserts action which does not exist
how to validate json being sent using struts2 json plugin without throwing exception

How to extract useful information from TransformerException

I am using javax.xml.transform.* to do XSLT transformation. Since the xslt file to be used comes from the outside world there could be errors in that file, and I am going to give back some meaningful response to the user.
Although I can easily catch the TransformationExceptions, I found no way to obtain enough information from it. For example, if there is a tag to be terminated by an end-tag, printStackTrace() gives scarring message
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
... (100 lines)
and getMessage() gives only
Could not compile stylesheet
None of them gives the real reason of the error.
I noticed that in Eclipse test console I can see the following
[Fatal Error] :259:155: The element type "sometag" must be terminated by the matching end-tag "</sometag>".
ERROR: 'The element type "sometag" must be terminated by the matching end-tag "</sometag>".'
FATAL ERROR: 'Could not compile stylesheet'
This is exactly what I want. Unfortunately, since this is a web application, the user cannot see this.
How can I display the correct error message to the user?
Put your own ErrorListener on your Transformer instance using Transformer.setErrorListener, like so:
final List<TransformationException> errors = new ArrayList<TransformationException>();
Transformer transformer = ... ;
transformer.setErrorListener(new ErrorListener() {
#Override
public void error(TransformerException exception) {
errors.add(exception);
}
#Override
public void fatalError(TransformerException exception) {
errors.add(exception);
}
#Override
public void warning(TransformerException exception) {
// handle warnings as well if you want them
}
});
// Any other transformer setup
Source xmlSource = ... ;
Result outputTarget = ... ;
try {
transformer.transform(xmlSource, outputTarget);
} catch (TransformerException e) {
errors.add(e); // Just in case one is thrown that isn't handled
}
if (!errors.isEmpty()) {
// Handle errors
} else {
// Handle output since there were no errors
}
This will log all the errors that occur into the errors list, then you can use the messages off those errors to get what you want. This has the added benefit that it will try to resume the transformation after the errors occur. If this causes any problems, just rethrow the exception by doing:
#Override
public void error(TransformerException exception) throws TransformationException {
errors.add(exception);
throw exception;
}
#Override
public void fatalError(TransformerException exception) throws TransformationException {
errors.add(exception);
throw exception;
}
Firstly, it's likely that any solution will dependent on your choice of XSLT processor. Different implementations of the JAXP interface might well provide different information in the exceptions they generate.
It's possible that the error from the XML parser is available in a wrapped exception. For historic reasons, TransformerConfigurationException offers both getException() and getCause() to access wrapped exceptions, and it may be worth checking them both.
Alternatively it's possible that the information was supplied in a separate call to the ErrorListener.
Finally, this particular error is detected by the XML parser (not the XSLT processor) so in the first instance it will be handled by the parser. It may well be worth setting the parser's ErrorHandler and catching parsing errors at that level. If you want explicit control over the XML parser used by the transformation, use a SAXSource whose XMLReader is suitably initialized.
You can configure System.out to write in your own OutputStream.
Use of ErrorListener don't catch all output.
If you work with threads you can look here (http://maiaco.com/articles/java/threadOut.php) to avoid change of System.out for other threads.
example
public final class XslUtilities {
private XslUtilities() {
// only static methods
}
public static class ConvertWithXslException extends Exception {
public ConvertWithXslException(String message, Throwable cause) {
super(message, cause);
}
}
public static String convertWithXsl(String input, String xsl) throws ConvertWithXslException {
ByteArrayOutputStream systemOutByteArrayOutputStream = new ByteArrayOutputStream();
PrintStream oldSystemOutPrintStream = System.out;
System.setOut(new PrintStream(systemOutByteArrayOutputStream));
ByteArrayOutputStream systemErrByteArrayOutputStream = new ByteArrayOutputStream();
PrintStream oldSystemErrPrintStream = System.err;
System.setErr(new PrintStream(systemErrByteArrayOutputStream));
String resultXml;
try {
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource(new StringReader(xsl)));
StringWriter stringWriter = new StringWriter();
transformer.transform(new StreamSource(new StringReader(input)), new StreamResult(stringWriter));
resultXml = stringWriter.toString();
} catch (TransformerException e) {
System.out.flush();
final String systemOut = systemOutByteArrayOutputStream.toString();
System.err.flush();
final String systemErr = systemErrByteArrayOutputStream.toString();
throw new ConvertWithXslException("TransformerException - " + e.getMessageAndLocation()
+ (systemOut.length() > 0 ? ("\nSystem.out:" + systemOut) : "")
+ (systemErr.length() > 0 ? ("\nSystem.err:" + systemErr) : ""), e);
} finally {
System.setOut(oldSystemOutPrintStream);
System.setErr(oldSystemErrPrintStream);
}
return resultXml;
}
}

How to capture the mybatis exception "org.apache.ibatis.exceptions.PersistenceException"?

Here is the code sample, I want to capture the exception throwed by mybatis:
String resource = "com/sureone/server/db/mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = factory.openSession(true);
tUserMapper = sqlSession.getMapper(TUserMapper.class);
if(tUserMapper.insert(user)>0){ <===Exception throwed here for duplicate entry problem
return verifyLogin(user.getAccount(),user.getPassword());
}
return null;
The exception I want to captured:
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'userName' for key 'account_UNIQUE'
You can capture the PersistenceException as you would do usually :
try {
...
} catch (PersistenceException pe) {
}
But don't forget that this Exception wraps the real one:
From MyBatis code
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error committing transaction. Cause: " + e, e);
}
So if you would like the get a grip on the cause of the PersistenceException you'll have to use .getCause() method on the PersistenceException
Be aware that MyBatis can also launch its own PersistenceException (TooManyResultException,BindingException ...) classes, those won't have a cause Exception wrapped.
You can capture the ibatis exception by adding a try/catch block around your statements that invoke myBatis query/insert. For instance, if you use the SqlSessionTemplate and the selectList() method, you can do this:
try {
myResults = mySqlSessionTemplate.selectList("getInfoList", parameterMap);
} catch (final org.apache.ibatis.exceptions.PersistenceException ex) {
logger.error("Problem accessing database");
throw ex;
}
Whether you re-throw the exception or consume and deal with it here is your choice. However, beware of "eating" it and not dealing with the problem, since this will allow calling code to progress without knowing about the underlying data access problem.

Categories

Resources