@Test public void assertHandleExceptionWithWrongToken() { WechatJobErrorHandler actual = getWechatJobErrorHandler(); actual.setWechatConfiguration(new WechatConfiguration(getHost() + "/send?key=wrongToken", 3000, 500)); setStaticFieldValue(actual); Throwable cause = new RuntimeException("test"); actual.handleException("test_job", cause); verify(log).error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: {}", "test_job", "token is invalid", cause); }
@Override public void handleException(final String jobName, final Throwable cause) { if (null == wechatConfiguration) { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ERROR_HANDLER_CONFIG); wechatConfiguration = YamlEngine.unmarshal(CONFIG_PREFIX, inputStream, WechatConfiguration.class); } HttpPost httpPost = new HttpPost(getUrl()); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(wechatConfiguration.getConnectTimeoutOrDefault()) .setSocketTimeout(wechatConfiguration.getReadTimeoutOrDefault()) .build(); httpPost.setConfig(requestConfig); StringEntity entity = new StringEntity(getParamJson(getMsg(jobName, cause)), StandardCharsets.UTF_8); entity.setContentEncoding(StandardCharsets.UTF_8.name()); entity.setContentType("application/json"); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { int status = response.getStatusLine().getStatusCode(); if (HttpURLConnection.HTTP_OK == status) { JsonObject resp = GsonFactory.getGson().fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: {}", jobName, resp.get("errmsg").getAsString(), cause); } else { log.error("An exception has occurred in Job '{}', Notification to wechat was successful.", jobName, cause); } } else { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: Unexpected response status: {}", jobName, status, cause); } } catch (IOException ex) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of", jobName, ex); } }
WechatJobErrorHandler implements JobErrorHandler { @Override public void handleException(final String jobName, final Throwable cause) { if (null == wechatConfiguration) { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ERROR_HANDLER_CONFIG); wechatConfiguration = YamlEngine.unmarshal(CONFIG_PREFIX, inputStream, WechatConfiguration.class); } HttpPost httpPost = new HttpPost(getUrl()); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(wechatConfiguration.getConnectTimeoutOrDefault()) .setSocketTimeout(wechatConfiguration.getReadTimeoutOrDefault()) .build(); httpPost.setConfig(requestConfig); StringEntity entity = new StringEntity(getParamJson(getMsg(jobName, cause)), StandardCharsets.UTF_8); entity.setContentEncoding(StandardCharsets.UTF_8.name()); entity.setContentType("application/json"); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { int status = response.getStatusLine().getStatusCode(); if (HttpURLConnection.HTTP_OK == status) { JsonObject resp = GsonFactory.getGson().fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: {}", jobName, resp.get("errmsg").getAsString(), cause); } else { log.error("An exception has occurred in Job '{}', Notification to wechat was successful.", jobName, cause); } } else { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: Unexpected response status: {}", jobName, status, cause); } } catch (IOException ex) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of", jobName, ex); } } }
WechatJobErrorHandler implements JobErrorHandler { @Override public void handleException(final String jobName, final Throwable cause) { if (null == wechatConfiguration) { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ERROR_HANDLER_CONFIG); wechatConfiguration = YamlEngine.unmarshal(CONFIG_PREFIX, inputStream, WechatConfiguration.class); } HttpPost httpPost = new HttpPost(getUrl()); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(wechatConfiguration.getConnectTimeoutOrDefault()) .setSocketTimeout(wechatConfiguration.getReadTimeoutOrDefault()) .build(); httpPost.setConfig(requestConfig); StringEntity entity = new StringEntity(getParamJson(getMsg(jobName, cause)), StandardCharsets.UTF_8); entity.setContentEncoding(StandardCharsets.UTF_8.name()); entity.setContentType("application/json"); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { int status = response.getStatusLine().getStatusCode(); if (HttpURLConnection.HTTP_OK == status) { JsonObject resp = GsonFactory.getGson().fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: {}", jobName, resp.get("errmsg").getAsString(), cause); } else { log.error("An exception has occurred in Job '{}', Notification to wechat was successful.", jobName, cause); } } else { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: Unexpected response status: {}", jobName, status, cause); } } catch (IOException ex) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of", jobName, ex); } } WechatJobErrorHandler(); }
WechatJobErrorHandler implements JobErrorHandler { @Override public void handleException(final String jobName, final Throwable cause) { if (null == wechatConfiguration) { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ERROR_HANDLER_CONFIG); wechatConfiguration = YamlEngine.unmarshal(CONFIG_PREFIX, inputStream, WechatConfiguration.class); } HttpPost httpPost = new HttpPost(getUrl()); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(wechatConfiguration.getConnectTimeoutOrDefault()) .setSocketTimeout(wechatConfiguration.getReadTimeoutOrDefault()) .build(); httpPost.setConfig(requestConfig); StringEntity entity = new StringEntity(getParamJson(getMsg(jobName, cause)), StandardCharsets.UTF_8); entity.setContentEncoding(StandardCharsets.UTF_8.name()); entity.setContentType("application/json"); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { int status = response.getStatusLine().getStatusCode(); if (HttpURLConnection.HTTP_OK == status) { JsonObject resp = GsonFactory.getGson().fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: {}", jobName, resp.get("errmsg").getAsString(), cause); } else { log.error("An exception has occurred in Job '{}', Notification to wechat was successful.", jobName, cause); } } else { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: Unexpected response status: {}", jobName, status, cause); } } catch (IOException ex) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of", jobName, ex); } } WechatJobErrorHandler(); @Override void handleException(final String jobName, final Throwable cause); @Override String getType(); }
WechatJobErrorHandler implements JobErrorHandler { @Override public void handleException(final String jobName, final Throwable cause) { if (null == wechatConfiguration) { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ERROR_HANDLER_CONFIG); wechatConfiguration = YamlEngine.unmarshal(CONFIG_PREFIX, inputStream, WechatConfiguration.class); } HttpPost httpPost = new HttpPost(getUrl()); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(wechatConfiguration.getConnectTimeoutOrDefault()) .setSocketTimeout(wechatConfiguration.getReadTimeoutOrDefault()) .build(); httpPost.setConfig(requestConfig); StringEntity entity = new StringEntity(getParamJson(getMsg(jobName, cause)), StandardCharsets.UTF_8); entity.setContentEncoding(StandardCharsets.UTF_8.name()); entity.setContentType("application/json"); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { int status = response.getStatusLine().getStatusCode(); if (HttpURLConnection.HTTP_OK == status) { JsonObject resp = GsonFactory.getGson().fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class); if (!"0".equals(resp.get("errcode").getAsString())) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: {}", jobName, resp.get("errmsg").getAsString(), cause); } else { log.error("An exception has occurred in Job '{}', Notification to wechat was successful.", jobName, cause); } } else { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of: Unexpected response status: {}", jobName, status, cause); } } catch (IOException ex) { log.error("An exception has occurred in Job '{}', But failed to send alert by wechat because of", jobName, ex); } } WechatJobErrorHandler(); @Override void handleException(final String jobName, final Throwable cause); @Override String getType();  }