Skip to content

Commit

Permalink
Add GlobalExceptionHandler.java
Browse files Browse the repository at this point in the history
  • Loading branch information
daverbk committed Apr 14, 2024
1 parent ace6781 commit 952eb84
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/ypm/dto/request/MergePlayListsRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

public record MergePlayListsRequest(String mergedPlayListTitle,
List<String> playListsIds,
boolean deleteAfterMerge) {
}
boolean deleteAfterMerge) { }
6 changes: 6 additions & 0 deletions src/main/java/com/ypm/dto/response/ExceptionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ypm.dto.response;

import java.time.Instant;

public record ExceptionResponse(int code, String message, Instant date) {
}
56 changes: 56 additions & 0 deletions src/main/java/com/ypm/error/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.ypm.error;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.ypm.dto.response.ExceptionResponse;
import com.ypm.exception.PlayListNotFoundException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.io.IOException;
import java.time.Instant;

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

public GlobalExceptionHandler() {
super();
}

@ExceptionHandler({GoogleJsonResponseException.class})
public ResponseEntity<?> handleBadRequest(final GoogleJsonResponseException ex,
final WebRequest request) {

ExceptionResponse exceptionResponse = new ExceptionResponse(
ex.getStatusCode(), ex.getDetails().getMessage(), Instant.now());

return handleExceptionInternal(ex, exceptionResponse,
new HttpHeaders(), HttpStatus.valueOf(ex.getStatusCode()), request);
}

@ExceptionHandler({PlayListNotFoundException.class})
public ResponseEntity<?> handleBadRequest(final PlayListNotFoundException ex,
final WebRequest request) {

ExceptionResponse exceptionResponse = new ExceptionResponse(
HttpStatus.NOT_FOUND.value(), ex.getMessage(), Instant.now());

return handleExceptionInternal(ex, exceptionResponse,
new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}

@ExceptionHandler({IOException.class})
public ResponseEntity<?> handleInternal(final IOException ex,
final WebRequest request) {

ExceptionResponse exceptionResponse = new ExceptionResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage(), Instant.now());

return handleExceptionInternal(ex, exceptionResponse,
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.ypm.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class PlayListNotFoundException extends RuntimeException {

public PlayListNotFoundException(String identifier, String message) {
super(String.format("Playlist with %s identifier was not found %n%s%n", identifier, message));
super(String.format("Playlist with the '%s' identifier was not found. %s", identifier, message));
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/ypm/service/PlayListService.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public Playlist getPlayListById(String accessToken, String playListId) throws IO
.findFirst()
.orElseThrow(() ->
new PlayListNotFoundException(playListId,
"At some stage we were not able to find one of the request-related playlists."
+ " Please repeat the request."));
"At some stage we were not able to find one of the request-related playlists."));
}

public void deletePlayList(String accessToken, String playListId) throws IOException {
Expand Down

0 comments on commit 952eb84

Please sign in to comment.