Skip to content

Commit

Permalink
Merge pull request #159 from AppLinkers/develop
Browse files Browse the repository at this point in the history
Deploy for manage error code
  • Loading branch information
yuseogi0218 authored Jan 9, 2024
2 parents ca42907 + 815c3ca commit 34f67fd
Show file tree
Hide file tree
Showing 37 changed files with 446 additions and 331 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.laser.ordermanage.common.cloud.aws;

import com.laser.ordermanage.common.exception.CustomCommonException;
import com.laser.ordermanage.common.exception.ErrorCode;
import com.laser.ordermanage.common.exception.CommonErrorCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -33,7 +33,7 @@ public String upload(String folder, MultipartFile multipartFile) {
RequestBody requestBody = RequestBody.fromInputStream(multipartFile.getInputStream(), multipartFile.getSize());
return putObject(requestBody, key);
} catch (IOException e) {
throw new CustomCommonException(ErrorCode.UNABLE_TO_AWS_S3_UPLOAD);
throw new CustomCommonException(CommonErrorCode.UNABLE_TO_AWS_S3_UPLOAD);
}
}

Expand All @@ -46,7 +46,7 @@ public String upload(String folder, String filePath) {
file.delete();
return fileUrl;
} catch (IOException e) {
throw new CustomCommonException(ErrorCode.UNABLE_TO_AWS_S3_UPLOAD);
throw new CustomCommonException(CommonErrorCode.UNABLE_TO_AWS_S3_UPLOAD);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.laser.ordermanage.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
@Getter
public enum CommonErrorCode implements ErrorCode {

// 400 BAD_REQUEST 잘못된 요청
BAD_REQUEST("COMMON_400_01", HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),

MISMATCH_PARAMETER_TYPE("COMMON_400_02", HttpStatus.BAD_REQUEST, " 파라미터의 타입이 올바르지 않습니다."),
REQUIRED_PARAMETER("COMMON_400_03", HttpStatus.BAD_REQUEST, " 파라미터는 필수 입력값입니다."),
INVALID_PARAMETER("COMMON_400_04", HttpStatus.BAD_REQUEST, " 파라미터가 올바르지 않습니다."),

INVALID_REQUEST_BODY_FIELDS("COMMON_400_05", HttpStatus.BAD_REQUEST, ""),
REQUIRED_REQUEST_BODY("COMMON_400_06", HttpStatus.BAD_REQUEST, "Request Body 가 필요한 요청 입니다."),

REQUIRED_COOKIE("COMMON_400_07", HttpStatus.BAD_REQUEST, " 쿠키값이 존재하지 않습니다."),

// 401 UNAUTHORIZED 인증 자격 정보가 유효하지 않음
UNAUTHORIZED("COMMON_401_01", HttpStatus.UNAUTHORIZED, "인증 자격 정보가 유효하지 않습니다."),

// 403 FORBIDDEN 인증 필요
FORBIDDEN("COMMON_403_01", HttpStatus.FORBIDDEN, "인증이 필요합니다."),

// 404 NOT_FOUND 리소스가 존재하지 않음
NOT_FOUND("COMMON_404_01", HttpStatus.NOT_FOUND, "리소스가 존재하지 않습니다."),

// 405 METHOD_NOT_ALLOWED 허용하지 않은 Http Method
METHOD_NOT_ALLOWED("COMMON_405_01", HttpStatus.METHOD_NOT_ALLOWED, "해당 요청에는 지원하지 않은 HTTP 메서드 입니다."),

// 413 PAYLOAD_TOO_LARGE
REQUEST_SIZE_EXCEEDED("COMMON_413_01", HttpStatus.PAYLOAD_TOO_LARGE, "요청의 크기가 100MB를 초과합니다."),
REQUEST_FILE_SIZE_EXCEEDED("COMMON_413_02", HttpStatus.PAYLOAD_TOO_LARGE, "요청 파일의 크기가 100MB를 초과합니다."),

// 500 INTERNAL SERVER ERROR 서버 에러
INTERNAL_SERVER_ERROR("COMMON_500_01", HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러 입니다."),
UNKNOWN_ERROR("COMMON_500_02", HttpStatus.INTERNAL_SERVER_ERROR, "알 수 없는 오류가 발생했습니다."),
UNABLE_TO_SEND_EMAIL("COMMON_500_03", HttpStatus.INTERNAL_SERVER_ERROR, "메일 전송이 불가능합니다."),
UNABLE_TO_AWS_S3_UPLOAD("COMMON_500_04", HttpStatus.INTERNAL_SERVER_ERROR, "AWS S3 에 파일 업로드가 불가능합니다."),
UNABLE_TO_EXTRACT_THUMBNAIL("COMMON_500_05", HttpStatus.INTERNAL_SERVER_ERROR, "썸네일 추출이 불가능합니다.");

private final String code;
private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.laser.ordermanage.common.exception.dto.response.ErrorResponse;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Getter
public class CustomCommonException extends RuntimeException{
Expand All @@ -22,11 +23,12 @@ public CustomCommonException(ErrorCode errorcode, String parameter) {
this.errorCode = errorcode.getCode();
}

public ErrorResponse toErrorResponse() {
return ErrorResponse.builder()
.httpStatus(this.httpStatus)
public ResponseEntity<ErrorResponse> toErrorResponse() {
ErrorResponse response = ErrorResponse.builder()
.errorCode(this.errorCode)
.message(this.getMessage())
.build();

return ResponseEntity.status(this.httpStatus).body(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,9 @@
package com.laser.ordermanage.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
@Getter
public enum ErrorCode {

// 400 BAD_REQUEST 잘못된 요청
BAD_REQUEST("-000", HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
MISSING_JWT_TOKEN("-001", HttpStatus.BAD_REQUEST, "JWT 토큰 정보가 요청에 포함되지 않았습니다."),
INVALID_CREDENTIALS("-002", HttpStatus.BAD_REQUEST, "ID 또는 비밀번호가 올바르지 않습니다."),

INVALID_PARAMETER("-003", HttpStatus.BAD_REQUEST, " 파라미터가 올바르지 않습니다."),
INVALID_PARAMETER_TYPE("-004", HttpStatus.BAD_REQUEST, " 파라미터의 타입이 올바르지 않습니다."),
INVALID_FIELDS("-005", HttpStatus.BAD_REQUEST, ""),

MISSING_COOKIE("-006", HttpStatus.BAD_REQUEST, " 쿠키값이 존재하지 않습니다."),
MISSING_QUERY_PARAMETER("-007", HttpStatus.BAD_REQUEST, " 쿼리 파라미터 값이 존재하지 않습니다."),

REQUEST_FILE_SIZE_EXCEED("-008", HttpStatus.BAD_REQUEST, "요청 파일의 크기가 100MB를 초과합니다."),
INVALID_FILE_EXTENSION("-009", HttpStatus.BAD_REQUEST, "지원하지 않는 파일 형식입니다."),
INVALID_INGREDIENT("-010", HttpStatus.BAD_REQUEST, "지원하지 않는 재료입니다."),
INVALID_ORDER_STAGE("-011", HttpStatus.BAD_REQUEST, " 단계의 거래는 해당 요청을 수행할 수 없습니다."),
LAST_DRAWING_DELETE("-012", HttpStatus.BAD_REQUEST, "거래에는 최소 하나의 도면이 필요합니다.마지막 도면은 삭제할 수 없습니다."),
MISSING_QUOTATION_FILE("-013", HttpStatus.BAD_REQUEST, "견적서 최초 작성 시, 견적서 파일은 필수 사항입니다."),
MISSING_QUOTATION("-014", HttpStatus.BAD_REQUEST, "거래의 견적서가 존재하지 않습니다."),
MISSING_PURCHASE_ORDER("-015", HttpStatus.BAD_REQUEST, "거래의 발주서가 존재하지 않습니다."),
DEFAULT_DELIVERY_ADDRESS_DELETE("-016", HttpStatus.BAD_REQUEST, "기본 배송지는 삭제할 수 없습니다."),
MISSING_PURCHASE_ORDER_FILE("-017", HttpStatus.BAD_REQUEST, "발주서 최초 작성 시, 발주서 파일은 필수 사항입니다."),

// 401 UNAUTHORIZED 인증 자격 정보가 유효하지 않음
UNAUTHORIZED("-100", HttpStatus.UNAUTHORIZED, "인증 자격 정보가 유효하지 않습니다."),
INVALID_JWT_TOKEN("-101", HttpStatus.UNAUTHORIZED, "JWT Token 정보가 유효하지 않습니다."),
EXPIRED_JWT_TOKEN("-102", HttpStatus.UNAUTHORIZED, "JWT 토큰 정보가 만료되었습니다."),
UNSUPPORTED_JWT_TOKEN("-103", HttpStatus.UNAUTHORIZED, "지원되지 않는 JWT 토큰 입니다."),
UNAUTHORIZED_JWT_TOKEN("-104", HttpStatus.UNAUTHORIZED, "JWT 토큰에 권한정보가 존재하지 않습니다."),

INVALID_ACCESS_JWT_TOKEN("-105", HttpStatus.UNAUTHORIZED, "Access JWT 토큰 정보가 유효하지 않습니다."),
INVALID_REFRESH_JWT_TOKEN("-106", HttpStatus.UNAUTHORIZED, "Refresh JWT 토큰 정보가 유효하지 않습니다."),

INVALID_VERIFY_CODE("-107", HttpStatus.UNAUTHORIZED, "인증 코드 정보가 유효하지 않습니다."),

INVALID_CHANGE_PASSWORD_JWT_TOKEN("-108", HttpStatus.UNAUTHORIZED, "Change Password JWT 토큰 정보가 유효하지 않습니다."),

// 403 FORBIDDEN 인증 필요
FORBIDDEN("-300", HttpStatus.FORBIDDEN, "인증이 필요합니다."),
DENIED_ACCESS("-301", HttpStatus.FORBIDDEN, "해당 요청에 대한 접근 권한이 없습니다."),
DENIED_ACCESS_TO_ENTITY("-302", HttpStatus.FORBIDDEN, " 엔티티에 접근 권한이 없습니다."),

// 404 NOT_FOUND 리소스가 존재하지 않음
NOT_FOUND("-400", HttpStatus.NOT_FOUND, "리소스가 존재하지 않습니다."),
NOT_FOUND_VERIFY_CODE("-401", HttpStatus.NOT_FOUND, "이메일에 해당하는 인증 코드가 존재하지 않습니다."),
NOT_FOUND_ENTITY("-402", HttpStatus.NOT_FOUND, " 엔티티가 존재하지 않습니다."),

// 405 METHOD_NOT_ALLOWED 허용하지 않은 Http Method
METHOD_NOT_ALLOWED("-405", HttpStatus.METHOD_NOT_ALLOWED, "해당 요청에는 지원하지 않은 HTTP 메서드 입니다."),

// 500 INTERNAL SERVER ERROR 서버 에러
INTERNAL_SERVER_ERROR("-500", HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러 입니다."),
UNKNOWN_ERROR("-501", HttpStatus.INTERNAL_SERVER_ERROR, "알 수 없는 오류가 발생했습니다."),
UNABLE_TO_SEND_EMAIL("-502", HttpStatus.INTERNAL_SERVER_ERROR, "메일 전송이 불가능합니다."),
UNABLE_TO_AWS_S3_UPLOAD("-503", HttpStatus.INTERNAL_SERVER_ERROR, "AWS S3 에 파일 업로드가 불가능합니다."),
UNABLE_TO_EXTRACT_THUMBNAIL("-504", HttpStatus.INTERNAL_SERVER_ERROR, "썸네일 추출이 불가능합니다.");

private final String code;
private final HttpStatus httpStatus;
private final String message;
public interface ErrorCode {
String getCode();
HttpStatus getHttpStatus();
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.laser.ordermanage.common.exception;

import com.laser.ordermanage.user.exception.UserErrorCode;
import jakarta.validation.ConstraintViolationException;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -21,82 +24,100 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleUnknownException(Exception e) {
CustomCommonException exception = new CustomCommonException(CommonErrorCode.UNKNOWN_ERROR);
return exception.toErrorResponse();
}

@ExceptionHandler(CustomCommonException.class)
public ResponseEntity<?> handleCustomCommonException(CustomCommonException e) {
return ResponseEntity.status(e.getHttpStatus()).body(e.toErrorResponse());
return e.toErrorResponse();
}

@ExceptionHandler(InsufficientAuthenticationException.class)
public ResponseEntity<?> handleAuthenticationException(InsufficientAuthenticationException e) {
CustomCommonException exception = new CustomCommonException(UserErrorCode.MISSING_JWT);
return exception.toErrorResponse();
}

@ExceptionHandler(BadCredentialsException.class)
public ResponseEntity<?> handleBadCredentialException(BadCredentialsException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.INVALID_CREDENTIALS);
return ResponseEntity.badRequest().body(exception.toErrorResponse());
CustomCommonException exception = new CustomCommonException(UserErrorCode.INVALID_CREDENTIALS);
return exception.toErrorResponse();
}

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<?> handleAccessDeniedException(Authentication e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.DENIED_ACCESS);
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
CustomCommonException exception = new CustomCommonException(UserErrorCode.DENIED_ACCESS);
return exception.toErrorResponse();
}

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<?> handleAuthenticationException(AuthenticationException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.MISSING_JWT_TOKEN);
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
CustomCommonException exception = new CustomCommonException(CommonErrorCode.MISMATCH_PARAMETER_TYPE, e.getName());
return exception.toErrorResponse();
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List<ObjectError> objectErrorList = e.getBindingResult().getAllErrors();
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<?> handleMissingRequestParameterException(MissingServletRequestParameterException e) {
CustomCommonException exception = new CustomCommonException(CommonErrorCode.REQUIRED_PARAMETER, e.getParameterName());
return exception.toErrorResponse();
}

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<?> handleConstraintViolationException(ConstraintViolationException e) {
StringBuilder sb = new StringBuilder();
objectErrorList.forEach(
objectError -> sb.append(objectError.getDefaultMessage())
e.getConstraintViolations().forEach(
constraintViolation -> sb.append(constraintViolation.getMessage())
);

CustomCommonException exception = new CustomCommonException(ErrorCode.INVALID_FIELDS, sb.toString());
CustomCommonException exception = new CustomCommonException(CommonErrorCode.INVALID_PARAMETER, sb.toString());

return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
return exception.toErrorResponse();
}

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<?> handleConstraintViolationException(ConstraintViolationException e) {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List<ObjectError> objectErrorList = e.getBindingResult().getAllErrors();

StringBuilder sb = new StringBuilder();
e.getConstraintViolations().forEach(
constraintViolation -> sb.append(constraintViolation.getMessage())
objectErrorList.forEach(
objectError -> sb.append(objectError.getDefaultMessage())
);

CustomCommonException exception = new CustomCommonException(ErrorCode.INVALID_FIELDS, sb.toString());
CustomCommonException exception = new CustomCommonException(CommonErrorCode.INVALID_REQUEST_BODY_FIELDS, sb.toString());

return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
return exception.toErrorResponse();
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.INVALID_PARAMETER_TYPE, e.getName());
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
CustomCommonException exception = new CustomCommonException(CommonErrorCode.REQUIRED_REQUEST_BODY);
return exception.toErrorResponse();
}

@ExceptionHandler(MissingRequestCookieException.class)
public ResponseEntity<?> handleMissingRequestCookieException(MissingRequestCookieException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.MISSING_COOKIE, e.getCookieName());
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<?> handleMissingRequestParameterException(MissingServletRequestParameterException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.MISSING_QUERY_PARAMETER, e.getParameterName());
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
CustomCommonException exception = new CustomCommonException(CommonErrorCode.REQUIRED_COOKIE, e.getCookieName());
return exception.toErrorResponse();
}

@ExceptionHandler(SizeLimitExceededException.class)
public ResponseEntity<?> handleSizeLimitExceededException(SizeLimitExceededException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.REQUEST_FILE_SIZE_EXCEED);
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
CustomCommonException exception = new CustomCommonException(CommonErrorCode.REQUEST_SIZE_EXCEEDED);
return exception.toErrorResponse();
}

@ExceptionHandler(FileSizeLimitExceededException.class)
public ResponseEntity<?> handleFileSizeLimitExceededException(FileSizeLimitExceededException e) {
CustomCommonException exception = new CustomCommonException(CommonErrorCode.REQUEST_FILE_SIZE_EXCEEDED);
return exception.toErrorResponse();
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
CustomCommonException exception = new CustomCommonException(ErrorCode.METHOD_NOT_ALLOWED);
return ResponseEntity.status(exception.getHttpStatus()).body(exception.toErrorResponse());
CustomCommonException exception = new CustomCommonException(CommonErrorCode.METHOD_NOT_ALLOWED);
return exception.toErrorResponse();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.laser.ordermanage.common.exception.dto.response;

import lombok.Builder;
import org.springframework.http.HttpStatus;

@Builder
public record ErrorResponse (
HttpStatus httpStatus,
String errorCode,
String message
) { }
Loading

0 comments on commit 34f67fd

Please sign in to comment.