Repository: james-project Updated Branches: refs/heads/master 83649fc34 -> 5a4c0ded9
JAMES-2321 Review error messages on MailRepository routes - Avoid implicit toString calls - Avoid a weird Optional not working trick Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e21b78b4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e21b78b4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e21b78b4 Branch: refs/heads/master Commit: e21b78b48f74665864f8d98e32c33ec3be511b4c Parents: 83649fc Author: benwa <btell...@linagora.com> Authored: Mon Jun 25 10:02:50 2018 +0700 Committer: benwa <btell...@linagora.com> Committed: Tue Jun 26 09:41:02 2018 +0700 ---------------------------------------------------------------------- .../webadmin/routes/MailRepositoriesRoutes.java | 20 ++++++++++---------- .../service/MailRepositoryStoreService.java | 6 +++--- .../routes/MailRepositoriesRoutesTest.java | 7 ++++--- 3 files changed, 17 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/e21b78b4/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java index 53d7a2a..c69f599 100644 --- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java +++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java @@ -181,11 +181,7 @@ public class MailRepositoriesRoutes implements Routes { MailRepositoryUrl url = MailRepositoryUrl.fromEncoded(encodedUrl); try { return repositoryStoreService.listMails(url, offset, limit) - .orElseThrow(() -> ErrorResponder.builder() - .statusCode(HttpStatus.NOT_FOUND_404) - .type(ErrorType.NOT_FOUND) - .message("The repository " + encodedUrl + "(decoded value: '" + url + "') does not exist") - .haltError()); + .orElseThrow(() -> repositoryNotFound(encodedUrl, url)); } catch (MailRepositoryStore.MailRepositoryStoreException | MessagingException e) { throw ErrorResponder.builder() @@ -294,6 +290,14 @@ public class MailRepositoriesRoutes implements Routes { .haltError(); } + private HaltException repositoryNotFound(String encodedUrl, MailRepositoryUrl url) { + return ErrorResponder.builder() + .statusCode(HttpStatus.NOT_FOUND_404) + .type(ErrorType.NOT_FOUND) + .message("The repository '" + encodedUrl + "' (decoded value: '" + url.asString() + "') does not exist") + .haltError(); + } + private HaltException internalServerError(Exception e) { return ErrorResponder.builder() .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500) @@ -317,11 +321,7 @@ public class MailRepositoriesRoutes implements Routes { MailRepositoryUrl url = MailRepositoryUrl.fromEncoded(encodedUrl); try { long size = repositoryStoreService.size(url) - .orElseThrow(() -> ErrorResponder.builder() - .statusCode(HttpStatus.NOT_FOUND_404) - .type(ErrorType.NOT_FOUND) - .message("The repository " + encodedUrl + "(decoded value: '" + url + "') does not exist") - .haltError()); + .orElseThrow(() -> repositoryNotFound(encodedUrl, url)); return new ExtendedMailRepositoryResponse(url, size); } catch (MailRepositoryStore.MailRepositoryStoreException e) { throw ErrorResponder.builder() http://git-wip-us.apache.org/repos/asf/james-project/blob/e21b78b4/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java index 6c7ed64..6efce69 100644 --- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java +++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java @@ -67,7 +67,7 @@ public class MailRepositoryStoreService { } public Optional<List<MailKeyDTO>> listMails(MailRepositoryUrl url, Offset offset, Limit limit) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { - Optional<MailRepository> mailRepository = Optional.ofNullable(getRepository(url)); + Optional<MailRepository> mailRepository = mailRepositoryStore.get(url); ThrowingFunction<MailRepository, List<MailKeyDTO>> list = repository -> list(repository, offset, limit); return mailRepository.map(Throwing.function(list).sneakyThrow()); } @@ -81,7 +81,7 @@ public class MailRepositoryStoreService { } public Optional<Long> size(MailRepositoryUrl url) throws MailRepositoryStore.MailRepositoryStoreException { - Optional<MailRepository> mailRepository = Optional.ofNullable(getRepository(url)); + Optional<MailRepository> mailRepository = mailRepositoryStore.get(url); return mailRepository.map(Throwing.function(MailRepository::size).sneakyThrow()); } @@ -113,7 +113,7 @@ public class MailRepositoryStoreService { .orElseThrow(() -> ErrorResponder.builder() .statusCode(HttpStatus.NOT_FOUND_404) .type(ErrorResponder.ErrorType.NOT_FOUND) - .message(url + "does not exist") + .message(url.asString() + " does not exist") .haltError()); } http://git-wip-us.apache.org/repos/asf/james-project/blob/e21b78b4/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java index 6ea3072..4f752c9 100644 --- a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java +++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java @@ -246,7 +246,8 @@ public class MailRepositoriesRoutesTest { when() .get(MY_REPO_MAILS) .then() - .statusCode(HttpStatus.NOT_FOUND_404); + .statusCode(HttpStatus.NOT_FOUND_404) + .body("message", is("The repository 'url%3A%2F%2FmyRepo' (decoded value: 'url://myRepo') does not exist")); } @Test @@ -947,7 +948,7 @@ public class MailRepositoriesRoutesTest { .statusCode(HttpStatus.NOT_FOUND_404) .body("statusCode", is(404)) .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType())) - .body("message", is(URL_MY_REPO + "does not exist")); + .body("message", is(URL_MY_REPO.asString() + " does not exist")); } @Test @@ -960,7 +961,7 @@ public class MailRepositoriesRoutesTest { .statusCode(HttpStatus.NOT_FOUND_404) .body("statusCode", is(404)) .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType())) - .body("message", is(URL_MY_REPO + "does not exist")); + .body("message", is(URL_MY_REPO.asString() + " does not exist")); } @Test --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org