Search in sources :

Example 6 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class DynamicTemplateSaxParser method createExpansionExtractor.

private static Mapper<DynamicDelegate, String> createExpansionExtractor(final String expression, final Location location, final BindingSource bindingSource) {
    return new Mapper<DynamicDelegate, String>() {

        public String map(DynamicDelegate delegate) {
            try {
                Binding binding = bindingSource.newBinding("dynamic template binding", delegate.getComponentResources().getContainerResources(), delegate.getComponentResources(), BindingConstants.PROP, expression, location);
                Object boundValue = binding.get();
                return boundValue == null ? null : boundValue.toString();
            } catch (Throwable t) {
                throw new TapestryException(ExceptionUtils.toMessage(t), location, t);
            }
        }
    };
}
Also used : Binding(org.apache.tapestry5.Binding) Mapper(org.apache.tapestry5.func.Mapper) DynamicDelegate(org.apache.tapestry5.services.dynamic.DynamicDelegate) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 7 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class PageLoaderImpl method startComponent.

private EmbeddedComponentAssembler startComponent(AssemblerContext context) {
    StartComponentToken token = context.next(StartComponentToken.class);
    ComponentAssembler assembler = context.assembler;
    String elementName = token.getElementName();
    // Initial guess: the type from the token (but this may be null in many cases).
    String embeddedType = token.getComponentType();
    // This may be null for an anonymous component.
    String embeddedId = token.getId();
    String embeddedComponentClassName = null;
    final EmbeddedComponentModel embeddedModel = embeddedId == null ? null : assembler.getModel().getEmbeddedComponentModel(embeddedId);
    if (embeddedId == null)
        embeddedId = assembler.generateEmbeddedId(embeddedType);
    if (embeddedModel != null) {
        String modelType = embeddedModel.getComponentType();
        if (InternalUtils.isNonBlank(modelType) && embeddedType != null) {
            throw new TapestryException(String.format("Embedded component '%s' provides a type attribute in the template ('%s') " + "as well as in the component class ('%s'). You should not provide a type attribute " + "in the template when defining an embedded component within the component class.", embeddedId, embeddedType, modelType), token, null);
        }
        embeddedType = modelType;
        embeddedComponentClassName = embeddedModel.getComponentClassName();
    }
    String componentClassName = embeddedComponentClassName;
    if (InternalUtils.isNonBlank(embeddedType)) {
        try {
            componentClassName = componentClassResolver.resolveComponentTypeToClassName(embeddedType);
        } catch (RuntimeException ex) {
            throw new TapestryException(ex.getMessage(), token, ex);
        }
    }
    // OK, now we can record an action to get it instantiated.
    EmbeddedComponentAssembler embeddedAssembler = assembler.createEmbeddedAssembler(embeddedId, componentClassName, embeddedModel, token.getMixins(), token.getLocation());
    addActionForEmbeddedComponent(context, embeddedAssembler, embeddedId, elementName, componentClassName);
    addParameterBindingActions(context, embeddedAssembler, embeddedModel);
    if (embeddedModel != null && embeddedModel.getInheritInformalParameters()) {
        // Another two-step: The first "captures" the container and embedded component. The second
        // occurs at the end of the page setup.
        assembler.add(new PageAssemblyAction() {

            public void execute(PageAssembly pageAssembly) {
                final ComponentPageElement container = pageAssembly.activeElement.peek();
                final ComponentPageElement embedded = pageAssembly.createdElement.peek();
                pageAssembly.deferred.add(new PageAssemblyAction() {

                    public void execute(PageAssembly pageAssembly) {
                        copyInformalParameters(container, embedded);
                    }
                });
            }
        });
    }
    return embeddedAssembler;
}
Also used : EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 8 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class PropertyEditorTest method no_editor_block_available.

@Test
public void no_editor_block_available() {
    PropertyModel model = mockPropertyModel();
    PropertyOverrides overrides = mockPropertyOverrides();
    ComponentResources resources = mockComponentResources();
    BeanBlockSource source = newMock(BeanBlockSource.class);
    RuntimeException exception = new RuntimeException("Simulated failure.");
    Messages messages = mockMessages();
    Location l = mockLocation();
    String propertyId = "foo";
    String dataType = "unk";
    String propertyName = "fooProp";
    Object object = "[OBJECT]";
    String formattedMessage = "formatted-message";
    expect(model.getId()).andReturn(propertyId);
    train_getOverrideBlock(overrides, propertyId, null);
    expect(model.getDataType()).andReturn(dataType);
    expect(source.getEditBlock(dataType)).andThrow(exception);
    expect(model.getPropertyName()).andReturn(propertyName);
    train_getLocation(resources, l);
    expect(messages.format("core-block-error", propertyName, dataType, object, exception)).andReturn(formattedMessage);
    replay();
    PropertyEditor pe = new PropertyEditor();
    pe.inject(resources, overrides, model, source, messages, object);
    try {
        pe.beginRender();
        unreachable();
    } catch (TapestryException ex) {
        assertEquals(ex.getMessage(), formattedMessage);
        assertSame(ex.getLocation(), l);
    }
}
Also used : Messages(org.apache.tapestry5.commons.Messages) BeanBlockSource(org.apache.tapestry5.services.BeanBlockSource) PropertyModel(org.apache.tapestry5.beanmodel.PropertyModel) PropertyOverrides(org.apache.tapestry5.PropertyOverrides) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 9 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class PageElementFactoryImplTest method unclosed_attribute_expression.

@Test
public void unclosed_attribute_expression() {
    TypeCoercer typeCoercer = mockTypeCoercer();
    BindingSource bindingSource = mockBindingSource();
    ComponentResources resources = mockComponentResources();
    Location location = mockLocation();
    AttributeToken token = new AttributeToken(null, "fred", "${flintstone", location);
    replay();
    PageElementFactory factory = new PageElementFactoryImpl(typeCoercer, bindingSource);
    try {
        factory.newAttributeElement(resources, token);
        unreachable();
    } catch (TapestryException ex) {
        assertEquals(ex.getMessage(), "Attribute expression \'${flintstone\' is missing a closing brace.");
        assertSame(ex.getLocation(), location);
    }
    verify();
}
Also used : BindingSource(org.apache.tapestry5.services.BindingSource) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) AttributeToken(org.apache.tapestry5.internal.parser.AttributeToken) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 10 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class TemplateParserImplTest method resource_that_throws_exception.

@Test
public // TAP5-2516
void resource_that_throws_exception() throws Exception {
    Resource resource = new AbstractResource("throwfoo") {

        @Override
        public URL toURL() {
            return null;
        }

        @Override
        public boolean exists() {
            return true;
        }

        @Override
        protected Resource newResource(String path) {
            return null;
        }

        @Override
        public InputStream openStream() throws IOException {
            throw new IOException("foo");
        }
    };
    try {
        getParser().parseTemplate(resource);
        unreachable();
    } catch (RuntimeException ex) {
        if (ex.getCause() instanceof TapestryException && ex.getCause().getCause() instanceof IOException) {
            assertMessageContains(ex, "foo");
        } else {
            throw ex;
        }
    }
}
Also used : Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) AbstractResource(org.apache.tapestry5.ioc.internal.util.AbstractResource) AbstractResource(org.apache.tapestry5.ioc.internal.util.AbstractResource) IOException(java.io.IOException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) Test(org.testng.annotations.Test)

Aggregations

TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)37 Location (org.apache.tapestry5.commons.Location)12 Test (org.testng.annotations.Test)12 ComponentResources (org.apache.tapestry5.ComponentResources)11 Binding (org.apache.tapestry5.Binding)7 BindingFactory (org.apache.tapestry5.services.BindingFactory)4 IOException (java.io.IOException)3 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)3 Component (org.apache.tapestry5.runtime.Component)3 PropertyOverrides (org.apache.tapestry5.PropertyOverrides)2 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)2 PropertyModel (org.apache.tapestry5.beanmodel.PropertyModel)2 Messages (org.apache.tapestry5.commons.Messages)2 Link (org.apache.tapestry5.http.Link)2 Instantiator (org.apache.tapestry5.internal.services.Instantiator)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 OperationException (org.apache.tapestry5.ioc.internal.OperationException)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 BindingSource (org.apache.tapestry5.services.BindingSource)2