Skip to content

Better Message for Invalid + Operation #15

@hantsy

Description

@hantsy

I tried to use EL to access the Optional<PhoneNubmer> in a record.

public record PhoneNumber(String countryCode, String number) {
}

And the Customer:

public record Customer(
        String firstName,
        String lastName,
        Optional<PhoneNumber> phoneNumber,
        EmailAddress[] emailAddresses,
        Address address
) {
}

And evaluate the following expression.

 String phoneNumber = elProcessor.eval("customer.phoneNumber.map(t-> t.countryCode+t.number).orElse('NotFound')");
        System.out.println(phoneNumber);

It throws an exception,

java.lang.ClassCastException: class java.lang.Long cannot be cast to 
class java.lang.String (java.lang.Long and java.lang.String are in 
module java.base of loader 'bootstrap')

But there is no Long type field in EL expression.

As suggested by @OndroMih, it is better to show a message to indicate what is wrong, which will help developers to locate the issues quickly.

 customer.phoneNumber.map(t-> t.countryCode[>>> + ]t.number).orElse('NotFound')

BTW: Replace the map() function with map(t -> t.contryCode.concat(t.number)), and it works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions