Deepen domain error semantics so not-found, bad-request, and forbidden cases return correct HTTP status codes#42
Merged
Conversation
…lobalExceptionHandler Introduces two new application exception types extending BaseAppException so 400 and 403 responses flow through the ProblemDetail seam instead of leaking raw Java exceptions. Adds an explicit FORBIDDEN title case to the handler switch for consistency with the other mapped statuses. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tion exceptions - LobbyAccessPolicy: SecurityException -> ForbiddenException in ensureMember/ensureOwner; add Objects.requireNonNull(lobby) guard in both methods - EventServiceImpl: NoSuchElementException -> NotFoundException via EntityFinder.findOrThrow in all must* helpers; IllegalArgumentException -> BadRequestException for date-range checks - TaskServiceImpl: same NotFoundException pattern; wrap TaskStatus.valueOf with BadRequestException to prevent HTTP 500 on invalid status strings - LobbyServiceImpl: NoSuchElementException -> NotFoundException; IllegalArgumentException -> BadRequestException for owner-removal guard - EventEntity: remove IllegalArgumentException from @PrePersist; service layer validates startAt < endAt before save, making the entity-level check both duplicate and a 500 risk - EventService/TaskService interfaces: update @throws Javadoc to reflect actual exception types Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace all NoSuchElementException -> NotFoundException, IllegalArgumentException -> BadRequestException, SecurityException -> ForbiddenException assertions across all affected service and policy test files. Rename test methods accordingly (e.g. throwsNoSuchElement_ -> throwsNotFound_) for clarity. Add list_throwsBadRequest_whenStatusIsInvalid to cover the TaskStatus.valueOf guard. Remove all imports of NoSuchElementException from test files. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Purpose
Raw
NoSuchElementException,IllegalArgumentException, andSecurityExceptionleakedfrom event, task, and lobby modules into the generic catch-all handler, returning HTTP 500
for every not-found, bad-request, and forbidden case. This PR deepens the lookup and domain
error seam so all three scenarios use application exception types routed through
BaseAppException→GlobalExceptionHandler→ProblemDetailwith correct HTTP status codes.Type
Changes
Two new exception classes extend
BaseAppException:BadRequestException(400,common.bad_request) andForbiddenException(403,common.forbidden), following thesame pattern as the existing
NotFoundExceptionandConflictException.GlobalExceptionHandlergains an explicit
FORBIDDENtitle case for consistency.LobbyAccessPolicy.ensureMemberandensureOwnernow throwForbiddenExceptioninsteadof
SecurityException. Both methods also gain aObjects.requireNonNull(lobby)guard.All
mustUser,mustLobby,mustEvent, andmustTaskhelpers now passNotFoundExceptionsuppliers to
EntityFinder.findOrThrowinstead of throwingNoSuchElementExceptiondirectly.Date-range checks in
EventServiceImpland the owner-removal guard inLobbyServiceImplthrow
BadRequestExceptioninstead ofIllegalArgumentException.TaskServiceImpl.listwraps
TaskStatus.valueOfin a try/catch and throwsBadRequestExceptionon an invalidstatus string — previously a silent HTTP 500.
EventEntity.@PrePersisthad a duplicatestartAt < endAtcheck that also threwIllegalArgumentException. The service layer validates the same constraint beforerepo.save(),so the entity-level check was unreachable in normal flow and a latent HTTP 500 risk from
any future bypass. It is removed.
EventServiceandTaskServiceinterface@throwsJavadoc updated to reflect the actualthrown types — the old tags documented
NoSuchElementException,SecurityException, andIllegalArgumentException.All affected tests updated to assert
NotFoundException,BadRequestException, orForbiddenException. Test method names renamed accordingly (e.g.throwsNoSuchElement_→
throwsNotFound_). One new test added:list_throwsBadRequest_whenStatusIsInvalid.Files changed
common/exception/BadRequestException.javacommon/exception/ForbiddenException.javaconfig/GlobalExceptionHandler.javaFORBIDDENtitle caselobby/service/LobbyAccessPolicy.javaSecurityException→ForbiddenException; null guard onlobbyevent/service/EventService.java@throwsupdated to application exception typesevent/service/EventServiceImpl.javaNoSuchElementException→NotFoundException;IllegalArgumentException→BadRequestExceptionevent/domain/EventEntity.javaIllegalArgumentExceptionfrom@PrePersisttask/service/TaskService.java@throwsupdated to application exception typestask/service/TaskServiceImpl.javaNoSuchElementException→NotFoundException;TaskStatus.valueOfwrapped withBadRequestExceptionlobby/service/LobbyServiceImpl.javaNoSuchElementException→NotFoundException;IllegalArgumentException→BadRequestExceptionlobby/service/LobbyAccessPolicyTest.javaSecurityException→ForbiddenExceptionassertionsevent/service/EventServiceImplTest.javaevent/service/EventServiceImplConflictTest.javatask/service/TaskServiceImplTest.javalist_throwsBadRequest_whenStatusIsInvalidlobby/service/LobbyServiceImplTest.javaExpected result
Checklist
./gradlew checkpasses locally./gradlew jacocoTestReportpasses locally