Example usage for javax.servlet RequestDispatcher ERROR_REQUEST_URI

List of usage examples for javax.servlet RequestDispatcher ERROR_REQUEST_URI

Introduction

In this page you can find the example usage for javax.servlet RequestDispatcher ERROR_REQUEST_URI.

Prototype

String ERROR_REQUEST_URI

To view the source code for javax.servlet RequestDispatcher ERROR_REQUEST_URI.

Click Source Link

Document

The name of the request attribute under which the request URI whose processing caused the error is propagated during an error dispatch

Usage

From source file:com.cloudera.oryx.lambda.serving.ErrorResourceTest.java

@Test
public void testError() {
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 500);
    mockRequest.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, "http://foo/bar");
    mockRequest.setAttribute(RequestDispatcher.ERROR_MESSAGE, "Something was wrong");
    mockRequest.setAttribute(RequestDispatcher.ERROR_EXCEPTION, new IllegalStateException());
    testResponse(new ErrorResource().errorHTML(mockRequest), false);
    testResponse(new ErrorResource().errorText(mockRequest), false);
    testResponse(new ErrorResource().errorEmpty(mockRequest), true);
}

From source file:ru.mystamps.web.controller.ErrorController.java

@RequestMapping(Url.NOT_FOUND_PAGE)
public void notFound(HttpServletRequest request, @CurrentUser Integer currentUserId,
        // CheckStyle: ignore LineLength for next 1 line
        @RequestAttribute(name = RequestDispatcher.ERROR_REQUEST_URI, required = false) String page,
        @RequestHeader(name = "referer", required = false) String referer,
        @RequestHeader(name = "user-agent", required = false) String agent) {

    // TODO: sanitize all user's values (#60)
    String ip = request.getRemoteAddr();
    String method = request.getMethod();

    siteService.logAboutAbsentPage(page, method, currentUserId, ip, referer, agent);
}

From source file:ru.mystamps.web.controller.ErrorController.java

@RequestMapping(Url.INTERNAL_ERROR_PAGE)
public void internalError(
        // CheckStyle: ignore LineLength for next 3 lines
        @RequestAttribute(name = RequestDispatcher.ERROR_EXCEPTION_TYPE, required = false) Class<?> exceptionType,
        @RequestAttribute(name = RequestDispatcher.ERROR_EXCEPTION, required = false) Exception exception,
        @RequestAttribute(name = RequestDispatcher.ERROR_REQUEST_URI, required = false) String page) {

    // TODO: log to database (with *.status_code, *.message, *.servlet_name and user details)

    if (page != null && !Url.INTERNAL_ERROR_PAGE.equals(page)) {
        String msg = String.format("Exception '%s' occurred at page %s", getNameOrAsIs(exceptionType), page);
        LOG.error(msg, exception);/*from  w  w w  .j  av a  2  s .c  o m*/
    }
}

From source file:org.tec.webapp.web.ErrorServlet.java

/**
 * process the error//from w  w w.j a  va  2 s. co  m
 * @param request the request instance
 * @param response the response instance
 * @throws IOException if processing fails
 */
protected void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
    Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
    String requestUri = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);

    LOGGER.error("failed to process " + requestUri + " error code: " + statusCode);

    WebError we;
    if (throwable != null) {
        LOGGER.error("error", throwable);
        we = new WebError(throwable.getMessage(), ErrorCodes.UNRESOLVEABLE_ERROR);
    } else {
        we = new WebError("error", ErrorCodes.UNRESOLVEABLE_ERROR);
    }

    PrintWriter pw = null;
    try {
        response.setStatus(statusCode != null ? statusCode : HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
        pw = response.getWriter();
        pw.write(we.toJSON());
        pw.flush();
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
}

From source file:myfeed.user.ApiDocumentation.java

@Test
public void errorExample() throws Exception {
    this.mockMvc/*from ww  w.  j  a  v a  2  s.c  o m*/
            .perform(get("/error").requestAttr(RequestDispatcher.ERROR_STATUS_CODE, 400)
                    .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, "/notes")
                    .requestAttr(RequestDispatcher.ERROR_MESSAGE,
                            "The tag 'http://localhost:8080/tags/123' does not exist"))
            .andDo(print()).andExpect(status().isBadRequest()).andExpect(jsonPath("error", is("Bad Request")))
            .andExpect(jsonPath("timestamp", is(notNullValue()))).andExpect(jsonPath("status", is(400)))
            .andExpect(jsonPath("path", is(notNullValue()))).andDo(document("error-example"));
}

From source file:io.spring.initializr.web.test.MockMvcClientHttpRequestFactory.java

@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    return new MockClientHttpRequest(httpMethod, uri) {
        @Override//from  w w  w.  ja  v a 2s.  c o  m
        public ClientHttpResponse executeInternal() throws IOException {
            try {
                MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString());
                requestBuilder.content(getBodyAsBytes());
                requestBuilder.headers(getHeaders());
                MockHttpServletResponse servletResponse = actions(requestBuilder).andReturn().getResponse();
                HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
                if (status.value() >= 400) {
                    requestBuilder = request(HttpMethod.GET, "/error")
                            .requestAttr(RequestDispatcher.ERROR_STATUS_CODE, status.value())
                            .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, uri.toString());
                    if (servletResponse.getErrorMessage() != null) {
                        requestBuilder.requestAttr(RequestDispatcher.ERROR_MESSAGE,
                                servletResponse.getErrorMessage());
                    }
                    // Overwrites the snippets from the first request
                    servletResponse = actions(requestBuilder).andReturn().getResponse();
                }
                byte[] body = servletResponse.getContentAsByteArray();
                HttpHeaders headers = getResponseHeaders(servletResponse);
                MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
                clientResponse.getHeaders().putAll(headers);
                return clientResponse;
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

    };
}

From source file:com.example.ApiDocumentation.java

@Test
public void error() throws Exception {
    this.mockMvc//www  .ja v  a  2 s. c  o m
            .perform(get("/error").requestAttr(RequestDispatcher.ERROR_STATUS_CODE, 400)
                    .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, "/orders")
                    .requestAttr(RequestDispatcher.ERROR_MESSAGE, "The order cannot be created."))
            .andExpect(status().isBadRequest()).andExpect(jsonPath("error", is("Bad Request")))
            .andExpect(jsonPath("timestamp", is(notNullValue()))).andExpect(jsonPath("status", is(400)))
            .andExpect(jsonPath("path", is(notNullValue())))
            .andDo(this.documentationHandler.document(responseFields(
                    fieldWithPath("error").description("The HTTP error that occurred, e.g. `Bad Request`"),
                    fieldWithPath("message").description("A description of the cause of the error"),
                    fieldWithPath("path").description("The path to which the request was made"),
                    fieldWithPath("status").description("The HTTP status code, e.g. `400`"),
                    fieldWithPath("timestamp")
                            .description("The time, in milliseconds, at which the error occurred"))));
}

From source file:edu.pitt.dbmi.ccd.anno.CCDAnnotationsTest.java

@Test
public void errorExample() throws Exception {
    this.mockMvc/*from  www . j ava2  s .c  o  m*/
            .perform(get("/error").requestAttr(RequestDispatcher.ERROR_STATUS_CODE, 404)
                    .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, "/test")
                    .requestAttr(RequestDispatcher.ERROR_MESSAGE, "Not found"))
            .andExpect(status().isNotFound()).andExpect(jsonPath("timestamp", is(notNullValue())))
            .andExpect(jsonPath("status", is(404))).andExpect(jsonPath("error", is("Not Found")))
            .andExpect(jsonPath("message", is("Not found"))).andExpect(jsonPath("path", is(notNullValue())))
            .andDo(this.documentationResultHandler.document(
                    responseHeaders(headerWithName("Content-Type")
                            .description("The Content-Type of the payload, e.g. `application/hal+json`")),
                    responseFields(
                            fieldWithPath("timestamp").description("The time at which the error occurred"),
                            fieldWithPath("status").description("The HTTP status code"),
                            fieldWithPath("error").description("The HTTP error"),
                            fieldWithPath("message").description("A description of what caused the error"),
                            fieldWithPath("path").description("The path to which the request was made"))));
}

From source file:com.example.notes.ApiDocumentation.java

@Test
public void errorExample() throws Exception {
    this.mockMvc// w  w w  .  j  a  v  a  2  s.  c  o m
            .perform(get("/error").requestAttr(RequestDispatcher.ERROR_STATUS_CODE, 400)
                    .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, "/notes")
                    .requestAttr(RequestDispatcher.ERROR_MESSAGE,
                            "The tag 'http://localhost:8080/tags/123' does not exist"))
            .andExpect(status().isBadRequest()).andExpect(jsonPath("error", is("Bad Request")))
            .andExpect(jsonPath("timestamp", is(notNullValue()))).andExpect(jsonPath("status", is(400)))
            .andExpect(jsonPath("path", is(notNullValue())))
            .andDo(this.documentationHandler.document(responseFields(
                    fieldWithPath("error").description("The HTTP error that occurred, e.g. `Bad Request`"),
                    fieldWithPath("message").description("A description of the cause of the error"),
                    fieldWithPath("path").description("The path to which the request was made"),
                    fieldWithPath("status").description("The HTTP status code, e.g. `400`"),
                    fieldWithPath("timestamp")
                            .description("The time, in milliseconds, at which the error occurred"))));
}