use of org.mule.runtime.api.exception.TypedException in project mule by mulesoft.
the class ThrowProcessor method process.
@Override
public CoreEvent process(CoreEvent event) throws MuleException {
if (count == -1 || count-- > 0) {
try {
Throwable instantiatedException = exception.newInstance();
if (error != null) {
ErrorTypeRepository errorTypeRepository = muleContext.getErrorTypeRepository();
ErrorType errorType = errorTypeRepository.lookupErrorType(buildFromStringRepresentation(error)).orElseThrow(() -> new DefaultMuleException(format("Could not find error: '%s'", error)));
throw new TypedException(instantiatedException, errorType);
} else {
checkArgument(instantiatedException instanceof TypedException, EXCEPTION_ERROR);
throw (TypedException) instantiatedException;
}
} catch (InstantiationException | IllegalAccessException e) {
throw new DefaultMuleException(format("Failed to instantiate exception class '%s'", exception.getSimpleName()));
}
} else {
return event;
}
}
use of org.mule.runtime.api.exception.TypedException in project mule by mulesoft.
the class ModuleExceptionHandlerTestCase method handleTypedException.
@Test
public void handleTypedException() {
when(operationModel.getErrorModels()).thenReturn(singleton(newError(CONNECTIVITY_ERROR_IDENTIFIER, ERROR_NAMESPACE).build()));
ModuleExceptionHandler handler = new ModuleExceptionHandler(operationModel, extensionModel, typeRepository);
typeRepository.addErrorType(builder().name(CONNECTIVITY_ERROR_IDENTIFIER).namespace(ERROR_NAMESPACE).build(), typeRepository.getAnyErrorType());
ModuleException moduleException = new ModuleException(CONNECTIVITY, new RuntimeException());
Throwable exception = handler.processException(moduleException);
assertThat(exception, is(instanceOf(TypedException.class)));
ErrorType errorType = ((TypedException) exception).getErrorType();
assertThat(errorType.getIdentifier(), is(CONNECTIVITY_ERROR_IDENTIFIER));
assertThat(errorType.getNamespace(), is(ERROR_NAMESPACE));
}
use of org.mule.runtime.api.exception.TypedException in project mule by mulesoft.
the class ModuleExceptionHandlerTestCase method handleThrowingChildErrorsFromTheOneDeclared.
@Test
public void handleThrowingChildErrorsFromTheOneDeclared() {
Set<ErrorModel> errors = new HashSet<>();
ErrorModel parent = newError(PARENT.getType(), ERROR_NAMESPACE).build();
ErrorModel child = newError(CHILD.getType(), ERROR_NAMESPACE).withParent(parent).build();
errors.add(parent);
ErrorType parentErrorType = typeRepository.addErrorType(getIdentifier(parent), typeRepository.getAnyErrorType());
typeRepository.addErrorType(getIdentifier(child), parentErrorType);
when(operationModel.getErrorModels()).thenReturn(errors);
ModuleExceptionHandler handler = new ModuleExceptionHandler(operationModel, extensionModel, typeRepository);
ModuleException moduleException = new ModuleException(CHILD, new RuntimeException());
Throwable throwable = handler.processException(moduleException);
assertThat(throwable, is(instanceOf(TypedException.class)));
ErrorType errorType = ((TypedException) throwable).getErrorType();
assertThat(errorType.getIdentifier(), is(CHILD.getType()));
assertThat(errorType.getNamespace(), is(ERROR_NAMESPACE));
}
Aggregations