Skip to content

Commit

Permalink
Reorganize exception, more precisely and more detailedly
Browse files Browse the repository at this point in the history
  • Loading branch information
junior9919 committed Oct 19, 2015
1 parent d2621de commit 26f5d5a
Show file tree
Hide file tree
Showing 53 changed files with 344 additions and 346 deletions.
33 changes: 17 additions & 16 deletions src/com/halo/http/utils/HttpClientTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private String getUrlWithArgs(String url, Map<String, String> args) throws HttpU
try {
paramValue = URLEncoder.encode(args.get(paramName), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HttpUtilsException("URL get parameter encode error: " + e.getMessage());
throw new HttpUtilsException("URL encode error.", e);
}
params += paramName + "=" + paramValue + "&";
}
Expand All @@ -56,7 +56,7 @@ private File saveStreamToFile(InputStream stream) throws HttpUtilsException {
try {
outputStream = new FileOutputStream(downloadFile);
} catch (FileNotFoundException e) {
throw new HttpUtilsException("Download file path " + downloadFile.getPath() + " not found.");
throw new HttpUtilsException("Can't create a FileOutputStream.", e);
}

int size = 0;
Expand All @@ -68,14 +68,14 @@ private File saveStreamToFile(InputStream stream) throws HttpUtilsException {
outputStream.write(bytes, 0, size);
}
} catch (IOException e) {
throw new HttpUtilsException("File i/o error.");
throw new HttpUtilsException("Can't read from input stream or write to output stream.", e);
}

try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
throw new HttpUtilsException("Close stream error.");
throw new HttpUtilsException("Close stream error.", e);
}

return downloadFile;
Expand All @@ -87,6 +87,7 @@ public String get(String url, Map<String, String> args) throws HttpUtilsExceptio

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(urlWithArgs);

// Create a custom response handler
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

Expand All @@ -106,12 +107,12 @@ public String handleResponse(final HttpResponse response) throws ClientProtocolE
try {
responseBody = httpClient.execute(httpGet, responseHandler);
} catch (IOException e) {
throw new HttpUtilsException("An error occued when execute http get method: " + urlWithArgs);
throw new HttpUtilsException("Get from " + urlWithArgs + " error.", e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new HttpUtilsException("An error occued when close http client.");
throw new HttpUtilsException("Close http stream error.", e);
}
}

Expand All @@ -134,12 +135,12 @@ public File handleResponse(final HttpResponse response) throws ClientProtocolExc
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
if (entity == null) {
throw new ClientProtocolException("Response contains no content");
throw new ClientProtocolException("There's no content in http response.");
}
try {
return saveStreamToFile(entity.getContent());
} catch (HttpUtilsException e) {
throw new IOException("Save file error.");
throw new IOException("Save file error.", e);
}
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
Expand All @@ -152,12 +153,12 @@ public File handleResponse(final HttpResponse response) throws ClientProtocolExc
try {
downloadFile = httpClient.execute(httpGet, responseHandler);
} catch (IOException e) {
throw new HttpUtilsException("An error occued when execute http get method: " + urlWithArgs);
throw new HttpUtilsException("Download from " + urlWithArgs + " error.", e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new HttpUtilsException("An error occued when close http client.");
throw new HttpUtilsException("Close http stream error.", e);
}
}

Expand Down Expand Up @@ -197,12 +198,12 @@ public String handleResponse(final HttpResponse response) throws ClientProtocolE
try {
responseBody = httpClient.execute(httpPost, responseHandler);
} catch (IOException e) {
throw new HttpUtilsException("An error occued when execute http post method: " + urlWithArgs);
throw new HttpUtilsException("Post to " + urlWithArgs + " error.", e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new HttpUtilsException("An error occued when close http client.");
throw new HttpUtilsException("Close http stream error.", e);
}
}

Expand Down Expand Up @@ -232,12 +233,12 @@ public File handleResponse(final HttpResponse response) throws ClientProtocolExc
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
if (entity == null) {
throw new ClientProtocolException("Response contains no content");
throw new ClientProtocolException("There's no content in http response.");
}
try {
return saveStreamToFile(entity.getContent());
} catch (HttpUtilsException e) {
throw new IOException("Save file error.");
throw new IOException("Save file error.", e);
}
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
Expand All @@ -250,12 +251,12 @@ public File handleResponse(final HttpResponse response) throws ClientProtocolExc
try {
downloadFile = httpClient.execute(httpPost, responseHandler);
} catch (IOException e) {
throw new HttpUtilsException("An error occued when execute http post method: " + urlWithArgs);
throw new HttpUtilsException("Download from " + urlWithArgs + " error.", e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new HttpUtilsException("An error occued when close http client.");
throw new HttpUtilsException("Close http stream error.", e);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/com/halo/http/utils/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static String readEntityFromStream(HttpServletRequest request) throws Htt
try {
stream = request.getInputStream();
} catch (IOException e) {
throw new HttpUtilsException("An error occured when get input stream. ");
throw new HttpUtilsException("Can't get request input stream.", e);
}

StringBuilder sb = new StringBuilder();
Expand All @@ -29,11 +29,11 @@ public static String readEntityFromStream(HttpServletRequest request) throws Htt
try {
sb.append(new String(b, 0, n, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new HttpUtilsException("Can't create string from byte. ");
throw new HttpUtilsException("Can't create string from byte.", e);
}
}
} catch (IOException e) {
throw new HttpUtilsException("An error occured when read from stream. ");
throw new HttpUtilsException("Read from stream error.", e);
}
return sb.toString();
}
Expand Down
8 changes: 5 additions & 3 deletions src/com/halo/http/utils/HttpUtilsException.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
*/
public class HttpUtilsException extends Exception {

/**
*
*/
private static final long serialVersionUID = 7379733374170302597L;

public HttpUtilsException(String message) {
super(message);
}

public HttpUtilsException(String message, Throwable cause) {
super(message, cause);
}

}
2 changes: 1 addition & 1 deletion src/com/halo/json/utils/JSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public JSONUtils(Class<T> clazz) {
}

@SuppressWarnings("unchecked")
public T getJsonBean(String jsonStr) throws JSONUtilsException {
public T getJsonBean(String jsonStr) {
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
return (T) JSONObject.toBean(jsonObj, this.clazz);
}
Expand Down
11 changes: 0 additions & 11 deletions src/com/halo/json/utils/JSONUtilsException.java

This file was deleted.

12 changes: 3 additions & 9 deletions src/com/halo/servlet/listeners/CustomMenuLoadListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import javax.servlet.ServletContextListener;

import com.halo.json.utils.JSONUtils;
import com.halo.json.utils.JSONUtilsException;
import com.halo.spring.utils.SpringUtils;
import com.halo.wechat.capabilities.CapabilityException;
import com.halo.wechat.capabilities.abilities.CustomMenuAbility;
Expand Down Expand Up @@ -66,7 +65,7 @@ private String getMenuFileName(ServletContext servletContext) {
private File getMenuFile(String menuFileName) {
File file = new File(menuFileName);
if (!file.exists()) {
appLogger.getLogger().error(file.getPath() + " does not exist.");
appLogger.getLogger().error(file.getPath() + " doesn't exist.");
return null;
}
return file;
Expand Down Expand Up @@ -112,13 +111,8 @@ private boolean deleteMenu(CustomMenuAbility customMenuAbility) {

private boolean createMenu(String menuJson, CustomMenuAbility customMenuAbility) {
JSONUtils<MenuBean> jsonUtils = new JSONUtils<MenuBean>(MenuBean.class);
MenuBean menu = null;
try {
menu = jsonUtils.getJsonBean(menuJson);
} catch (JSONUtilsException e) {
appLogger.getLogger().error(e.getMessage());
return false;
}
MenuBean menu = jsonUtils.getJsonBean(menuJson);

if (null != menu) {
ResultBean resultBean = null;
try {
Expand Down
17 changes: 17 additions & 0 deletions src/com/halo/spring/utils/NullWebApplicationContextException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.halo.spring.utils;

/**
* @file NullWebApplicationContextException.java
* @author Junior
* @date 2015年8月28日 下午10:40:11
* @version 1.0
*/
public class NullWebApplicationContextException extends Exception {

private static final long serialVersionUID = 5096369159157276362L;

public NullWebApplicationContextException(String message) {
super(message);
}

}
13 changes: 6 additions & 7 deletions src/com/halo/spring/utils/SpringUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.halo.spring.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -31,11 +30,11 @@ public static void setWebApplicationContext(WebApplicationContext webApplication
}

@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
public void setApplicationContext(ApplicationContext arg0) {
applicationContext = arg0;
}

public static Object getServletBean(String servletName, String beanId) throws SpringUtilsException {
public static Object getServletBean(String servletName, String beanId) throws NullWebApplicationContextException {
ApplicationContext servletApplicationContext = (ApplicationContext) getFromServletContext(FrameworkServlet.class.getName() + ".CONTEXT." + servletName);
Object bean = null;
try {
Expand All @@ -56,23 +55,23 @@ public static Object getBean(String beanId) {
return bean;
}

public static void addIntoServletContext(String attrId, Object toAdd) throws SpringUtilsException {
public static void addIntoServletContext(String attrId, Object toAdd) throws NullWebApplicationContextException {
if (null == webApplicationContext) {
webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
}
if (null == webApplicationContext) {
throw new SpringUtilsException(
throw new NullWebApplicationContextException(
"Spring WebApplicationContext is null, please check web.xml make sure ContextLoaderListener configuration is correct.");
}
webApplicationContext.getServletContext().setAttribute(attrId, toAdd);
}

public static Object getFromServletContext(String attrId) throws SpringUtilsException {
public static Object getFromServletContext(String attrId) throws NullWebApplicationContextException {
if (null == webApplicationContext) {
webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
}
if (null == webApplicationContext) {
throw new SpringUtilsException(
throw new NullWebApplicationContextException(
"Spring WebApplicationContext is null, please check web.xml make sure ContextLoaderListener configuration is correct.");
}
return webApplicationContext.getServletContext().getAttribute(attrId);
Expand Down
17 changes: 0 additions & 17 deletions src/com/halo/spring/utils/SpringUtilsException.java

This file was deleted.

29 changes: 8 additions & 21 deletions src/com/halo/wechat/capabilities/AbstractCapability.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.halo.http.utils.HttpTemplate;
import com.halo.json.utils.JSONUtils;
import com.halo.json.utils.JSONUtilsException;

/**
* 微信能力接口抽象类,实际上本类中没有一个抽象方法。本类实现了所有微信能力接口的共用方法,可以当作是微信能力接口类的工具类。
Expand Down Expand Up @@ -61,13 +60,10 @@ public Properties getProperties() {
*
* @throws CapabilityException
* 加载"wechat.properties"配置文件失败抛出的异常
* @throws PropertiesException
*/
public AbstractCapability() throws CapabilityException {
try {
properties = WechatProperties.getInstance();
} catch (PropertiesException e) {
throw new CapabilityException("Instantiate capability object error: " + e.getMessage());
}
public AbstractCapability() throws PropertiesException {
properties = WechatProperties.getInstance();
}

/**
Expand All @@ -76,13 +72,13 @@ public AbstractCapability() throws CapabilityException {
* @param propertyName
* 参数名,系统默认的微信客户端参数有:app_id、app_secret、token;用户也可以在配置文件中声明自己的参数。
* @return String类型的参数值
* @throws CapabilityException
* @throws UndefinedPropertyException
* 参数名在配置文件中不存在,将抛出异常。此时应当检查配置文件是否损坏或参数名是否与配置文件中一致
*/
public String getProperty(String propertyName) throws CapabilityException {
public String getProperty(String propertyName) throws UndefinedPropertyException {
String propertyValue = getProperties().getProperty(propertyName);
if (null == propertyValue) {
throw new CapabilityException("Missing " + propertyName + " in " + WECHAT_PROPERTIES_FILE_NAME + ", please make sure property has been set.");
throw new UndefinedPropertyException(propertyName + " doesn't defined in " + WECHAT_PROPERTIES_FILE_NAME);
}
return propertyValue;
}
Expand All @@ -95,18 +91,9 @@ public String getProperty(String propertyName) throws CapabilityException {
* @param jsonStr
* 要解析的json字符串
* @return 解析后的对象,类型由JSONUtils的泛型指定
* @throws CapabilityException
* 解析过程中发生错误抛出的异常
*/
public <T> T getJsonBean(JSONUtils<T> jsonUtils, String jsonStr) throws CapabilityException {
T bean = null;
try {
bean = jsonUtils.getJsonBean(jsonStr);
} catch (JSONUtilsException e) {
throw new CapabilityException("Parse result error with json string: " + jsonStr);
}

return bean;
public <T> T getJsonBean(JSONUtils<T> jsonUtils, String jsonStr) {
return jsonUtils.getJsonBean(jsonStr);
}

/**
Expand Down
Loading

0 comments on commit 26f5d5a

Please sign in to comment.