use of org.mule.runtime.core.api.exception.ExceptionMapper in project mule by mulesoft.
the class ExceptionMapperTestCase method sameHierarchyMapping.
@Test
public void sameHierarchyMapping() {
ExceptionMapper exceptionMapper = ExceptionMapper.builder().addExceptionMapping(RuntimeException.class, runtimeExceptionErrorType).addExceptionMapping(NumberFormatException.class, numberFormatExceptionErrorType).addExceptionMapping(IllegalArgumentException.class, illegalArgumentExceptionErrorType).build();
assertThat(exceptionMapper.resolveErrorType(IllegalArgumentException.class).get(), is(illegalArgumentExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(NumberFormatException.class).get(), is(numberFormatExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(RuntimeException.class).get(), is(runtimeExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(Exception.class).isPresent(), is(false));
}
use of org.mule.runtime.core.api.exception.ExceptionMapper in project mule by mulesoft.
the class ExceptionMapperTestCase method differentHierarchyMapping.
@Test
public void differentHierarchyMapping() {
ExceptionMapper exceptionMapper = ExceptionMapper.builder().addExceptionMapping(NumberFormatException.class, numberFormatExceptionErrorType).addExceptionMapping(ClassCastException.class, classCastExceptionErrorType).addExceptionMapping(ArrayStoreException.class, arrayStoreExceptionErrorType).build();
assertThat(exceptionMapper.resolveErrorType(NumberFormatException.class).get(), is(numberFormatExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(ArrayStoreException.class).get(), is(arrayStoreExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(ClassCastException.class).get(), is(classCastExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(Exception.class).isPresent(), is(false));
}
use of org.mule.runtime.core.api.exception.ExceptionMapper in project mule by mulesoft.
the class ExceptionMapperTestCase method complexHierarchyMapping.
/**
* Creates a complex hierarchy starting with {@link RuntimeException} and several children, but with gaps in between and adding
* them out of order. Verifies that the error types respect the hierarchy even for those classes in the gap.
*/
@Test
public void complexHierarchyMapping() {
ExceptionMapper exceptionMapper = ExceptionMapper.builder().addExceptionMapping(NumberFormatException.class, numberFormatExceptionErrorType).addExceptionMapping(ClassCastException.class, classCastExceptionErrorType).addExceptionMapping(RuntimeException.class, runtimeExceptionErrorType).addExceptionMapping(ArrayStoreChildException.class, arrayStoreChildExceptionErrorType).addExceptionMapping(ArrayStoreException.class, arrayStoreExceptionErrorType).build();
assertThat(exceptionMapper.resolveErrorType(RuntimeException.class).get(), is(runtimeExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(IllegalArgumentException.class).get(), is(runtimeExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(NumberFormatException.class).get(), is(numberFormatExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(ArrayStoreException.class).get(), is(arrayStoreExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(ArrayStoreChildException.class).get(), is(arrayStoreChildExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(ClassCastException.class).get(), is(classCastExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(ClassCastChildException.class).get(), is(classCastExceptionErrorType));
assertThat(exceptionMapper.resolveErrorType(Exception.class).isPresent(), is(false));
}
use of org.mule.runtime.core.api.exception.ExceptionMapper in project mule by mulesoft.
the class ErrorTypeLocator method lookupComponentErrorType.
/**
* Finds the {@code ErrorType} related to a component defined by the {@link ComponentIdentifier} based on the exception thrown
* by the component and the mappings configured in the {@code ErrorTypeLocator}.
*
* If no mapping is available then the {@link #lookupErrorType(Throwable)} rules applies.
*
* @param componentIdentifier the identifier of the component that throw the exception.
* @param exception the exception thrown by the component.
* @return the error type related to the exception based on the component mappings. If there's no mapping then the error type
* related to UNKNOWN will be returned.
*/
public ErrorType lookupComponentErrorType(ComponentIdentifier componentIdentifier, Class<? extends Throwable> exception) {
ExceptionMapper exceptionMapper = componentExceptionMappers.get(componentIdentifier);
Optional<ErrorType> errorType = empty();
if (exceptionMapper != null) {
errorType = exceptionMapper.resolveErrorType(exception);
}
return errorType.orElseGet(() -> lookupErrorType(exception));
}
use of org.mule.runtime.core.api.exception.ExceptionMapper in project mule by mulesoft.
the class ExtensionErrorsRegistrant method registerErrors.
/**
* Registers the found {@link ErrorModel} from each {@link OperationModel} into the {@link ErrorTypeRepository} and creates an
* {@link ExceptionMapper} for each {@link OperationModel} that declares {@link ErrorModel}s.
*
* @param extensionModel from where get the {@link ErrorModel} from each {@link OperationModel}
*/
void registerErrors(ExtensionModel extensionModel) {
Set<ErrorModel> errorTypes = extensionModel.getErrorModels();
String extensionNamespace = extensionModel.getXmlDslModel().getPrefix();
String errorExtensionNamespace = MuleExtensionUtils.getExtensionsNamespace(extensionModel);
DslSyntaxResolver syntaxResolver = DslSyntaxResolver.getDefault(extensionModel, new SingleExtensionImportTypesStrategy());
ErrorModel connectivityErrorModel = newError(CONNECTIVITY_ERROR_IDENTIFIER, errorExtensionNamespace).withParent(newError(CONNECTIVITY_ERROR_IDENTIFIER, MULE).build()).build();
ErrorModel retryExhaustedError = newError(RETRY_EXHAUSTED_ERROR_IDENTIFIER, errorExtensionNamespace).withParent(newError(RETRY_EXHAUSTED_ERROR_IDENTIFIER, MULE).build()).build();
errorTypes.forEach(errorModel -> getErrorType(errorModel, extensionModel));
ExtensionWalker extensionWalker = new IdempotentExtensionWalker() {
@Override
protected void onOperation(OperationModel model) {
registerErrors(model);
}
@Override
protected void onConstruct(ConstructModel model) {
registerErrors(model);
}
private void registerErrors(ComponentModel model) {
if (!errorTypes.isEmpty()) {
ExceptionMapper.Builder builder = ExceptionMapper.builder();
builder.addExceptionMapping(ConnectionException.class, getErrorType(connectivityErrorModel, extensionModel));
builder.addExceptionMapping(RetryPolicyExhaustedException.class, getErrorType(retryExhaustedError, extensionModel));
String elementName = syntaxResolver.resolve(model).getElementName();
errorTypeLocator.addComponentExceptionMapper(createIdentifier(elementName, extensionNamespace), builder.build());
}
}
};
extensionWalker.walk(extensionModel);
}
Aggregations