-
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
8 changed files
with
90 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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/rest/Exceptions/ApiException.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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.rest.Exceptions; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
public class ApiException { | ||
private final String message; | ||
private final Throwable throwable; | ||
private final HttpStatus httpStatus; | ||
private final ZonedDateTime timestamp; | ||
|
||
public ApiException(String message, Throwable throwable, HttpStatus httpStatus, ZonedDateTime timestamp) { | ||
this.message = message; | ||
this.throwable = throwable; | ||
this.httpStatus = httpStatus; | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public Throwable getThrowable() { | ||
return throwable; | ||
} | ||
|
||
public HttpStatus getHttpStatus() { | ||
return httpStatus; | ||
} | ||
|
||
public ZonedDateTime getTimestamp() { | ||
return timestamp; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/example/rest/Exceptions/ApiExceptionHandler.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.rest.Exceptions; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
|
||
@ControllerAdvice | ||
public class ApiExceptionHandler { | ||
|
||
@ExceptionHandler(value = {ApiRequestException.class}) | ||
public ResponseEntity<Object> handleApiRequestException (ApiRequestException e) { | ||
HttpStatus badRequest = HttpStatus.BAD_REQUEST; | ||
ApiException apiException = new ApiException(e.getMessage(), e, badRequest, ZonedDateTime.now(ZoneId.of("Z"))); | ||
return new ResponseEntity<>(apiException, badRequest); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/example/rest/Exceptions/ApiRequestException.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.rest.Exceptions; | ||
|
||
public class ApiRequestException extends RuntimeException { | ||
public ApiRequestException(String message) { | ||
super(message); | ||
} | ||
|
||
public ApiRequestException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
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