5 changed files with 143 additions and 0 deletions
@ -0,0 +1,23 @@ |
|||
package com.crawler.exception.handler; |
|||
|
|||
import com.crawler.exception.CacheException; |
|||
import com.crawler.exception.ExceptionHandler; |
|||
import com.crawler.view.CrawlerView; |
|||
|
|||
public class CacheExceptionHandler implements ExceptionHandler { |
|||
@Override |
|||
public void handle(Exception e, CrawlerView view) { |
|||
CacheException cacheException = (CacheException) e; |
|||
StringBuilder message = new StringBuilder("缓存异常: "); |
|||
message.append(cacheException.getMessage()); |
|||
if (cacheException.getFilePath() != null) { |
|||
message.append(" (路径: ").append(cacheException.getFilePath()).append(")"); |
|||
} |
|||
view.showErrorMessage(message.toString()); |
|||
} |
|||
|
|||
@Override |
|||
public Class<? extends Exception> getSupportedExceptionType() { |
|||
return CacheException.class; |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package com.crawler.exception.handler; |
|||
|
|||
import com.crawler.exception.ConfigurationException; |
|||
import com.crawler.exception.InvalidUrlException; |
|||
import com.crawler.exception.UnsupportedCrawlerException; |
|||
import com.crawler.exception.ExceptionHandler; |
|||
import com.crawler.view.CrawlerView; |
|||
|
|||
public class ConfigurationExceptionHandler implements ExceptionHandler { |
|||
@Override |
|||
public void handle(Exception e, CrawlerView view) { |
|||
if (e instanceof InvalidUrlException) { |
|||
InvalidUrlException ex = (InvalidUrlException) e; |
|||
view.showErrorMessage("无效的URL格式: " + ex.getInvalidUrl()); |
|||
} else if (e instanceof UnsupportedCrawlerException) { |
|||
UnsupportedCrawlerException ex = (UnsupportedCrawlerException) e; |
|||
view.showErrorMessage("未找到支持该URL的爬虫: " + ex.getUrl()); |
|||
} else { |
|||
view.showErrorMessage("配置异常: " + e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Class<? extends Exception> getSupportedExceptionType() { |
|||
return ConfigurationException.class; |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
package com.crawler.exception.handler; |
|||
|
|||
import com.crawler.exception.CrawlerException; |
|||
import com.crawler.exception.ExceptionHandler; |
|||
import com.crawler.view.CrawlerView; |
|||
|
|||
public class CrawlerExceptionHandler implements ExceptionHandler { |
|||
@Override |
|||
public void handle(Exception e, CrawlerView view) { |
|||
view.showErrorMessage("爬虫异常: " + e.getMessage()); |
|||
} |
|||
|
|||
@Override |
|||
public Class<? extends Exception> getSupportedExceptionType() { |
|||
return CrawlerException.class; |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
package com.crawler.exception.handler; |
|||
|
|||
import com.crawler.exception.*; |
|||
import com.crawler.exception.ExceptionHandler; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class ExceptionHandlerFactory { |
|||
private static ExceptionHandlerFactory instance; |
|||
private List<ExceptionHandler> handlers; |
|||
|
|||
private ExceptionHandlerFactory() { |
|||
handlers = new ArrayList<>(); |
|||
registerHandlers(); |
|||
} |
|||
|
|||
public static ExceptionHandlerFactory getInstance() { |
|||
if (instance == null) { |
|||
instance = new ExceptionHandlerFactory(); |
|||
} |
|||
return instance; |
|||
} |
|||
|
|||
private void registerHandlers() { |
|||
handlers.add(new NetworkExceptionHandler()); |
|||
handlers.add(new ParseExceptionHandler()); |
|||
handlers.add(new CacheExceptionHandler()); |
|||
handlers.add(new ConfigurationExceptionHandler()); |
|||
handlers.add(new CrawlerExceptionHandler()); |
|||
} |
|||
|
|||
public ExceptionHandler getHandler(Exception e) { |
|||
for (ExceptionHandler handler : handlers) { |
|||
if (handler.getSupportedExceptionType().isInstance(e)) { |
|||
return handler; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public void handleException(Exception e, com.crawler.view.CrawlerView view) { |
|||
ExceptionHandler handler = getHandler(e); |
|||
if (handler != null) { |
|||
handler.handle(e, view); |
|||
} else { |
|||
view.showErrorMessage("未知异常: " + e.getMessage()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
package com.crawler.exception.handler; |
|||
|
|||
import com.crawler.exception.HttpRequestException; |
|||
import com.crawler.exception.NetworkException; |
|||
import com.crawler.exception.TimeoutException; |
|||
import com.crawler.exception.ExceptionHandler; |
|||
import com.crawler.view.CrawlerView; |
|||
|
|||
public class NetworkExceptionHandler implements ExceptionHandler { |
|||
@Override |
|||
public void handle(Exception e, CrawlerView view) { |
|||
if (e instanceof HttpRequestException) { |
|||
HttpRequestException ex = (HttpRequestException) e; |
|||
view.showErrorMessage("HTTP请求失败,状态码: " + ex.getStatusCode()); |
|||
} else if (e instanceof TimeoutException) { |
|||
view.showErrorMessage("网络连接超时,请稍后重试"); |
|||
} else { |
|||
view.showErrorMessage("网络异常: " + e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Class<? extends Exception> getSupportedExceptionType() { |
|||
return NetworkException.class; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue