Skip to content

getXY throws exception if XY not exists #127

@Spenhouet

Description

@Spenhouet

It seems that get operations (not sure if this is a general behaviour) throw an exception if the requested object doesn't exist. I don't think this is good and in my opinion the get request should just return null or Optional.empty if the requested object doesn't exists.

The problem here is the following:
If I want to get an object that may or may not already exist. If it exists it should just be returned but if it doesn't exist it should be created and then returned.
In my eyes this is a common use case.

If for example I want to get a specific group that may or may not exists but that definitely should be returned.

I can try to get the group groupApi.getGroup(...) but if it doesn't exist this will throw an exception so something like the following doesn't work:

Optional<Group> groupOptional = Optional.ofNullable(groupApi.getGroup(...));
if (groupOptional.isPresent())
    return groupOptional.get();

I can just try to create the group groupApi.addGroup(...) but if the group already exists this will also throw an exception.

The only thing that can be done is something like the following but that is in my eyes very bad design:

try {
    return groupApi.getGroup(...);
} catch (GitLabApiException e) {
    if (e.getHttpStatus() == 404)
        return groupApi.addGroup(...);

    throw e;
}

I think throwing a exception when someone tries to create something that already exists is a correct behaviour but throwing an exception if the requested object doesn't exists isn't good.
Just returning null or Optional.empty would make much more sense. Returning an Optional also would force everyone to check if the request returned anything.

I did see that the GitLab API does throw a 404 Group Not Found exception. But I'm not sure if it makes sense to propagate this error further into the Java API.

@gmessner What do you think about this change request? Does this behaviour has another reason than that the GitLab API does it like this? Is there a better way than I'm doing it?

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