Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more specific exception for not found entity #207

Merged
merged 1 commit into from
Feb 11, 2020

Conversation

maihacke
Copy link
Member

@maihacke maihacke commented Feb 5, 2020

Currently our GenericRepository throws an IllegalStateException when invoking find(ID id) for a not existing id.
Spring Data converts this to an InvalidDataAccessApiUsageException this exception is missleading in my opinion.
Spring Data offers an EmptyResultDataAccessException which is more specific and allows easy handling e.g. in a RestService:

  @Override
  public PersonEto getPerson(long id) {

    try {
      return this.ucManagePerson.findPerson(id);
    } catch (EmptyResultDataAccessException e) {
      throw new NotFoundException();
    }
  }

If the proposed change here is accepted, we could even implement this in com.devonfw.module.rest.service.impl.RestServiceExceptionFacade.

What do you think?

@maihacke maihacke added enhancement New feature or request dataaccess data-access layer (persistence, etc.) labels Feb 5, 2020
@maihacke maihacke changed the title Use more specific exception Use more specific exception for not found entity Feb 5, 2020
@maihacke maihacke requested a review from hohwille February 7, 2020 11:29
Copy link
Member

@hohwille hohwille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maihacke 👍 reasonable improvement, nice.

@hohwille hohwille merged commit 701a427 into devonfw:develop Feb 11, 2020
@hohwille
Copy link
Member

For the record: cause e should be passed to new exception:

    } catch (EmptyResultDataAccessException e) {
      throw new NotFoundException(e);
    }

@maihacke maihacke deleted the jpa-not-found branch February 11, 2020 12:02
@hohwille hohwille added this to the release:2020.04.00 milestone Feb 11, 2020
hohwille pushed a commit to hohwille/devon4j that referenced this pull request Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dataaccess data-access layer (persistence, etc.) enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants