Created
December 5, 2018 22:48
-
-
Save h4t0n/c21567c5c42f37f5c5275dd8e8f89cb0 to your computer and use it in GitHub Desktop.
[Gemini] ErrorService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class ErrorService implements InitializingBean { | |
private List<String> ERROR_CODES = new ArrayList<>(); | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); | |
provider.addIncludeFilter(new AssignableTypeFilter(GeminiException.class)); | |
Set<BeanDefinition> components = provider.findCandidateComponents("it.at7.gemini"); | |
for (BeanDefinition component : components) { | |
Class<?> gemException = Class.forName(component.getBeanClassName()); | |
Class<?>[] innerClasses = gemException.getClasses(); | |
for (Class<?> innerClass : innerClasses) { | |
String simpleName = innerClass.getSimpleName(); | |
if (simpleName.equals("Code")) { | |
Enum[] enumConstants = (Enum[]) innerClass.getEnumConstants(); | |
register(enumConstants); | |
} | |
} | |
} | |
} | |
private void register(Enum<?>... errors) { | |
for (Enum<?> error : errors) { | |
String erroCode = error.name(); | |
addToErrorCodes(erroCode); | |
} | |
} | |
private void addToErrorCodes(String error) { | |
error = error.toUpperCase(); | |
if (ERROR_CODES.contains(error)) { | |
throw new RuntimeException(String.format("DUPLICATE ERROR MESSAGES %s", error)); | |
} | |
ERROR_CODES.add(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment