You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
50 lines
1.4 KiB
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());
|
|
}
|
|
}
|
|
}
|