-
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.
Browse files
Browse the repository at this point in the history
…ned_url [FEAT] 사진 업로드를 위한 S3 + Presigned URL 구현
- Loading branch information
Showing
21 changed files
with
565 additions
and
111 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
69 changes: 0 additions & 69 deletions
69
src/main/java/org/sopt/lequuServer/global/common/exception/GlobalExceptionHandler.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
src/main/java/org/sopt/lequuServer/global/common/exception/enums/ErrorType.java
This file was deleted.
Oops, something went wrong.
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
130 changes: 130 additions & 0 deletions
130
src/main/java/org/sopt/lequuServer/global/exception/GlobalExceptionHandler.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,130 @@ | ||
package org.sopt.lequuServer.global.exception; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.sopt.lequuServer.global.common.dto.ApiResponse; | ||
import org.sopt.lequuServer.global.exception.enums.ErrorType; | ||
import org.sopt.lequuServer.global.exception.model.CustomException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.MissingRequestHeaderException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import jakarta.validation.UnexpectedTypeException; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.sopt.lequuServer.global.exception.enums.ErrorType.*; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
@RequiredArgsConstructor | ||
public class GlobalExceptionHandler { | ||
|
||
|
||
/** | ||
* 400 VALIDATION_ERROR | ||
*/ | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
protected ApiResponse<?> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) { | ||
|
||
Errors errors = e.getBindingResult(); | ||
Map<String, String> validateDetails = new HashMap<>(); | ||
|
||
for (FieldError error : errors.getFieldErrors()) { | ||
String validKeyName = String.format("valid_%s", error.getField()); | ||
validateDetails.put(validKeyName, error.getDefaultMessage()); | ||
} | ||
return ApiResponse.error(REQUEST_VALIDATION_ERROR, validateDetails); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(UnexpectedTypeException.class) | ||
protected ApiResponse<?> handleUnexprectedTypeException(final UnexpectedTypeException e) { | ||
return ApiResponse.error(INVALID_TYPE_ERROR); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
public ApiResponse<?> handlerMethodArgumentTypeMismatchException(final MethodArgumentTypeMismatchException e) { | ||
return ApiResponse.error(INVALID_TYPE_ERROR); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MissingRequestHeaderException.class) | ||
protected ApiResponse<?> handlerMissingRequestHeaderException(final MissingRequestHeaderException e) { | ||
return ApiResponse.error(INVALID_MISSING_HEADER_ERROR); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
protected ApiResponse<?> handlerHttpMessageNotReadableException(final HttpMessageNotReadableException e) { | ||
return ApiResponse.error(INVALID_HTTP_REQUEST_ERROR); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
protected ApiResponse<?> handlerHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) { | ||
return ApiResponse.error(INVALID_HTTP_METHOD_ERROR); | ||
} | ||
|
||
|
||
/** | ||
* 500 INTERNEL_SERVER_ERROR | ||
*/ | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(Exception.class) | ||
protected ApiResponse<Exception> handleException(final Exception e, final HttpServletRequest request) { | ||
|
||
log.error("500 Error Occured: {}", e.getMessage(), e); | ||
|
||
return ApiResponse.error(INTERNAL_SERVER_ERROR, e); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ApiResponse<Exception> handlerIllegalArgumentException(final IllegalArgumentException e, final HttpServletRequest request) { | ||
return ApiResponse.error(ErrorType.INTERNAL_SERVER_ERROR, e); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(IOException.class) | ||
public ApiResponse<Exception> handlerIOException(final IOException e, final HttpServletRequest request) { | ||
return ApiResponse.error(ErrorType.INTERNAL_SERVER_ERROR, e); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(RuntimeException.class) | ||
public ApiResponse<Exception> handlerRuntimeException(final RuntimeException e, final HttpServletRequest request) { | ||
|
||
if (e.getMessage() != null) { | ||
return ApiResponse.error(ErrorType.INTERNAL_SERVER_ERROR, e.getMessage(), e); | ||
} else { | ||
return ApiResponse.error(ErrorType.INTERNAL_SERVER_ERROR, e); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* CUSTOM_ERROR | ||
*/ | ||
@ExceptionHandler(CustomException.class) | ||
protected ResponseEntity<ApiResponse<?>> handleCustomException(CustomException e) { | ||
|
||
log.error("CustomException Occured: {}", e.getMessage(), e); | ||
|
||
return ResponseEntity.status(e.getHttpStatus()) | ||
.body(ApiResponse.error(e.getErrorType(), e.getMessage())); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/sopt/lequuServer/global/exception/enums/ErrorType.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,36 @@ | ||
package org.sopt.lequuServer.global.exception.enums; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum ErrorType { | ||
|
||
/** | ||
* 400 BAD REQUEST | ||
*/ | ||
REQUEST_VALIDATION_ERROR(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
INVALID_TYPE_ERROR(HttpStatus.BAD_REQUEST, "잘못된 타입이 입력되었습니다."), | ||
INVALID_MISSING_HEADER_ERROR(HttpStatus.BAD_REQUEST, "요청에 필요한 헤더값이 존재하지 않습니다."), | ||
INVALID_HTTP_REQUEST_ERROR(HttpStatus.BAD_REQUEST, "허용되지 않는 문자열이 입력되었습니다."), | ||
INVALID_HTTP_METHOD_ERROR(HttpStatus.BAD_REQUEST, "지원되지 않는 HTTP method 요청입니다."), | ||
IMAGE_EXTENSION_ERROR(HttpStatus.BAD_REQUEST, "이미지 확장자는 jpg, png, webp만 가능합니다."), | ||
IMAGE_SIZE_ERROR(HttpStatus.BAD_REQUEST, "이미지 사이즈는 5MB를 넘을 수 없습니다."), | ||
|
||
/** | ||
* 500 INTERNAL SERVER ERROR | ||
*/ | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "알 수 없는 서버 에러가 발생했습니다."), | ||
GET_UPLOAD_PRESIGNED_URL_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "업로드를 위한 Presigned URL 획득에 실패했습니다.") | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
|
||
public int getHttpStatusCode() { | ||
return httpStatus.value(); | ||
} | ||
} |
Oops, something went wrong.