001package edu.pdx.cs410J.grader.poa; 002 003import com.google.common.eventbus.EventBus; 004import com.google.common.eventbus.Subscribe; 005import com.google.inject.Inject; 006import com.google.inject.Singleton; 007 008import java.io.IOException; 009import java.io.PrintWriter; 010import java.io.StringWriter; 011 012@Singleton 013public class UnhandledExceptionPresenter { 014 private final UnhandledExceptionView view; 015 016 @Inject 017 public UnhandledExceptionPresenter(EventBus bus, UnhandledExceptionView view) { 018 this.view = view; 019 bus.register(this); 020 } 021 022 @Subscribe 023 public void populateViewFrom(UnhandledExceptionEvent event) throws IOException { 024 Throwable exception = event.getUnhandledException(); 025 this.view.setExceptionMessage(exception.getMessage()); 026 this.view.setExceptionDetails(getStackTrace(exception)); 027 this.view.displayView(); 028 } 029 030 private String getStackTrace(Throwable exception) throws IOException { 031 try (StringWriter sw = new StringWriter()) { 032 exception.printStackTrace(new PrintWriter(sw)); 033 sw.flush(); 034 return sw.toString(); 035 } 036 } 037}