-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #457 from this-Aditya/entity-exceptions
Sending Response entities as exceptions.
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/org/radarbase/appserver/exception/entity/ErrorDetails.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,17 @@ | ||
package org.radarbase.appserver.exception.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.Instant; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ErrorDetails { | ||
Instant timestamp; | ||
int status; | ||
String message; | ||
String path; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/radarbase/appserver/exception/entity/ErrorDetailsWithCause.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,14 @@ | ||
package org.radarbase.appserver.exception.entity; | ||
|
||
import lombok.Getter; | ||
|
||
import java.time.Instant; | ||
|
||
@Getter | ||
public class ErrorDetailsWithCause extends ErrorDetails { | ||
String cause; | ||
public ErrorDetailsWithCause(Instant timestamp, int status, String cause, String message, String path) { | ||
super(timestamp, status, message, path); | ||
this.cause = cause; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/org/radarbase/appserver/exception/handler/ResponseEntityExceptionHandler.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,73 @@ | ||
package org.radarbase.appserver.exception.handler; | ||
|
||
import org.radarbase.appserver.exception.*; | ||
import org.radarbase.appserver.exception.entity.ErrorDetails; | ||
import org.radarbase.appserver.exception.entity.ErrorDetailsWithCause; | ||
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 org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.time.Instant; | ||
|
||
@ControllerAdvice | ||
public class ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(Exception.class) | ||
public final ResponseEntity<ErrorDetails> handleUnhandledException(Exception ex, WebRequest request) { | ||
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; | ||
ErrorDetails error; | ||
if (ex.getCause() == null) { | ||
error = new ErrorDetails(Instant.now(), status.value(), ex.getMessage(), request.getDescription(false)); | ||
} else { | ||
error = new ErrorDetailsWithCause(Instant.now(), status.value(), ex.getCause().getMessage(), ex.getMessage(), request.getDescription(false)); | ||
} | ||
return new ResponseEntity<>(error, status); | ||
} | ||
|
||
|
||
@ExceptionHandler(InvalidProjectDetailsException.class) | ||
public final ResponseEntity<ErrorDetails> handleInvalidProjectDetailsException(Exception ex, WebRequest request) throws Exception { | ||
return handleEntityWithCause(ex, request); | ||
} | ||
|
||
@ExceptionHandler(AlreadyExistsException.class) | ||
public final ResponseEntity<ErrorDetails> handleAlreadyExistsException(Exception ex, WebRequest request) throws Exception { | ||
return handleEntityWithoutCause(ex, request); | ||
} | ||
|
||
@ExceptionHandler(NotFoundException.class) | ||
public final ResponseEntity<ErrorDetails> handleNotFoundException(Exception ex, WebRequest request) throws Exception { | ||
return handleEntityWithoutCause(ex, request); | ||
} | ||
|
||
@ExceptionHandler(InvalidNotificationDetailsException.class) | ||
public final ResponseEntity<ErrorDetails> handleInvalidNotificationDetailsException(Exception ex, WebRequest request) { | ||
return handleEntityWithoutCause(ex, request); | ||
} | ||
|
||
@ExceptionHandler(InvalidUserDetailsException.class) | ||
public final ResponseEntity<ErrorDetails> handleInvalidUserDetailsException(Exception ex, WebRequest request) { | ||
return handleEntityWithCause(ex, request); | ||
} | ||
|
||
public ResponseEntity<ErrorDetails> handleEntityWithCause(Exception ex, WebRequest request) { | ||
String cause = ex.getCause() != null ? ex.getCause().getMessage() : null; | ||
HttpStatus status = ex.getClass().getAnnotation(ResponseStatus.class).value(); | ||
ErrorDetails error; | ||
if (cause != null) { | ||
error = new ErrorDetailsWithCause(Instant.now(), status.value(), cause, ex.getMessage(), request.getDescription(false)); | ||
} else { | ||
error = new ErrorDetails(Instant.now(), status.value(), ex.getMessage(), request.getDescription(false)); | ||
} | ||
return new ResponseEntity<>(error, status); | ||
} | ||
|
||
public ResponseEntity<ErrorDetails> handleEntityWithoutCause(Exception ex, WebRequest request) { | ||
HttpStatus status = ex.getClass().getAnnotation(ResponseStatus.class).value(); | ||
ErrorDetails error = new ErrorDetails(Instant.now(), status.value(), ex.getMessage(), request.getDescription(false)); | ||
return new ResponseEntity<>(error, status); | ||
} | ||
} |