Skip to content

Commit

Permalink
when objects are unable to be found; throw specific exception
Browse files Browse the repository at this point in the history
  • Loading branch information
andponlin committed Sep 21, 2024
1 parent 1f6ae0d commit 6bf14f6
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Pkg extends _Pkg implements MutableCreateAndModifyTimestamped {

public static Pkg getByName(ObjectContext context, String name) {
return tryGetByName(context, name)
.orElseThrow(() -> new IllegalStateException("unable to find pkg for name [" + name + "]"));
.orElseThrow(() -> new ObjectNotFoundException(Pkg.class.getSimpleName(), name));
}

public static Optional<Pkg> tryGetByName(ObjectContext context, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.cayenne.validation.ValidationResult;
import org.haiku.haikudepotserver.dataobjects.auto._PkgScreenshot;
import org.haiku.haikudepotserver.dataobjects.support.MutableCreateAndModifyTimestamped;
import org.haiku.haikudepotserver.support.exception.ObjectNotFoundException;

import java.util.List;
import java.util.Optional;
Expand All @@ -21,7 +22,7 @@ public class PkgScreenshot extends _PkgScreenshot implements Comparable<PkgScree

public static PkgScreenshot getByCode(ObjectContext context, String code) {
return tryGetByCode(context, code)
.orElseThrow(() -> new IllegalStateException("unable to find the screenshot with code [" + code + "]"));
.orElseThrow(() -> new ObjectNotFoundException(PkgScreenshot.class.getSimpleName(), code));
}

public static Optional<PkgScreenshot> tryGetByCode(ObjectContext context, String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.haiku.haikudepotserver.dataobjects.support.Coded;
import org.haiku.haikudepotserver.dataobjects.support.MutableCreateAndModifyTimestamped;
import org.haiku.haikudepotserver.support.SingleCollector;
import org.haiku.haikudepotserver.support.exception.ObjectNotFoundException;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static Optional<Repository> tryGetByCode(ObjectContext context, String co

public static Repository getByCode(ObjectContext context, String code) {
return tryGetByCode(context, code).orElseThrow(
() -> new IllegalStateException("unable to find repository by code [" + code + "]"));
() -> new ObjectNotFoundException(Repository.class.getSimpleName(), code));
}

public static List<Repository> getAllActive(ObjectContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror;
import org.haiku.haikudepotserver.support.ExposureType;
import org.haiku.haikudepotserver.support.SingleCollector;
import org.haiku.haikudepotserver.support.exception.ObjectNotFoundException;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.MalformedURLException;
Expand All @@ -30,7 +31,6 @@
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RepositorySource extends _RepositorySource {

Expand All @@ -48,8 +48,7 @@ public static RepositorySource get(ObjectContext context, ObjectId objectId) {

public static RepositorySource getByCode(ObjectContext context, String code) {
return tryGetByCode(context, code)
.orElseThrow(() -> new IllegalStateException(
"unable to find the repository source for code [" + code + "]"));
.orElseThrow(() -> new ObjectNotFoundException(RepositorySource.class.getSimpleName(), code));
}

public static Optional<RepositorySource> tryGetByCode(ObjectContext context, String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import org.haiku.haikudepotserver.support.exception.ObjectNotFoundException;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -60,7 +62,7 @@ public static User getByObjectId(ObjectContext context, ObjectId objectId) {

public static User getByNickname(ObjectContext context, String nickname) {
return tryGetByNickname(context, nickname)
.orElseThrow(() -> new IllegalStateException("unable to get the user for nickname [" + nickname + "]"));
.orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), nickname));
}

public static Optional<User> tryGetByNickname(ObjectContext context, String nickname) {
Expand Down

0 comments on commit 6bf14f6

Please sign in to comment.