You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Sergiy Kyrylkov <se...@cs.unm.edu> on 2005/09/22 21:24:29 UTC

Email on exception

Hi,

Is it possible to extend Tapestry exception handling mechanism in an easy
way to send exception report by e-mail to a specified address every time an
exception occurs?

Sergiy


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Email on exception

Posted by Mind Bridge <mi...@yahoo.com>.
This is exactly what we have done.

Define your own Exception page. Mke it have an "exception" property to 
accept the exception that has occurred, e.g. by defining 
setException(Throwable). In that way you can both have your own 
appearance when an exception occurs, and perform you own actions as well.

See the standard Exception page in org.apache.tapestry.pages for an example.

Sergiy Kyrylkov wrote:

>Hi,
>
>Is it possible to extend Tapestry exception handling mechanism in an easy
>way to send exception report by e-mail to a specified address every time an
>exception occurs?
>
>Sergiy
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Email on exception

Posted by Henri Dupre <he...@gmail.com>.
So what I do is that I email myself a tapestry exception page. It has
been incredibly usefull for tracking bugs!

  /**
   * Sets the Exception page's exception property, then renders the Exception
   * page.
   * <p>
   * If the render throws an exception, then copious output is sent to
   * <code>System.err</code> and a {@link ServletException} is thrown.
   */
  @Override
  protected void activateExceptionPage(IRequestCycle cycle,
ResponseOutputStream output,
      Throwable cause) throws ServletException {
    try {
      Throwable realcause = cause.getCause();
      if (realcause != null)
        s_logger.error("Error processing request:", cause.getCause());
      else
        s_logger.error("Error processing request:", cause);

      String expPage;
      if (doDebug())
        expPage = getExceptionPageName();
      else
        expPage = "ErreurGenerale";

      if (!doDebug()) {
        String pageName = "-";
        if (cycle.getPage()!= null)
          pageName = cycle.getPage().getPageName();
        IPage defaultPage = cycle.getPage(getExceptionPageName());
        String message = cause.toString();
        if (cause.getMessage() != null)
          message = cause.getMessage();
        defaultPage.setProperty("exception", cause);
        Email.sendEmail(null, defaultPage, cycle, "source-email@email.com",
            "target-email@email.com", "Page:" + pageName + " " + message);
      }

      IPage exceptionPage = cycle.getPage(expPage);

      exceptionPage.setProperty("exception", cause);
      cycle.activate(exceptionPage);
      renderResponse(cycle, output);
    } catch (Throwable ex) {
      // Worst case scenario. The exception page itself is broken, leaving
      // us with no option but to write the cause to the output.

      reportException(Tapestry
          .getMessage("AbstractEngine.unable-to-process-client-request"),
cause);

      // Also, write the exception thrown when redendering the exception
      // page, so that can get fixed as well.

      reportException(Tapestry
          .getMessage("AbstractEngine.unable-to-present-exception-page"), ex);

      // And throw the exception.

      throw new ServletException(ex.getMessage(), ex);
    }
  }

and
 /**
   * @param cycle
   * @param from
   * @param recipient
   * @param subject
   */
  public static void sendEmail(IPage source, IPage page, IRequestCycle cycle,
      String from, String recipient, String subject) {
    IForm cform = Form.get(cycle);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    HTMLWriter writer = new HTMLWriter(baos);
    cycle.activate(page);
    cycle.renderPage(writer);
    writer.flush();
    String semail = baos.toString();
    semail = semail.replaceAll("img src=\"/", "img src=\"http://"
        + cycle.getRequestContext().getServerName()
        + ((cycle.getRequestContext().getServerPort() == 80) ? "" : (":" + cycle
            .getRequestContext().getServerPort())) + "/");
    semail = semail.replaceAll("href=\"/", "href=\"http://"
        + cycle.getRequestContext().getServerName()
        + ((cycle.getRequestContext().getServerPort() == 80) ? "" : (":" + cycle
            .getRequestContext().getServerPort())) + "/");
    Context initCtx;
    try {
      initCtx = new InitialContext();

      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      Session session = (Session) envCtx.lookup("mail/Session");

      MimeMultipart content = new MimeMultipart("mixed/alternative");
      MimeBodyPart html = new MimeBodyPart();

      content.addBodyPart(html);

      Message message = new MimeMessage(session);
      message.setContent(semail, "text/html;charset=\"UTF-8\"");
      message.setFrom(new InternetAddress(from));
      InternetAddress to[] = new InternetAddress[1];
      to[0] = new InternetAddress(recipient);
      message.setRecipients(Message.RecipientType.TO, to);
      message.setSubject(subject);
      message.setSentDate(new Date());

      Transport.send(message);
      s_logger.debug("Message sent from " + from + " to " + recipient
+ ":" + subject);

    } catch (NamingException e) {
      s_logger.debug(e);
    } catch (AddressException e) {
      s_logger.debug(e);
    } catch (MessagingException e) {
      s_logger.warn(e);
    }
    page.detach();

    if (source != null) {
      cycle.activate(source);
      cycle.setAttribute(IForm.ATTRIBUTE_NAME, cform);
    }
  }

On 9/23/05, Andrey Tkach <tk...@sac.com.ua> wrote:
> Yep, it's interesting. Could you please post your code here (with an ant
> build file)
>
> Andrey
>
> >
> > We are using that with Tapestry 3. I can post my code if you are
> > interested.
> > I have configured our ant build with 2 targets dev and production, on
> > dev the exceptions are showed normally but on prod, another page is
> > displayed and an email is sent.
> >
> > Henri.
> >
> > On 9/22/05, Sergiy Kyrylkov <se...@cs.unm.edu> wrote:
> > > Hi,
> > >
> > > Is it possible to extend Tapestry exception handling mechanism in an
> > easy
> > > way to send exception report by e-mail to a specified address every time
> > an
> > > exception occurs?
> > >
> > > Sergiy
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> > >
> > >
> >
> >
> > --
> > Thanks,
> >
> > Henri.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>


--
Thanks,

Henri.

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Email on exception

Posted by Leonardo Quijano Vincenzi <le...@dtqsoftware.com>.
You can also use log4j's e-mailing capabilities and set an log.error("") 
on your Tapestry.
That way you don't have to reimplement the error reporting (log4j even 
includes some previous debug / info logging messages as a nice feature).

-- 
Ing. Leonardo Quijano Vincenzi
Director Técnico
DTQ Software


Andrey Tkach escribió:

>Yep, it's interesting. Could you please post your code here (with an ant
>build file)
>
>Andrey
>
>  
>
>>We are using that with Tapestry 3. I can post my code if you are
>>interested.
>>I have configured our ant build with 2 targets dev and production, on
>>dev the exceptions are showed normally but on prod, another page is
>>displayed and an email is sent.
>>
>>Henri.
>>
>>    
>>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Email on exception

Posted by Andrey Tkach <tk...@sac.com.ua>.
Yep, it's interesting. Could you please post your code here (with an ant
build file)

Andrey

> 
> We are using that with Tapestry 3. I can post my code if you are
> interested.
> I have configured our ant build with 2 targets dev and production, on
> dev the exceptions are showed normally but on prod, another page is
> displayed and an email is sent.
> 
> Henri.
> 
> On 9/22/05, Sergiy Kyrylkov <se...@cs.unm.edu> wrote:
> > Hi,
> >
> > Is it possible to extend Tapestry exception handling mechanism in an
> easy
> > way to send exception report by e-mail to a specified address every time
> an
> > exception occurs?
> >
> > Sergiy
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
> 
> 
> --
> Thanks,
> 
> Henri.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Email on exception

Posted by Henri Dupre <he...@gmail.com>.
We are using that with Tapestry 3. I can post my code if you are interested.
I have configured our ant build with 2 targets dev and production, on
dev the exceptions are showed normally but on prod, another page is
displayed and an email is sent.

Henri.

On 9/22/05, Sergiy Kyrylkov <se...@cs.unm.edu> wrote:
> Hi,
>
> Is it possible to extend Tapestry exception handling mechanism in an easy
> way to send exception report by e-mail to a specified address every time an
> exception occurs?
>
> Sergiy
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>


--
Thanks,

Henri.

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org