3

I've got a class that implements the onRender(Canvas.RenderContext renderContext) method of Canvas.CanvasLifecycleHandler. It has a corresponding test class that uses the Canvas.Test methods to set mock context values and to invoke the onRender() method.

If an exception is thrown in the onRender() method it appears to be swallowed and not reported back to the test class. This includes causing null reference exceptions, throwing custom exceptions, and making assertions that fail. The exceptions appear in the logs as fatal exceptions, but nothing appears in the actual test case.

How can I detect and then handle exceptions that are occurring in the onRender() method?

1 Answer 1

2

It turns out that the Canvas.CanvasRenderException is the only exception that will emerge from the onRender() method.

As such, I found it best to wrap the entire body of the onRender() method in something like:

public void onRender(Canvas.RenderContext renderContext) {

    try {
        Canvas.ApplicationContext app = renderContext.getApplicationContext();
        // ... entire onRender implementation.
    } catch (Canvas.CanvasRenderException ex) {
        // Pass this exception straight through as it is already the required type.
        // No need to throw a new exception.
        throw ex;
    } catch (Exception ex) {
        throw new Canvas.CanvasRenderException(ex.getMessage());
    }
}

It won't help with exceptions that can't be caught, so avoid making assertions within the method.

However, now exceptions will appear in the UI and in test cases.


Update: Note that all stacktraces coming out of Canvas.Test.testCanvasLifecycle() appear to start from:

Class.Canvas.Test.testCanvasLifecycle: line 30, column 1

regardless of which line in the implementation thru the exception.

3
  • Why is the middle catch block necessary?
    – Adrian Larson
    Sep 19, 2016 at 21:09
  • My intention was to catch and pass on instances of Canvas.CanvasRenderException without altering the stack trace. I didn't want everything to go into the generic Exception handling to throw a new exception. In .NET I could just do a throw;. Then in wouldn't be a new exception. In my initial test cases the above code seems to be preserving the stacktrace so I can quickly find the source line causing the problem. Sep 19, 2016 at 21:18
  • I'll see if throw new Canvas.CanvasRenderException(ex.getMessage(), ex); works better. Sep 19, 2016 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .