Example usage for java.lang Exception getCause

List of usage examples for java.lang Exception getCause

Introduction

In this page you can find the example usage for java.lang Exception getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.bbm.common.aspect.ExceptionTransfer.java

/**
 * ? Exception ? ?  ??   ??  ? .//from w w  w. j  a v  a 2s.  c om
 * 
 * @param clazz Exception ? ? 
 * @param methodName Exception ?  
 * @param exception ? Exception 
 * @param pm ? PathMatcher(default : AntPathMatcher) 
 * @param exceptionHandlerServices[] ??  ExceptionHandlerService 
 */
protected void processHandling(Class clazz, String methodName, Exception exception, PathMatcher pm,
        ExceptionHandlerService[] exceptionHandlerServices) {
    try {
        for (ExceptionHandlerService ehm : exceptionHandlerServices) {

            if (!ehm.hasReqExpMatcher())
                ehm.setReqExpMatcher(pm);

            ehm.setPackageName(clazz.getCanonicalName() + "." + methodName);
            ehm.run(exception);

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        getLog(clazz).error(e.getMessage(), e.getCause());
    }
}

From source file:cn.afterturn.easypoi.excel.export.ExcelExportService.java

public void createSheet(Workbook workbook, ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Excel export start ,class is {}", pojoClass);
        LOGGER.debug("Excel version is {}", entity.getType().equals(ExcelType.HSSF) ? "03" : "07");
    }//from  ww w . j a v  a 2  s .c  o  m
    if (workbook == null || entity == null || pojoClass == null || dataSet == null) {
        throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
    }
    try {
        List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
        // 
        Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
        ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
        String targetId = etarget == null ? null : etarget.value();
        getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, null, null);
        //???,??
        createSheetForMap(workbook, entity, excelParams, dataSet);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
    }
}

From source file:org.talend.dataprep.command.GenericCommandTest.java

@Test
public void testFail_With_400() throws Exception {
    // Given//from   w  w w .j a v a  2  s  .c  o  m
    GenericCommand<String> command = getCommand("http://localhost:" + port + "/command/test/fail_with_400",
            GenericCommandTest::error);
    try {
        // When
        command.run();
    } catch (Exception e) {
        // Then
        // underlying was wrapped in another exception by error() method
        assertThat(e.getCause(), is(lastException));
        // underlying exception is a TDPException
        assertThat(lastException, isA(TDPException.class));
        // Underlying exception is expected to be MISSING_ACTION_SCOPE
        assertThat(lastException.getCode().getCode(), is(BaseErrorCodes.MISSING_ACTION_SCOPE.getCode()));
        // And status is 400.
        assertThat(lastException.getCode().getHttpStatus(), is(400));
    }
}

From source file:org.talend.dataprep.command.GenericCommandTest.java

@Test
public void testFail_With_404() throws Exception {
    // Given//from w  w w.ja  va2s . co m
    GenericCommand<String> command = getCommand("http://localhost:" + port + "/command/test/not_found",
            GenericCommandTest::error);
    try {
        // When
        command.run();
    } catch (Exception e) {
        // Then
        // underlying was wrapped in another exception by error() method
        assertThat(e.getCause(), is(lastException));
        // underlying exception is a TDPException
        assertThat(lastException, isA(TDPException.class));
        // Underlying exception is expected to be UNEXPECTED_EXCEPTION
        assertThat(lastException.getCode().getCode(), is(CommonErrorCodes.UNEXPECTED_EXCEPTION.getCode()));
        // and thrown because of a 404 error
        assertThat(lastException.getCode().getHttpStatus(), is(404));
    }
}

From source file:ProxyAdapter.java

@SuppressWarnings("unchecked")
public ProxyAdapter(Class... interfaces) {
    proxy = (T) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, new InvocationHandler() {
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Method m;//  ww w  .ja va  2  s  . c  o m
            try {
                //Determine if the method has been defined in a subclass
                m = ProxyAdapter.this.getClass().getMethod(method.getName(), method.getParameterTypes());
                m.setAccessible(true);
            } catch (Exception e) { //if not found
                throw new UnsupportedOperationException(method.toString(), e);

            }
            //Invoke the method found and return the result
            try {
                return m.invoke(ProxyAdapter.this, args);
            } catch (InvocationTargetException e) {
                throw e.getCause();
            }
        }
    });
}

From source file:org.talend.dataprep.command.GenericCommandTest.java

@Test
public void testFail_With_500() throws Exception {
    // Given/*from w w  w .  j  ava  2s.  c  o m*/
    GenericCommand<String> command = getCommand("http://localhost:" + port + "/command/test/fail_with_500",
            GenericCommandTest::error);
    try {
        // When
        command.run();
    } catch (Exception e) {
        // Then
        // underlying was wrapped in another exception by error() method
        assertThat(e.getCause(), is(lastException));
        // underlying exception is a TDPException
        assertThat(lastException, isA(TDPException.class));
        // Underlying exception is expected to be UNABLE_TO_SERIALIZE_TO_JSON
        assertThat(lastException.getCode().getCode(),
                is(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON.getCode()));
        // And status is 500.
        assertThat(lastException.getCode().getHttpStatus(), is(500));
    }
}

From source file:org.talend.dataprep.command.GenericCommandTest.java

@Test
public void testFail_With_Unknown() throws Exception {
    // Given/*from  w ww .  ja va2 s.c om*/
    GenericCommand<String> command = getCommand("http://localhost:" + port + "/command/test/fail_with_unknown",
            GenericCommandTest::error);
    try {
        // When
        command.run();
    } catch (Exception e) {
        // Then
        // underlying was wrapped in another exception by error() method
        assertThat(e.getCause(), is(lastException));
        // underlying exception is a TDPException
        assertThat(lastException, isA(TDPException.class));
        // Underlying exception is expected to be UNEXPECTED_EXCEPTION
        assertThat(lastException.getCode().getProduct(),
                is(CommonErrorCodes.UNEXPECTED_EXCEPTION.getProduct()));
        assertThat(lastException.getCode().getCode(), is(CommonErrorCodes.UNEXPECTED_EXCEPTION.getCode()));
        // And status is 418.
        assertThat(lastException.getCode().getHttpStatus(), is(418));
    }
}

From source file:edu.vt.middleware.ldap.servlets.LoginServlet.java

/**
 * Handle all requests sent to this servlet.
 *
 * @param  request  <code>HttpServletRequest</code>
 * @param  response  <code>HttpServletResponse</code>
 *
 * @throws  ServletException  if this request cannot be serviced
 * @throws  IOException  if a response cannot be sent
 *///from   w w  w.ja v  a  2 s.c om
public void service(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
    boolean validCredentials = false;
    String user = request.getParameter(ServletConstants.USER_PARAM);
    if (user != null) {
        user = user.trim().toLowerCase();
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Received user param = " + user);
    }

    final String credential = request.getParameter(ServletConstants.CREDENTIAL_PARAM);
    String url = request.getParameter(ServletConstants.URL_PARAM);
    if (url == null) {
        url = "";
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Received url param = " + url);
    }

    final StringBuffer error = new StringBuffer(this.errorMsg);

    try {
        if (this.auth.authenticate(user, credential)) {
            validCredentials = true;
        }
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error authenticating user " + user, e);
        }
        if (e.getCause() != null && e.getCause().getMessage() != null
                && !e.getCause().getMessage().equals("null")) {
            error.append(": ").append(e.getCause().getMessage());
        } else if (e.getMessage() != null && !e.getMessage().equals("null")) {
            error.append(": ").append(e.getMessage());
        }
    }

    if (validCredentials) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Authentication succeeded for user " + user);
        }
        try {
            this.sessionManager.login(request.getSession(true), user);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Initialized session for user " + user);
            }
            response.sendRedirect(url);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Redirected user to " + url);
            }
            return;
        } catch (Exception e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Error authorizing user " + user, e);
            }
            if (e.getCause() != null && e.getCause().getMessage() != null
                    && !e.getCause().getMessage().equals("null")) {
                error.append(": ").append(e.getCause().getMessage());
            } else if (e.getMessage() != null && !e.getMessage().equals("null")) {
                error.append(": ").append(e.getMessage());
            }
        }
    }

    final StringBuffer errorUrl = new StringBuffer(this.loginUrl);
    if (error != null) {
        errorUrl.append("?error=").append(URLEncoder.encode(error.toString(), "UTF-8"));
    }
    if (user != null) {
        errorUrl.append("&user=").append(URLEncoder.encode(user, "UTF-8"));
    }
    if (url != null) {
        errorUrl.append("&url=").append(URLEncoder.encode(url, "UTF-8"));
    }
    response.sendRedirect(errorUrl.toString());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Redirected user to " + errorUrl.toString());
    }
}

From source file:fr.paris.lutece.util.sql.Transaction.java

/**
 * Rollback the transaction//from  w  w  w .j  a  va2 s . co  m
 * @param e The exception that cause the rollback
 */
public void rollback(Exception e) {
    if (e != null) {
        _logger.error("Transaction Error - Rollback in progress " + e.getMessage(), e.getCause());
    }

    try {
        if (_connection != null) {
            _connection.rollback();
            _logger.debug("Plugin : '" + _strPluginName + "' - ROLLBACK TRANSACTION");
        } else {
            _logger.debug("Plugin : '" + _strPluginName + "' - TRANSACTION HAS ALREADY BEEN ROLLED BACK");
        }
    } catch (SQLException ex) {
        _logger.error("Transaction Error - Rollback error : " + ex.getMessage(), ex.getCause());
    } finally {
        closeTransaction(ROLLEDBACK);
    }
}

From source file:org.geogit.rest.dispatch.GeogitDispatcher.java

@Override
public ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    try {//from   w ww. ja  v  a 2  s. com
        converter.service(req, resp);
    } catch (Exception e) {
        RestletException re = null;
        if (e instanceof RestletException) {
            re = (RestletException) e;
        }
        if (re == null && e.getCause() instanceof RestletException) {
            re = (RestletException) e.getCause();
        }

        if (re != null) {
            resp.setStatus(re.getStatus().getCode());

            String reStr = re.getRepresentation().getText();
            if (reStr != null) {
                LOG.severe(reStr);
                resp.setContentType("text/plain");
                resp.getOutputStream().write(reStr.getBytes());
            }

            // log the full exception at a higher level
            LOG.log(Level.SEVERE, "", re);
        } else {
            LOG.log(Level.SEVERE, "", e);
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

            if (e.getMessage() != null) {
                resp.getOutputStream().write(e.getMessage().getBytes());
            }
        }
        resp.getOutputStream().flush();
    }

    return null;
}