Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] hotfix/auth member #346

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
@Getter
public enum AtlasErrorCode {

ILLEGAL_COORDINATE_RANGE("00000", "한국 내의 좌표만 입력해주세요."),
ILLEGAL_TOPIC_ID("00001", "유효하지 않은 지도입니다."),
ILLEGAL_MEMBER_ID("00002", "유효하지 않은 회원입니다."),

ILLEGAL_TOPIC_ID("00000", "유효하지 않은 지도입니다."),
ILLEGAL_MEMBER_ID("00001", "유효하지 않은 회원입니다."),
FORBIDDEN_TOPIC_ADD("00300", "모아보기 추가 권한이 없습니다."),
FORBIDDEN_TOPIC_READ("00301", "지도 조회 권한이 없습니다."),
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

public class AtlasException {

public static class AtlasForbiddenException extends ForbiddenException {
public AtlasForbiddenException(AtlasErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public static class AtlasBadRequestException extends BadRequestException {
public AtlasBadRequestException(AtlasErrorCode errorCode) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

public static class AtlasBadRequestException extends BadRequestException {
public AtlasBadRequestException(AtlasErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public static class AtlasForbiddenException extends ForbiddenException {
public AtlasForbiddenException(AtlasErrorCode errorCode) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private boolean isGroup(Long topicId) {
}

private boolean hasPermission(Long topicId) {
return topicsWithPermission.contains(topicId);
return createdTopic.contains(topicId) || topicsWithPermission.contains(topicId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private Topic getTopicById(Long topicId) {

private void validateBookmarkDuplication(AuthMember authMember, Long topicId) {
if (isExistBookmark(authMember, topicId)) {
throw new BookmarkConflictException(CONFLICT_TOPIC_ALREADY_ADD);
throw new BookmarkConflictException(CONFLICT_TOPIC_ALREADY_ADD, topicId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

public class BookmarkException {

public static class BookmarkForbiddenException extends ForbiddenException {
public BookmarkForbiddenException(BookmarkErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public static class BookmarkBadRequestException extends BadRequestException {
public BookmarkBadRequestException(BookmarkErrorCode errorCode) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

public static class BookmarkBadRequestException extends BadRequestException {
public BookmarkBadRequestException(BookmarkErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public static class BookmarkForbiddenException extends ForbiddenException {
public BookmarkForbiddenException(BookmarkErrorCode errorCode) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

public static class BookmarkConflictException extends ConflictException {

public BookmarkConflictException(BookmarkErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public BookmarkConflictException(BookmarkErrorCode errorCode, Long id) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), id));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mapbefine.mapbefine.common.exception.ErrorCode;
import com.mapbefine.mapbefine.common.exception.GlobalException;
import com.mapbefine.mapbefine.common.exception.dto.ErrorResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -14,19 +17,29 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

private final ObjectMapper objectMapper;

public GlobalExceptionHandler(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@ExceptionHandler(GlobalException.class)
public ResponseEntity<ErrorCode> handle(GlobalException exception, HttpServletRequest request) {
public ResponseEntity<ErrorResponse> handle(
GlobalException exception,
HttpServletRequest request
) throws JsonProcessingException {
String exceptionSource = extractExceptionSource(exception);
ErrorCode errorCode = exception.getErrorCode();
ErrorCode<?> errorCode = exception.getErrorCode();

log.warn(
"source = {} \n {} = {} \n code = {} \n message = {}",
"source = {} \n {} = {} \n code = {} \n message = {} \n info = {}",
exceptionSource,
request.getMethod(), request.getRequestURI(),
errorCode.code(), errorCode.message()
errorCode.getCode(), errorCode.getMessage(),
objectMapper.writeValueAsString(errorCode.getInfo())
);

return ResponseEntity.status(exception.getStatus()).body(errorCode);
return ResponseEntity.status(exception.getStatus()).body(ErrorResponse.from(errorCode));
}

private String extractExceptionSource(Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

public class BadRequestException extends GlobalException {

public BadRequestException(ErrorCode errorCode) {
public BadRequestException(ErrorCode<?> errorCode) {
super(errorCode, HttpStatus.BAD_REQUEST);
}

public static class ImageBadRequestException extends BadRequestException {
public ImageBadRequestException() {
super(new ErrorCode("03000", "잘못된 형식의 URL입니다."));
super(new ErrorCode<>("03000", "잘못된 형식의 URL입니다."));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class ConflictException extends GlobalException {

public ConflictException(ErrorCode errorCode) {
public ConflictException(ErrorCode<?> errorCode) {
super(errorCode, HttpStatus.CONFLICT);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package com.mapbefine.mapbefine.common.exception;

public record ErrorCode(
String code,
String message
) {
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorCode<T> {

private final String code;
private final String message;
private T info;

public ErrorCode(String code, String message, T info) {
this.code = code;
this.message = message;
this.info = info;
}

public ErrorCode(String code, String message) {
this.code = code;
this.message = message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class ForbiddenException extends GlobalException {

public ForbiddenException(ErrorCode errorCode) {
public ForbiddenException(ErrorCode<?> errorCode) {
super(errorCode, HttpStatus.FORBIDDEN);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
@Getter
public class GlobalException extends RuntimeException {

private final ErrorCode errorCode;
private final ErrorCode<?> errorCode;
private final HttpStatus status;

public GlobalException(ErrorCode errorCode, HttpStatus status) {
super(errorCode.message());
public GlobalException(ErrorCode<?> errorCode, HttpStatus status) {
super(errorCode.getMessage());
this.errorCode = errorCode;
this.status = status;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class NotFoundException extends GlobalException {

public NotFoundException(ErrorCode errorCode) {
public NotFoundException(ErrorCode<?> errorCode) {
super(errorCode, HttpStatus.NOT_FOUND);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class UnauthorizedException extends GlobalException {

public UnauthorizedException(ErrorCode errorCode) {
public UnauthorizedException(ErrorCode<?> errorCode) {
super(errorCode, HttpStatus.UNAUTHORIZED);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mapbefine.mapbefine.common.exception.dto;

import com.mapbefine.mapbefine.common.exception.ErrorCode;

public record ErrorResponse(
String code,
String message
) {
public static ErrorResponse from(ErrorCode<?> errorCode) {
return new ErrorResponse(errorCode.getCode(), errorCode.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LocationException {
public static class LocationBadRequestException extends BadRequestException {

public LocationBadRequestException(LocationErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MemberDetailResponse findById(Long id) {

private Member findMemberById(Long id) {
return memberRepository.findById(id)
.orElseThrow(() -> new MemberNotFoundException(MemberErrorCode.MEMBER_NOT_FOUND));
.orElseThrow(() -> new MemberNotFoundException(MemberErrorCode.MEMBER_NOT_FOUND, id));
}

public List<MemberResponse> findAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class MemberException {

public static class MemberBadRequestException extends BadRequestException {
public MemberBadRequestException(MemberErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

public static class MemberNotFoundException extends NotFoundException {
public MemberNotFoundException(MemberErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public MemberNotFoundException(MemberErrorCode errorCode, Long id) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), id));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public String provide(OauthServerType oauthServerType) {

public AuthCodeRequestUrlProvider getProvider(OauthServerType oauthServerType) {
return Optional.ofNullable(mapping.get(oauthServerType))
.orElseThrow(() -> new OauthNotFoundException(OAUTH_SERVER_TYPE_NOT_FOUND));
.orElseThrow(() -> new OauthNotFoundException(OAUTH_SERVER_TYPE_NOT_FOUND, oauthServerType));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public OauthMember fetch(OauthServerType oauthServerType, String authCode) {

private OauthMemberClient getClient(OauthServerType oauthServerType) {
return Optional.ofNullable(mapping.get(oauthServerType))
.orElseThrow(() -> new OauthNotFoundException(OAUTH_SERVER_TYPE_NOT_FOUND));
.orElseThrow(() -> new OauthNotFoundException(OAUTH_SERVER_TYPE_NOT_FOUND, oauthServerType));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.mapbefine.mapbefine.common.exception.ErrorCode;
import com.mapbefine.mapbefine.common.exception.NotFoundException;
import com.mapbefine.mapbefine.oauth.domain.OauthServerType;

public class OathException {

public static class OauthNotFoundException extends NotFoundException {
public OauthNotFoundException(OauthErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public OauthNotFoundException(OauthErrorCode errorCode, OauthServerType oauthServerType) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), oauthServerType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ private boolean isNotSelfMember(AuthMember authMember, Member member) {
}

private Topic findTopic(PermissionRequest request) {
return topicRepository.findById(request.topicId())
.orElseThrow(() -> new PermissionBadRequestException(ILLEGAL_TOPIC_ID));
Long topicId = request.topicId();
return topicRepository.findById(topicId)
.orElseThrow(() -> new PermissionBadRequestException(ILLEGAL_TOPIC_ID, topicId));
}

private void validateMemberCanTopicUpdate(AuthMember authMember, Topic topic) {
Expand All @@ -93,7 +94,7 @@ private List<Member> findTargetMembers(PermissionRequest request) {

public void deleteMemberTopicPermission(AuthMember authMember, Long permissionId) {
Permission permission = permissionRepository.findById(permissionId)
.orElseThrow(() -> new PermissionBadRequestException(ILLEGAL_PERMISSION_ID));
.orElseThrow(() -> new PermissionBadRequestException(ILLEGAL_PERMISSION_ID, permissionId));

validateMemberCanTopicUpdate(authMember, permission.getTopic());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<PermissionResponse> findAllTopicPermissions(Long topicId) {

public PermissionDetailResponse findPermissionById(Long permissionId) {
Permission permission = permissionRepository.findById(permissionId)
.orElseThrow(() -> new PermissionNotFoundException(PERMISSION_NOT_FOUND));
.orElseThrow(() -> new PermissionNotFoundException(PERMISSION_NOT_FOUND, permissionId));

return PermissionDetailResponse.from(permission);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

public class PermissionException {

public static class PermissionForbiddenException extends ForbiddenException {
public PermissionForbiddenException(PermissionErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public static class PermissionBadRequestException extends BadRequestException {
public PermissionBadRequestException(PermissionErrorCode errorCode, Long id) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), id));
}
}

public static class PermissionBadRequestException extends BadRequestException {
public PermissionBadRequestException(PermissionErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public static class PermissionForbiddenException extends ForbiddenException {
public PermissionForbiddenException(PermissionErrorCode errorCode) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage()));
}
}

public static class PermissionNotFoundException extends NotFoundException {
public PermissionNotFoundException(PermissionErrorCode errorCode) {
super(new ErrorCode(errorCode.getCode(), errorCode.getMessage()));
public PermissionNotFoundException(PermissionErrorCode errorCode, Long id) {
super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), id));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<PinResponse> findAllReadable(AuthMember member) {
// TODO: 2023/08/08 isDeleted 제외하고 조회하기
public PinDetailResponse findDetailById(AuthMember member, Long pinId) {
Pin pin = pinRepository.findById(pinId)
.orElseThrow(() -> new PinNotFoundException(PIN_NOT_FOUND));
.orElseThrow(() -> new PinNotFoundException(PIN_NOT_FOUND, pinId));
validateReadAuth(member, pin.getTopic());

return PinDetailResponse.from(pin);
Expand Down
Loading
Loading