Java tutorial
/* * This file is a component of thundr, a software library from 3wks. * Read more: http://3wks.github.io/thundr/ * Copyright (C) 2015 3wks, <thundr@3wks.com.au> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.threewks.thundr.servlet; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import com.threewks.thundr.http.StatusCode; import com.threewks.thundr.request.Response; import com.threewks.thundr.view.ViewResolutionException; import com.threewks.thundr.view.exception.ExceptionViewResolver; public class ServletExceptionViewResolver extends ExceptionViewResolver { @Override protected void renderException(Response resp, Throwable exceptionOfInterest) { HttpServletResponse rawResponse = resp.getRawResponse(HttpServletResponse.class); if (rawResponse == null) { super.renderException(resp, exceptionOfInterest); } else { sendError(exceptionOfInterest, rawResponse); } } protected void sendError(Throwable exceptionOfInterest, HttpServletResponse rawResponse) { try { String message = exceptionOfInterest == null ? "" : StringUtils.trimToEmpty(exceptionOfInterest.getMessage()); rawResponse.sendError(StatusCode.InternalServerError.getCode(), message); } catch (IOException e) { throw new ViewResolutionException(e, "Failed to send error to client: %s", e.getMessage()); } } @Override public String toString() { return this.getClass().getSimpleName(); } }