Add ID validation to GetTrackHasGenreByIdHandler

Add validation to reject ID values <= 0 in GetTrackHasGenreByIdHandler
  before calling the service layer. This prevents the protobuf default value
  of 0 from being processed, which would incorrectly pass the null check in
  the service but represents an invalid ID.

  Changes:
  - Extract ID from GetTrackHasGenreByIdRequest and validate id <= 0
  - Throw IllegalArgumentException with message "ID must be greater than 0"
    if validation fails
  - Add specific catch block for IllegalArgumentException that returns
    HTTP 400 status code with "Validation error: " prefix
  - Mirrors validation pattern from CreateTrackHasGenreHandler

  This aligns with validation patterns used in similar handlers throughout
  the codebase (GetTrackHasArtistByIdHandler, GetTrackHasComposerByIdHandler)
  and ensures consistent error handling for invalid ID inputs.

  Modified:
  - src/main/java/com/mediamanager/service/delegate/handler/trackhasgenre/GetTrackHasGenreByIdHandler.java

  🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
Gustavo Henrique Santos Souza de Miranda 2025-12-09 19:21:04 -03:00
parent 19ddf262df
commit be24b68923
1 changed files with 9 additions and 0 deletions

View File

@ -32,6 +32,10 @@ public class GetTrackHasGenreByIdHandler implements ActionHandler {
TrackHasGenreMessages.GetTrackHasGenreByIdRequest.parseFrom(requestPayload);
int id = getByIdRequest.getId();
if (id <= 0) {
throw new IllegalArgumentException("ID must be greater than 0");
}
Optional<TrackHasGenre> trackHasGenreOpt = trackHasGenreService.getTrackHasGenreById(id);
if (trackHasGenreOpt.isEmpty()){
@ -47,6 +51,11 @@ public class GetTrackHasGenreByIdHandler implements ActionHandler {
return TransportProtocol.Response.newBuilder()
.setStatusCode(200)
.setPayload(getByIdResponse.toByteString());
} catch (IllegalArgumentException e) {
logger.error("Validation error", e);
return TransportProtocol.Response.newBuilder()
.setStatusCode(400)
.setPayload(ByteString.copyFromUtf8("Validation error: " + e.getMessage()));
} catch (Exception e) {
logger.error("Error getting track has genre by ID", e);
return TransportProtocol.Response.newBuilder()