Search in sources :

Example 1 with TypedException

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;
    }
}
Also used : ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ErrorType(org.mule.runtime.api.message.ErrorType) TypedException(org.mule.runtime.api.exception.TypedException)

Example 2 with TypedException

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));
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ErrorType(org.mule.runtime.api.message.ErrorType) ModuleException(org.mule.runtime.extension.api.exception.ModuleException) TypedException(org.mule.runtime.api.exception.TypedException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with TypedException

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));
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ErrorType(org.mule.runtime.api.message.ErrorType) ErrorModel(org.mule.runtime.api.meta.model.error.ErrorModel) ModuleException(org.mule.runtime.extension.api.exception.ModuleException) TypedException(org.mule.runtime.api.exception.TypedException) HashSet(java.util.HashSet) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

TypedException (org.mule.runtime.api.exception.TypedException)3 ErrorType (org.mule.runtime.api.message.ErrorType)3 Test (org.junit.Test)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 ModuleException (org.mule.runtime.extension.api.exception.ModuleException)2 SmallTest (org.mule.tck.size.SmallTest)2 HashSet (java.util.HashSet)1 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)1 ErrorTypeRepository (org.mule.runtime.api.exception.ErrorTypeRepository)1 ErrorModel (org.mule.runtime.api.meta.model.error.ErrorModel)1