Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Specify property name in exception message on nested type build failure #411

@petringo

Description

@petringo

When a nested buildable type fails to build, exception is passed as is, giving no hint in exception message of failed property.

For example

@FreeBuilder
public abstract class TopLevel {
    public static void main(final String[] args) {
        new TopLevel.Builder()
                .mutateNested1(builder -> builder.requiredProperty(true))
                // leave nested2.requiredProperty unset
                .build();
    }

    public abstract Nested nested1();
    public abstract Nested nested2();

    public static class Builder extends TopLevel_Builder {}

    @FreeBuilder
    public abstract static class Nested {
        public abstract boolean requiredProperty();

        public static class Builder extends TopLevel_Nested_Builder {}
    }
}

outputs when generated by FreeBuilder 2.3.0:

Exception in thread "main" java.lang.IllegalStateException: Not set: [requiredProperty]
	at example.TopLevel_Nested_Builder.build(TopLevel_Nested_Builder.java:135)
	at example.TopLevel$Nested$Builder.build(TopLevel.java:23)
	at example.TopLevel_Builder$Value.<init>(TopLevel_Builder.java:254)
	at example.TopLevel_Builder$Value.<init>(TopLevel_Builder.java:240)
	at example.TopLevel_Builder.build(TopLevel_Builder.java:217)
	at example.TopLevel$Builder.build(TopLevel.java:17)
	at example.TopLevel.main(TopLevel.java:11)

To resolve which of the nested properties failed one must go through stacktrace and look at the generated code.

Instead the generated top level class should wrap the exception adding name of failed property, e.g.:

  private static final class Value extends Rebuildable {
    private final Nested nested1;
    private final Nested nested2;

    private Value(TopLevel_Builder builder) {
        // ...
        try {
          this.nested2 = nested2Builder.build();
        } catch (RuntimeException e) {
          throw new IllegalStateException("nested2: " + e.getMessage(), e);
        }
    // ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions