You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Steinar Bang <sb...@dod.no> on 2022/09/30 18:54:53 UTC

Not catching exception from the "wrong" version of a bundle

I have this code[1]:
        try {
            no.priv.bang.osgiservice.users.User user = useradmin.getUser(username);
            return Account.with()
                .accountid(results.getInt("account_id"))
                .username(username)
                .firstName(user.getFirstname())
                .lastName(user.getLastname())
                .balance(results.getDouble("balance"))
                .build();
        } catch (AuthserviceException e) {
            logWarning(String.format("No authservice user for username \"%s\" when fetching account", username), e);
            return Account.with()
                .accountid(results.getInt("account_id"))
                .username(username)
                .balance(results.getDouble("balance"))
                .build();
        }
where the catch suddenly didn't catch an AuthserviceException[2].
 

I'm thinking the reason it didn't catch the AuthserviceException, was
that the version the class was imported from, was version 1.14.5, while
the one thrown was 1.15.0-SNAPSHOT.

The import-package in the ukelonn.backend bundle imports:
 no.priv.bang.authservice.definitions;version="[1.14,2)
and I would have thought with everything being loaded at the same time,
using karaf features, the one from 1.15.0.SNAPSHOT would have been
picked...?

What do others do in this case?  Catch Exception or RuntimeException and
check the classname and rethrow?  Is there a clever OSGi approach I'm
not seeing?

Or should I make an effort to make the features not load a non-snapshot
version of the bundle defining the exception, so that the
1.15.0.SNAPSHOT version is the only one loaded?

I think the reason the service injection throwing the exception isn't
failing, is that I defined the service in a separately released
bundle[3].

Maybe I should move the exceptions there as well? (they haven't changed
since 2018 and are unlikely to change)

Thanks!


- Steinar

References:
[1] <https://github.com/steinarb/ukelonn/blob/3616bf316d914a6695c4e6c5103dbcbe84456be0/ukelonn.backend/src/main/java/no/priv/bang/ukelonn/backend/UkelonnServiceProvider.java#L736>
[2] <https://github.com/steinarb/authservice/blob/master/authservice/authservice.definitions/src/main/java/no/priv/bang/authservice/definitions/AuthserviceException.java#L18>
[3] <https://github.com/steinarb/osgi-service/blob/master/osgiservice/osgiservice.users/src/main/java/no/priv/bang/osgiservice/users/UserManagementService.java#L33>