-
Notifications
You must be signed in to change notification settings - Fork 16
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
132 additions
and
3 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
kbazaar/src/main/java/com/kampus/kbazaar/exceptions/ApiErrorResponse.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,59 @@ | ||
package com.kampus.kbazaar.exceptions; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class ApiErrorResponse { | ||
private LocalDateTime timestamp; | ||
private int status; | ||
private String error; | ||
private String message; | ||
private String path; | ||
|
||
public ApiErrorResponse(LocalDateTime timestamp, int status, String error, String message, String path) { | ||
this.timestamp = timestamp; | ||
this.status = status; | ||
this.error = error; | ||
this.message = message; | ||
this.path = path; | ||
} | ||
|
||
public LocalDateTime getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(LocalDateTime timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
|
||
public void setError(String error) { | ||
this.error = error; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
kbazaar/src/main/java/com/kampus/kbazaar/exceptions/ControllerExceptionHandler.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,56 @@ | ||
package com.kampus.kbazaar.exceptions; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@RestControllerAdvice | ||
public class ControllerExceptionHandler { | ||
|
||
@ExceptionHandler(value = {MethodArgumentNotValidException.class}) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ApiErrorResponse handleBadRequestException(MethodArgumentNotValidException notValidException, WebRequest request) { | ||
List<String> error = notValidException.getFieldErrors() | ||
.stream() | ||
.map(f -> f.getField() + " " + f.getDefaultMessage()) | ||
.toList(); | ||
|
||
return new ApiErrorResponse( | ||
LocalDateTime.now(), | ||
HttpStatus.BAD_REQUEST.value(), | ||
HttpStatus.BAD_REQUEST.getReasonPhrase(), | ||
String.join(", ", error), | ||
request.getDescription(false) | ||
); | ||
} | ||
|
||
@ExceptionHandler(value = {NotFoundException.class}) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public ApiErrorResponse handleNotFoundRequestException(NotFoundException notValidException, WebRequest request) { | ||
return new ApiErrorResponse( | ||
LocalDateTime.now(), | ||
HttpStatus.NOT_FOUND.value(), | ||
HttpStatus.NOT_FOUND.getReasonPhrase(), | ||
notValidException.getMessage(), | ||
request.getDescription(false) | ||
); | ||
} | ||
|
||
@ExceptionHandler(value = {InternalServerException.class}) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public ApiErrorResponse handleInternalServerException(InternalServerException internalServerException, WebRequest request) { | ||
return new ApiErrorResponse( | ||
LocalDateTime.now(), | ||
HttpStatus.INTERNAL_SERVER_ERROR.value(), | ||
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), | ||
internalServerException.getMessage(), | ||
request.getDescription(false) | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
kbazaar/src/main/java/com/kampus/kbazaar/exceptions/InternalServerException.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,7 @@ | ||
package com.kampus.kbazaar.exceptions; | ||
|
||
public class InternalServerException extends RuntimeException { | ||
public InternalServerException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
kbazaar/src/main/java/com/kampus/kbazaar/exceptions/NotFoundException.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,7 @@ | ||
package com.kampus.kbazaar.exceptions; | ||
|
||
public class NotFoundException extends RuntimeException { | ||
public NotFoundException(String message) { | ||
super(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