diff --git a/src/main/java/com/tiki/server/auth/controller/AuthController.java b/src/main/java/com/tiki/server/auth/controller/AuthController.java index 0114947f..7dd1b6e2 100644 --- a/src/main/java/com/tiki/server/auth/controller/AuthController.java +++ b/src/main/java/com/tiki/server/auth/controller/AuthController.java @@ -1,5 +1,6 @@ package com.tiki.server.auth.controller; +import com.tiki.server.auth.controller.docs.AuthControllerDocs; import com.tiki.server.auth.dto.request.SignInRequest; import com.tiki.server.auth.dto.response.ReissueGetResponse; import com.tiki.server.auth.dto.response.SignInGetResponse; @@ -19,10 +20,11 @@ @RestController @RequiredArgsConstructor @RequestMapping("api/v1/auth") -public class AuthController { +public class AuthController implements AuthControllerDocs { private final AuthService authService; + @Override @PostMapping("/sign-in") public ResponseEntity> signIn(@RequestBody SignInRequest request) { val response = authService.signIn(request); @@ -30,6 +32,7 @@ public ResponseEntity> signIn(@RequestBody Si .body(SuccessResponse.success(SUCCESS_SIGN_IN.getMessage(), response)); } + @Override @GetMapping("/reissue") public ResponseEntity> reissue(HttpServletRequest httpServletRequest) { val response = authService.reissueToken(httpServletRequest); diff --git a/src/main/java/com/tiki/server/auth/controller/docs/AuthControllerDocs.java b/src/main/java/com/tiki/server/auth/controller/docs/AuthControllerDocs.java index 3c0e9238..85263f64 100644 --- a/src/main/java/com/tiki/server/auth/controller/docs/AuthControllerDocs.java +++ b/src/main/java/com/tiki/server/auth/controller/docs/AuthControllerDocs.java @@ -40,9 +40,7 @@ public interface AuthControllerDocs { description = "서버 내부 오류", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))} ) - ResponseEntity> login( - HttpServletResponse httpServletResponse, - @RequestBody SignInRequest request); + ResponseEntity> signIn(@RequestBody SignInRequest request); @Operation( summary = "엑세스 토큰 재발급", diff --git a/src/main/java/com/tiki/server/auth/exception/handler/CustomAuthenticationEntryPointHandler.java b/src/main/java/com/tiki/server/auth/exception/handler/CustomAuthenticationEntryPointHandler.java index 85c78090..f02a1be0 100644 --- a/src/main/java/com/tiki/server/auth/exception/handler/CustomAuthenticationEntryPointHandler.java +++ b/src/main/java/com/tiki/server/auth/exception/handler/CustomAuthenticationEntryPointHandler.java @@ -1,8 +1,9 @@ package com.tiki.server.auth.exception.handler; +import static com.tiki.server.auth.message.ErrorCode.*; + import com.fasterxml.jackson.databind.ObjectMapper; -import com.tiki.server.auth.message.ErrorCode; -import com.tiki.server.common.dto.ErrorResponse; +import com.tiki.server.common.dto.ErrorCodeResponse; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; @@ -30,14 +31,15 @@ public void commence( AuthenticationException authException ) throws IOException { log.info("[AuthenticationEntryPoint] " + authException.getMessage()); - setResponse(response, ErrorCode.UNAUTHENTICATED.getMessage()); + setResponse(response); } - private void setResponse(HttpServletResponse response, String errorMessage) throws IOException { + private void setResponse(HttpServletResponse response) throws IOException { response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setCharacterEncoding("UTF-8"); response.setStatus(HttpStatus.UNAUTHORIZED.value()); val writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(ErrorResponse.of(errorMessage))); + writer.write(objectMapper.writeValueAsString( + ErrorCodeResponse.of(UNAUTHENTICATED.getCode(), UNAUTHENTICATED.getMessage()))); } } diff --git a/src/main/java/com/tiki/server/auth/filter/ExceptionHandlerFilter.java b/src/main/java/com/tiki/server/auth/filter/ExceptionHandlerFilter.java index b389caab..b93346c9 100644 --- a/src/main/java/com/tiki/server/auth/filter/ExceptionHandlerFilter.java +++ b/src/main/java/com/tiki/server/auth/filter/ExceptionHandlerFilter.java @@ -1,9 +1,11 @@ package com.tiki.server.auth.filter; +import static com.tiki.server.auth.message.ErrorCode.*; + import com.fasterxml.jackson.databind.ObjectMapper; import com.tiki.server.auth.exception.AuthException; import com.tiki.server.auth.message.ErrorCode; -import com.tiki.server.common.dto.ErrorResponse; +import com.tiki.server.common.dto.ErrorCodeResponse; import jakarta.servlet.FilterChain; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -11,7 +13,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; @@ -35,30 +36,19 @@ protected void doFilterInternal( filterChain.doFilter(request, response); } catch (AuthException e) { log.info("[ExceptionHandlerFilter] - AuthException : " + e); - handleAuthException(response, e); + setResponse(response, e.getErrorCode()); } catch (Exception e) { log.info("[ExceptionHandlerFilter] - UncaughtException : " + e); - handleUncaughtException(response); + setResponse(response, UNCAUGHT_EXCEPTION); } } - private void handleAuthException(HttpServletResponse response, AuthException e) throws IOException { - val errorMessage = e.getErrorCode().getMessage(); - val httpStatus = e.getErrorCode().getHttpStatus(); - setResponse(response, httpStatus, errorMessage); - } - - private void handleUncaughtException(HttpServletResponse response) throws IOException { - val uncaughtException = ErrorCode.UNCAUGHT_EXCEPTION; - setResponse(response, uncaughtException.getHttpStatus(), uncaughtException.getMessage()); - } - - private void setResponse(HttpServletResponse response, HttpStatus httpStatus, String errorMessage) + private void setResponse(HttpServletResponse response, ErrorCode errorCode) throws IOException { response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setCharacterEncoding("UTF-8"); - response.setStatus(httpStatus.value()); + response.setStatus(errorCode.getHttpStatus().value()); val writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(ErrorResponse.of(errorMessage))); + writer.write(objectMapper.writeValueAsString(ErrorCodeResponse.of(errorCode.getCode(), errorCode.getMessage()))); } } diff --git a/src/main/java/com/tiki/server/auth/info/AuthenticationResponse.java b/src/main/java/com/tiki/server/auth/info/AuthenticationResponse.java deleted file mode 100644 index 08fc8bf2..00000000 --- a/src/main/java/com/tiki/server/auth/info/AuthenticationResponse.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.tiki.server.auth.info; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.tiki.server.auth.message.ErrorCode; -import com.tiki.server.common.dto.ErrorResponse; -import jakarta.servlet.http.HttpServletResponse; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; - -import java.io.IOException; - -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - -@Component -@RequiredArgsConstructor -public class AuthenticationResponse { - - private final ObjectMapper objectMapper; - - public void makeFailureResponse(HttpServletResponse response, ErrorCode errorCode) throws IOException { - response.setCharacterEncoding("UTF-8"); - response.setContentType(APPLICATION_JSON_VALUE); - response.setStatus(errorCode.getHttpStatus().value()); - response.getWriter().println(objectMapper.writeValueAsString(ErrorResponse.of(errorCode.getMessage()))); - } -} diff --git a/src/main/java/com/tiki/server/auth/message/ErrorCode.java b/src/main/java/com/tiki/server/auth/message/ErrorCode.java index 57f7a31c..02a99f11 100644 --- a/src/main/java/com/tiki/server/auth/message/ErrorCode.java +++ b/src/main/java/com/tiki/server/auth/message/ErrorCode.java @@ -11,21 +11,23 @@ @AllArgsConstructor public enum ErrorCode { - /* 400 INTERNAL_SERVER_ERROR : 잘못된 요청입니다. */ - UNCAUGHT_EXCEPTION(BAD_REQUEST, "예상치 못한 오류입니다."), + /* 400 BAD REQUEST : 잘못된 요청 */ + UNCAUGHT_EXCEPTION(BAD_REQUEST, 40001, "예상치 못한 오류가 발생했습니다."), /* 401 UNAUTHORIZED : 인증 없음 */ - UNAUTHENTICATED(UNAUTHORIZED, "인증과정중 오류가 발생했습니다"), - UNMATCHED_TOKEN(UNAUTHORIZED, "토큰이 일치하지 않습니다."), - INVALID_JWT_TOKEN(UNAUTHORIZED, "잘못된 토큰 형식입니다."), - EXPIRED_JWT_TOKEN(UNAUTHORIZED, "만료된 토큰입니다."), - EMPTY_JWT(UNAUTHORIZED, "빈 토큰입니다."), + UNAUTHENTICATED(UNAUTHORIZED, 40101, "인증 과정 중 오류가 발생했습니다"), + UNMATCHED_TOKEN(UNAUTHORIZED, 40102, "토큰이 일치하지 않습니다."), + INVALID_JWT_TOKEN(UNAUTHORIZED, 40103, "잘못된 토큰 형식입니다."), + EXPIRED_JWT_TOKEN(UNAUTHORIZED, 40104, "만료된 토큰입니다."), + EMPTY_JWT(UNAUTHORIZED, 40105, "빈 토큰입니다."), - /* 403 FORBIDDEN : 인가 없음 */ - UNAUTHORIZED_USER(FORBIDDEN, "권한이 없는 사용자입니다."), + /* 403 FORBIDDEN : 권한 없음 */ + UNAUTHORIZED_USER(FORBIDDEN, 40301, "권한이 없는 사용자입니다."), - UNCAUGHT_SERVER_EXCEPTION(INTERNAL_SERVER_ERROR,"처리되지 않은 에러ㅜ(서버한테 물어보삼)"); + /* 500 INTERNAL_SERVER_ERROR : 서버 내부 오류 발생 */ + UNCAUGHT_SERVER_EXCEPTION(INTERNAL_SERVER_ERROR, 500, "서버 내부에서 오류가 발생했습니다."); private final HttpStatus httpStatus; + private final int code; private final String message; } diff --git a/src/main/java/com/tiki/server/common/dto/ErrorCodeResponse.java b/src/main/java/com/tiki/server/common/dto/ErrorCodeResponse.java new file mode 100644 index 00000000..f55bea47 --- /dev/null +++ b/src/main/java/com/tiki/server/common/dto/ErrorCodeResponse.java @@ -0,0 +1,22 @@ +package com.tiki.server.common.dto; + +import static lombok.AccessLevel.PRIVATE; + +import lombok.Builder; +import lombok.NonNull; + +@Builder(access = PRIVATE) +public record ErrorCodeResponse( + boolean success, + int code, + @NonNull String message +) implements BaseResponse { + + public static ErrorCodeResponse of(int code, String message) { + return ErrorCodeResponse.builder() + .success(false) + .code(code) + .message(message) + .build(); + } +} diff --git a/src/main/java/com/tiki/server/common/handler/ErrorHandler.java b/src/main/java/com/tiki/server/common/handler/ErrorHandler.java index 6034d0ec..cb1eafd3 100644 --- a/src/main/java/com/tiki/server/common/handler/ErrorHandler.java +++ b/src/main/java/com/tiki/server/common/handler/ErrorHandler.java @@ -1,6 +1,7 @@ package com.tiki.server.common.handler; import com.tiki.server.auth.exception.AuthException; +import com.tiki.server.common.dto.ErrorCodeResponse; import com.tiki.server.mail.exception.MailException; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -77,7 +78,8 @@ public ResponseEntity MailException(MailException exception) { public ResponseEntity AuthException(AuthException exception) { log.error(exception.getMessage()); val errorCode = exception.getErrorCode(); - return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage())); + return ResponseEntity.status(errorCode.getHttpStatus()).body( + ErrorCodeResponse.of(errorCode.getCode(), errorCode.getMessage())); } @ExceptionHandler(Exception.class) diff --git a/src/main/java/com/tiki/server/document/message/ErrorCode.java b/src/main/java/com/tiki/server/document/message/ErrorCode.java index 93f92769..c78424b2 100644 --- a/src/main/java/com/tiki/server/document/message/ErrorCode.java +++ b/src/main/java/com/tiki/server/document/message/ErrorCode.java @@ -16,11 +16,11 @@ public enum ErrorCode { /* 400 BAD_REQUEST : 잘못된 요청 */ INVALID_TYPE(BAD_REQUEST, "유효한 타입이 아닙니다."), - /* 404 NOT_FOUND : 자원을 찾을 수 없음 */ - INVALID_DOCUMENT(NOT_FOUND, "유효하지 않은 문서입니다."), - /* 403 FORBIDDEN : 권한 없음 */ - INVALID_AUTHORIZATION(FORBIDDEN, "문서에 대한 권한이 없습니다."); + INVALID_AUTHORIZATION(FORBIDDEN, "문서에 대한 권한이 없습니다."), + + /* 404 NOT_FOUND : 자원을 찾을 수 없음 */ + INVALID_DOCUMENT(NOT_FOUND, "유효하지 않은 문서입니다."); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/com/tiki/server/external/message/ErrorCode.java b/src/main/java/com/tiki/server/external/message/ErrorCode.java index 778f8e12..6dd0199f 100644 --- a/src/main/java/com/tiki/server/external/message/ErrorCode.java +++ b/src/main/java/com/tiki/server/external/message/ErrorCode.java @@ -1,6 +1,5 @@ package com.tiki.server.external.message; -import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import org.springframework.http.HttpStatus; @@ -12,9 +11,9 @@ @AllArgsConstructor public enum ErrorCode { - /* 500 INTERNAL_SERVER_ERROR : 서버 에러 */ - PRESIGNED_URL_GET_ERROR(INTERNAL_SERVER_ERROR, "S3 PRESIGNED URL 불러오기 실패"), - FILE_DELETE_ERROR(INTERNAL_SERVER_ERROR, "S3 버킷의 파일 삭제 실패"); + /* 500 INTERNAL_SERVER_ERROR : 서버 내부 오류 발생 */ + PRESIGNED_URL_GET_ERROR(INTERNAL_SERVER_ERROR, "S3 PRESIGNED URL 불러오기에 실패했습니다."), + FILE_DELETE_ERROR(INTERNAL_SERVER_ERROR, "S3 버킷의 파일 삭제에 실패했습니다."); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/com/tiki/server/mail/message/ErrorCode.java b/src/main/java/com/tiki/server/mail/message/ErrorCode.java index 0fbeb131..6eb5096e 100644 --- a/src/main/java/com/tiki/server/mail/message/ErrorCode.java +++ b/src/main/java/com/tiki/server/mail/message/ErrorCode.java @@ -10,13 +10,13 @@ @AllArgsConstructor public enum ErrorCode { - /* 403 BAD REQUEST: 인증 거부 */ + /* 403 FORBIDDEN : 권한 없음 */ INVALID_MATCHED(FORBIDDEN, "인증 정보가 일치하지 않습니다."), - /* 404 NOT FOUND: 요청 리소스를 찾을 수 없음 */ + /* 404 NOT_FOUND : 자원을 찾을 수 없음 */ INVALID_REQUEST(NOT_FOUND, "인증 정보가 존재하지 않습니다."), - /* 500 INTERNAL_SERVER_ERROR 서버 내부 오류 발생 */ + /* 500 INTERNAL_SERVER_ERROR : 서버 내부 오류 발생 */ MESSAGE_HELPER_ERROR(INTERNAL_SERVER_ERROR,"메세지를 설정할 수 없습니다."); private final HttpStatus httpStatus; diff --git a/src/main/java/com/tiki/server/member/message/ErrorCode.java b/src/main/java/com/tiki/server/member/message/ErrorCode.java index ea84c46b..3ed7d8d7 100644 --- a/src/main/java/com/tiki/server/member/message/ErrorCode.java +++ b/src/main/java/com/tiki/server/member/message/ErrorCode.java @@ -11,7 +11,7 @@ @AllArgsConstructor public enum ErrorCode { - /* 400 BAD REQUEST: 잘못된 요청 */ + /* 400 BAD REQUEST : 잘못된 요청 */ UNMATCHED_PASSWORD(BAD_REQUEST, "비밀번호가 일치하지 않습니다."), INVALID_EMAIL(BAD_REQUEST, "잘못된 이메일 형식입니다."),