Skip to content

Commit

Permalink
add Exceptions package
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuchitO committed Mar 21, 2024
1 parent 216748b commit 1cf85f5
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
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;
}
}
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)
);
}
}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;
import java.util.Optional;

import com.kampus.kbazaar.exceptions.NotFoundException;
import org.springframework.stereotype.Service;

@Service
Expand Down Expand Up @@ -29,9 +31,7 @@ public List<ProductResponse> getAll() {
public ProductResponse getBySku(String sku) {
Optional<Product> product = productRepository.findBySku(sku);
if (product.isEmpty()) {
// throw new ProductNotFoundException("Product not found");
// TODO: handle exception
return null;
throw new NotFoundException("Product not found");
}

return product.get().toResponse();
Expand Down

0 comments on commit 1cf85f5

Please sign in to comment.