001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.error;
016
017import org.apache.hivemind.ApplicationRuntimeException;
018import org.apache.tapestry.IPage;
019import org.apache.tapestry.IRequestCycle;
020import org.apache.tapestry.services.ResponseRenderer;
021
022/**
023 * @author Howard M. Lewis Ship
024 * @since 4.0
025 */
026public class ExceptionPresenterImpl implements ExceptionPresenter
027{
028    private RequestExceptionReporter _requestExceptionReporter;
029
030    private ResponseRenderer _responseRenderer;
031
032    private String _exceptionPageName;
033
034    private boolean _verbose;
035
036    public void presentException(IRequestCycle cycle, Throwable cause)
037    {
038        try
039        {
040            IPage exceptionPage = cycle.getPage(_exceptionPageName);
041
042            exceptionPage.setProperty("exception", cause);
043
044            cycle.activate(exceptionPage);
045
046            _responseRenderer.renderResponse(cycle);
047        }
048        catch (Throwable ex)
049        {
050            // Worst case scenario. The exception page itself is broken, leaving
051            // us with no option but to write the cause to the output.
052
053            _requestExceptionReporter.reportRequestException(ErrorMessages
054                    .unableToProcessClientRequest(cause), cause);
055
056            // Also, write the exception thrown when redendering the exception
057            // page, so that can get fixed as well.
058
059            _requestExceptionReporter.reportRequestException(ErrorMessages
060                    .unableToPresentExceptionPage(ex), ex);
061
062            // And throw the exception.
063
064            throw new ApplicationRuntimeException(ex.getMessage(), ex);
065        }
066
067        if (_verbose)
068            _requestExceptionReporter.reportRequestException(ErrorMessages
069                    .unableToProcessClientRequest(cause), cause);
070    }
071
072    public void setExceptionPageName(String exceptionPageName)
073    {
074        _exceptionPageName = exceptionPageName;
075    }
076
077    public void setRequestExceptionReporter(RequestExceptionReporter requestExceptionReporter)
078    {
079        _requestExceptionReporter = requestExceptionReporter;
080    }
081
082    public void setResponseRenderer(ResponseRenderer responseRenderer)
083    {
084        _responseRenderer = responseRenderer;
085    }
086
087    public boolean isVerbose()
088    {
089        return _verbose;
090    }
091
092    public void setVerbose(boolean verbose)
093    {
094        _verbose = verbose;
095    }
096}