I'm new in reactive programming using project reactor, I don't understand why I can't execute the method doOnError() in this case :
public class MyTest{
public static void main(String[] args) {
getMonoWithError().doOnError(throwable ->System.out.println(" " + throwable)).subscribe();}
private Mono<String> getMonoWithError() {throw new RuntimeException("MONO IN ERROR.....");}
}
Thank you for your help.
I expect to print the message "Error while processing....." and get the exception in the console.
Related
I am trying to save my Error Message and StackTrace when my Xamarin.Android App crashes.
In Xamarin.iOS I can simply wrap my Main-Method in a Try-Catch-Block as you can see in the screenshot below:
Since there is no Main-Method in Xamarin.Android (just my MainLauncher Activity), I ask myself the question, if there is a similarly simple method to log the exceptions.
I found a solution to Log all Unhandled Exception:
In my MainLauncher Activity:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;
and
private void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
{
e.Handled = true;
Logger = new LogUtils();
Logger.Log("ERROR: Unhandled Exception");
Logger.Log("MESSAGE: " + e.Exception.Message);
Logger.Log("STACKTRACE: " + e.Exception.StackTrace);
}
While writing unit test case for method someMethod1, I have a use case where I'm trying to ensure that an abstract exception (AnalysisException) is thrown when method (someMethod2) is called. Class under test is JdbcTemplateSampleImpl .
public class JdbcTemplateSampleImpl {
public void someMethod1() {
someMethod2();
}
private void someMethod2() throws AnalysisException {
// some code here
}
}
I am using PowerMockito to do like this
#Test(expected = com.test.AnalysisException.class)
public void abstractClassExceptionCheck2Test1() throws Throwable {
JdbcTemplateSampleImpl jdbcTemplateSampleImpl1 =
PowerMockito.spy(jdbcTemplateSampleImpl0);
PowerMockito.doThrow(mock(AnalysisException.class)).
when(jdbcTemplateSampleImpl1,"classCheck2");
jdbcTemplateSampleImpl1.abstractClassExceptionCheck2();
}
But while executing test case , i'm getting an error like this
java.lang.Exception: Unexpected exception, expected "com.test.AnalysisException" but was "java.lang.NullPointerException"
Mock the exception outside of the doThrow method call.
AnalysisException e = mock(AnalysisException.class);
PowerMockito.doThrow(e).
when(jdbcTemplateSampleImpl1,"classCheck2");
Iam working on mockito testcases positive test methods are getting executed but comming to Exception Test methods its failing with the Exception
java.lang.Exception: Unexpected exception, expected<com.apple.ist.retail.xcard.common.exception.InvalidArgumentException> but was<org.jboss.resteasy.client.ClientResponseFailure>
at
Below is the test method which is failing and its parent class containing client object
package com.apple.ist.retail.xcard.ws.exception;
public class TestActivatePrepaidCard extends CertificateResourceTestCase {
public TestActivatePrepaidCard(String aMediaType) {
super(aMediaType);
}
#Before
public void setUp() {
super.setUp();
}
#Test(expected = InvalidArgumentException.class)
public void testActivatePrepaidCard_InvalidArgumentException()
throws DuplicateCertificateIDException, InvalidArgumentException,
DupTxnRefException, AmountException, SystemException,
XCardException {
when(
server.activatePrepaidCard(any(DiagnosticContext.class),
any(String.class), any(Number.class),
any(Amount.class), any(String.class), any(int.class),
any(HashMap.class), any(String.class),
any(SalesOrg.class), any(TxnRef.class))).thenThrow(
new InvalidArgumentException("Invalid Argument ",
INVALID_ARGUMENT));
client.activatePrepaidCard(certificateRequest);
}
Its failing near client.activatePrepaidCard(certificateRequest); with ClientResponseFailure Exception
Parent test case is
package com.apple.ist.retail.xcard.ws.exception;
#RunWith(value = Parameterized.class)
public abstract class CertificateResourceTestCase extends Assert {
protected CertificateResource client;
protected XCardServiceServer server;
protected CertificateResource resource;
protected CertificateRequest certificateRequest;
// protected Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
private String mediaType;
public CertificateResourceTestCase(String aMediaType) {
this.mediaType = aMediaType;
server = mock(XCardServiceServer.class);
CertificateResourceImpl xcardServiceRs = new CertificateResourceImpl();
xcardServiceRs.setService(server);
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(xcardServiceRs);
dispatcher.getProviderFactory().addExceptionMapper(
XCardExceptionMapper.class);
dispatcher.getProviderFactory().addExceptionMapper(
BusinessExceptionMapper.class);
dispatcher.getProviderFactory().addExceptionMapper(
RuntimeExceptionMapper.class);
dispatcher.getProviderFactory().addExceptionMapper(
BusinessExceptionMapper.class);
dispatcher.getProviderFactory().addExceptionMapper(
RuntimeExceptionMapper.class);
dispatcher.getProviderFactory()
.getServerMessageBodyWriterInterceptorRegistry()
.register(new XCardTxnWriterInterceptor());
dispatcher.getProviderFactory().getContextDataMap()
.put(HttpServletRequest.class, new MockHttpServletRequest());
client = ProxyFactory.create(CertificateResource.class, "/", new InMemoryClientExecutor(dispatcher));
diagnosticContext.setReportingRecommended(false);
}
#After
public void tearDown() throws Exception {
Mockito.reset(server);
}
Please let me know whats wrong in my code,I am pasting complete code so that I will not miss any detail
Your code is throwing an ClientResponseFailure. Debug your test and find out why. Use an exception breakpoint.
After doing some research online I found that this would be the way to catch the exceptions and output my own error messages. For some reason I still cannot seem to catch the errors. Below is the code for a class that overrides antlrs default error handling.
All I want to do is catch the exception from antlr and output to the screen that the syntax is incorrect in a java gui.
public class ExceptionErrorStrategy extends DefaultErrorStrategy {
#Override
public void recover(Parser recognizer, RecognitionException e) {
throw e;
}
#Override
public void reportInputMismatch(Parser recognizer, InputMismatchException e) throws RecognitionException {
String msg = "Input is mismatched " + getTokenErrorDisplay(e.getOffendingToken());
msg += " expecting: "+e.getExpectedTokens().toString(recognizer.getTokenNames());
RecognitionException ex = new RecognitionException(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
ex.initCause(e);
throw ex;
}
#Override
public void reportMissingToken(Parser recognizer) {
beginErrorCondition(recognizer);
Token t = recognizer.getCurrentToken();
IntervalSet expecting = getExpectedTokens(recognizer);
String msg = "Missing "+expecting.toString(recognizer.getTokenNames()) + " at " + getTokenErrorDisplay(t);
throw new RecognitionException(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
}
}
If all you want to do is report errors, then you are probably looking for the ANTLRErrorListener interface, not the AntlrErrorStrategy interface. The latter is geared towards actually modifying the behavior of the parser in response to errors, e.g. for automatic recovery attempts.
In ANTLRWorks 2, I use the following two classes as my primary implementations of this interface:
SyntaxErrorListener
DescriptiveErrorListener
I get an unhandled Exception type error for the following code, even though, as I understand it, I have handled the exception in the catch block.
class NewException extends Exception{
private String msg;
public NewException(String msg){
this.msg = msg;
}
public String getExceptionMsg(){
return msg;
}}
class CatchException {
public static void method () throws NewException{
try {
throw new NewException("New exception thrown");
}
catch (NewException e){
e.printStackTrace();
System.out.println(e.getExceptionMsg());
}
finally {
System.out.println("In finally");
}
}}
public class TestExceptions{
public static void main(String[] args){
CatchException.method();
}}
Your method() declares that it throws NewException. Whatever is inside that method is irrelevant:
public static void method () throws NewException{
//...
}}
public static void main(String[] args){
CatchException.method();
}}
The compiler sees that you are calling CatchException.method() in main() and that you are not handling it in any way (either catching or declaring main() to throw NewException as well. Thus the error.
The compiler doesn't care if you are actually throwing that exception or not. Have a look at ByteArrayInputStream.close() - there is no way it'll ever throw an IOException - but you still have to handle it since it's declared.