@Test( expected = NullPointerException.class ) public void testGetSwitchInfo() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test public void asyncRequestThatThrowsUncheckedException() throws Exception { final MvcResult result = mvc.perform(get("/api/c1/completableFutureException")) .andExpect(request().asyncStarted()) .andReturn(); // once the async dispatch is complete, it should no longer contribute to the activeTasks count assertThat(registry.find("my.long.request.exception") .longTaskTimer() .activeTasks()) .isEqualTo(1); assertThatExceptionOfType(NestedServletException.class) .isThrownBy(() -> mvc.perform(asyncDispatch(result))) .withRootCauseInstanceOf(RuntimeException.class); assertThat(registry.find("http.server.requests") .tags("uri", "/api/c1/completableFutureException").timer().count()).isEqualTo(1); // once the async dispatch is complete, it should no longer contribute to the activeTasks count assertThat(registry.find("my.long.request.exception") .longTaskTimer().activeTasks()).isEqualTo(0); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { chain.doFilter(request, response); } catch (Throwable e) { if (e instanceof IOException || (e instanceof ServletException && !(e instanceof NestedServletException))) throw e; if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { processException(e, (HttpServletRequest) request, (HttpServletResponse) response); return; } throw e; } }
/** * Processes the incoming Burlap request and creates a Burlap response. */ @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!"POST".equals(request.getMethod())) { throw new HttpRequestMethodNotSupportedException(request.getMethod(), new String[] {"POST"}, "BurlapServiceExporter only supports POST requests"); } try { invoke(request.getInputStream(), response.getOutputStream()); } catch (Throwable ex) { throw new NestedServletException("Burlap skeleton invocation failed", ex); } }
/** * Processes the incoming Hessian request and creates a Hessian response. */ @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!"POST".equals(request.getMethod())) { throw new HttpRequestMethodNotSupportedException(request.getMethod(), new String[] {"POST"}, "HessianServiceExporter only supports POST requests"); } response.setContentType(CONTENT_TYPE_HESSIAN); try { invoke(request.getInputStream(), response.getOutputStream()); } catch (Throwable ex) { throw new NestedServletException("Hessian skeleton invocation failed", ex); } }
/** * Merge the template with the context. * Can be overridden to customize the behavior. * @param template the template to merge * @param context the Velocity context to use for rendering * @param response servlet response (use this to get the OutputStream or Writer) * @throws Exception if thrown by Velocity * @see org.apache.velocity.Template#merge */ protected void mergeTemplate( Template template, Context context, HttpServletResponse response) throws Exception { try { template.merge(context, response.getWriter()); } catch (MethodInvocationException ex) { Throwable cause = ex.getWrappedThrowable(); throw new NestedServletException( "Method invocation failed during rendering of Velocity view with name '" + getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() + "], method '" + ex.getMethodName() + "'", cause==null ? ex : cause); } }
public ConcurrentResultHandlerMethod(final Object result, ConcurrentResultMethodParameter returnType) { super(new Callable<Object>() { @Override public Object call() throws Exception { if (result instanceof Exception) { throw (Exception) result; } else if (result instanceof Throwable) { throw new NestedServletException("Async processing failed", (Throwable) result); } return result; } }, CALLABLE_METHOD); setHandlerMethodReturnValueHandlers(ServletInvocableHandlerMethod.this.returnValueHandlers); this.returnType = returnType; }
@Test public void equivalentMappingsWithSameMethodName() throws Exception { initServlet(ChildController.class); servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/child/test"); request.addParameter("childId", "100"); MockHttpServletResponse response = new MockHttpServletResponse(); try { servlet.service(request, response); fail("Didn't fail with due to ambiguous method mapping"); } catch (NestedServletException ex) { assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getCause().getMessage().contains("doGet")); } }
@Test( expected = NullPointerException.class ) public void testGetSwitchBgpConfig() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/bgp" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testCreateOrUpdateSwitchBgpConfig() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/bgp" ).content( mapper.writeValueAsString( HmsAggregatorDummyDataProvider.getNBSwitchBgpConfig() ).getBytes() ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testDeleteSwitchBgpConfig() throws Throwable { try { this.mockMvc.perform( delete( "http://localhost:8080/napi/switches/S1/bgp" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchOspfv2Config() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/ospfv2" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testCreateOrUpdateSwitchOspfv2Config() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/ospfv2" ).content( mapper.writeValueAsString( HmsAggregatorDummyDataProvider.getNBSwitchOspfv2Config() ).getBytes() ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testDeleteSwitchOspfv2Config() throws Throwable { try { this.mockMvc.perform( delete( "http://localhost:8080/napi/switches/S1/ospfv2" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchMcLagConfig() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/mclag" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testDeleteSwitchMcLagConfig() throws Throwable { try { this.mockMvc.perform( delete( "http://localhost:8080/napi/switches/S1/mclag" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchLagConfig() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/lags/bd-test" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testCreateOrUpdateSwitchLagConfig() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/lags/" ).content( mapper.writeValueAsString( HmsAggregatorDummyDataProvider.getNBSwitchLagConfig() ).getBytes() ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testDeleteSwitchLagConfig() throws Throwable { try { this.mockMvc.perform( delete( "http://localhost:8080/napi/switches/S1/lags/bd-test" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchAllLagsConfigs() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/lags" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchVlanConfig() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/vlans/2011" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testCreateOrUpdateSwitchVlanConfig() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/vlans" ).content( mapper.writeValueAsString( HmsAggregatorDummyDataProvider.getNBSwitchVlanConfig() ).getBytes() ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testDeleteSwitchVlanConfig() throws Throwable { try { this.mockMvc.perform( delete( "http://localhost:8080/napi/switches/S1/vlans/2011" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchAllVlansConfigs() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/vlans" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testConfigureIpv4DefaultRoute() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/ipv4defaultroute?gateway=1.1.1.1&port=swp1" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testDeleteIpv4DefaultRoute() throws Throwable { try { this.mockMvc.perform( delete( "http://localhost:8080/napi/switches/S1/ipv4defaultroute" ).accept( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchPortInfo() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/ports/swp1" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testUpdateSwitchPortConfig() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/ports/swp1" ).content( mapper.writeValueAsString( HmsAggregatorDummyDataProvider.getNBSwitchPortConfig() ).getBytes() ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testApplyBulkConfigs() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/bulkconfigs" ).content( mapper.writeValueAsString( HmsAggregatorDummyDataProvider.getNBSwitchBulkConfigList() ).getBytes() ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetSwitchAllPortInfos() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/S1/ports" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testGetAllSwitchInfos() throws Throwable { try { this.mockMvc.perform( get( "http://localhost:8080/napi/switches/" ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
@Test( expected = NullPointerException.class ) public void testSetSwitchTime() throws Throwable { try { this.mockMvc.perform( put( "http://localhost:8080/napi/switches/S1/time?value=10000" ).accept( MediaType.APPLICATION_JSON ).contentType( MediaType.APPLICATION_JSON ) ).andReturn(); } catch ( NestedServletException e ) { assertNotNull( e ); assertNotNull( e.getCause() ); assertTrue( e.getCause() instanceof NullPointerException ); throw e.getCause(); } throw new Exception( "failed" ); }
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response); try { chain.doFilter(request, wrapped); if (wrapped.hasErrorToSend()) { handleErrorStatus(request, response, wrapped.getStatus(), wrapped.getMessage()); response.flushBuffer(); } else if (!request.isAsyncStarted() && !response.isCommitted()) { response.flushBuffer(); } } catch (Throwable ex) { Throwable exceptionToHandle = ex; if (ex instanceof NestedServletException) { exceptionToHandle = ((NestedServletException) ex).getRootCause(); } handleException(request, response, wrapped, exceptionToHandle); response.flushBuffer(); } }
@Test public void nestedServletExceptionIsUnwrapped() throws Exception { this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); throw new NestedServletException("Wrapper", new RuntimeException("BAD")); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()) .isEqualTo(500); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)) .isEqualTo(500); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)) .isEqualTo("BAD"); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)) .isEqualTo(RuntimeException.class.getName()); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)) .isEqualTo("/test/path"); assertThat(this.response.isCommitted()).isTrue(); assertThat(this.response.getForwardedUrl()).isEqualTo("/500"); }
@Test public void controllerMethodThatThrowsUnhandledException() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( Config.class, MetricFilterAutoConfiguration.class); Filter filter = context.getBean(Filter.class); MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()) .addFilter(filter).build(); try { mvc.perform(get("/unhandledException")) .andExpect(status().isInternalServerError()); } catch (NestedServletException ex) { // Expected } verify(context.getBean(CounterService.class)) .increment("status.500.unhandledException"); verify(context.getBean(GaugeService.class)) .submit(eq("response.unhandledException"), anyDouble()); context.close(); }
@RequestMapping("/error") String handleError(ModelMap map, HttpServletRequest request, HttpServletResponse response) { prepareMap(map, request); String message = ""; String stackTrace = ""; Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception"); if (exception instanceof NestedServletException && exception.getCause() instanceof Exception) { exception = (Exception) exception.getCause(); } if (exception != null) { if (exception.getMessage() != null) message = exception.getMessage(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (PrintWriter writer = new PrintWriter(bos)) { exception.printStackTrace(writer); } stackTrace = bos.toString(); stackTrace = stackTrace.replace(System.getProperty("line.separator"), "<br/>"); } map.put("message", message); map.put("stackTrace", stackTrace); map.put("status", response.getStatus()); return "error"; }