-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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) { | ||
} |
This file contains 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
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); | ||
} | ||
} |
6 changes: 1 addition & 5 deletions
6
src/main/java/com/ypm/exception/PlayListNotFoundException.java
This file contains 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
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)); | ||
} | ||
} |
This file contains 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