This repository has been archived by the owner on Mar 11, 2023. It is now read-only.
-
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.
Global exception handling for controller layer added using @Controlle…
…rAdvice
- Loading branch information
Showing
8 changed files
with
150 additions
and
28 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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/workflow/exceptions/GenericRuntimeException.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,40 @@ | ||
package com.workflow.exceptions; | ||
|
||
import java.util.Date; | ||
|
||
public class GenericRuntimeException extends RuntimeException { | ||
private Date timestamp; | ||
private String message; | ||
|
||
public GenericRuntimeException(String WFId, String name, String notAvailable) { | ||
this.timestamp = new Date(); | ||
this.message = WFId + " " + name + "-" + notAvailable; | ||
} | ||
|
||
public GenericRuntimeException(String WFId, String msg) { | ||
this.timestamp = new Date(); | ||
this.message = WFId + " : " + msg; | ||
} | ||
|
||
public GenericRuntimeException(String message) { | ||
this.message = message; | ||
} | ||
|
||
public Date getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(Date timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = 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
48 changes: 48 additions & 0 deletions
48
src/main/java/com/workflow/exceptions/RestExceptionHandler.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,48 @@ | ||
package com.workflow.exceptions; | ||
|
||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
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.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@ControllerAdvice | ||
public class RestExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
// other exception handlers | ||
|
||
@ExceptionHandler(NoDataFoundException.class) | ||
protected ResponseEntity<Object> handleEntityNotFound(NoDataFoundException ex) { | ||
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex); | ||
apiError.setMessage(ex.getMessage()); | ||
return buildResponseEntity(apiError); | ||
} | ||
|
||
@ExceptionHandler(GenericRuntimeException.class) | ||
protected ResponseEntity<Object> handleGenericException(GenericRuntimeException ex) { | ||
ApiError apiError = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex); | ||
apiError.setMessage(ex.getMessage()); | ||
return buildResponseEntity(apiError); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
protected ResponseEntity<Object> baseException(Exception ex) { | ||
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex); | ||
apiError.setMessage(ex.getMessage()); | ||
return buildResponseEntity(apiError); | ||
} | ||
|
||
@ExceptionHandler(RuntimeException.class) | ||
protected ResponseEntity<Object> baseRunTimeException(Exception ex) { | ||
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex); | ||
apiError.setMessage(ex.getMessage()); | ||
return buildResponseEntity(apiError); | ||
} | ||
|
||
private ResponseEntity<Object> buildResponseEntity(ApiError apiError) { | ||
return new ResponseEntity<>(apiError, apiError.getStatus()); | ||
} | ||
} |
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