org.fenixedu.bennu.spring.resolvers.BennuSpringExceptionResolver.java Source code

Java tutorial

Introduction

Here is the source code for org.fenixedu.bennu.spring.resolvers.BennuSpringExceptionResolver.java

Source

/**
 * Copyright  2014 Instituto Superior Tcnico
 *
 * This file is part of Bennu Spring.
 *
 * Bennu Spring is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bennu Spring is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Bennu Spring.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fenixedu.bennu.spring.resolvers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.fenixedu.bennu.core.domain.exceptions.DomainException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;

/**
 * {@link HandlerExceptionResolver} that handles {@link DomainException}s, returning a status code and error message, as defined
 * by the exception.
 * 
 * @author Joo Carvalho (joao.pedro.carvalho@tecnico.ulisboa.pt)
 *
 */
public class BennuSpringExceptionResolver extends AbstractHandlerExceptionResolver {

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
            Object handler, Exception ex) {
        if (ex instanceof DomainException) {
            try {
                DomainException domainException = (DomainException) ex;
                response.sendError(domainException.getResponseStatus().getStatusCode(),
                        domainException.getLocalizedMessage());
                return new ModelAndView();
            } catch (IOException e) {
                logger.warn("Exception ocurred while handling BennuSpringException", e);
            }
        }
        return null;
    }

}