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

[REFACTOR] Error Handling #324

Merged
merged 4 commits into from
Feb 13, 2022
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
64 changes: 64 additions & 0 deletions spring/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,70 @@ Bancow;
| Not Found Event
|===

=== Farm Error Code
|===
| Error Code | Usage

| F001
| Not Found Farm
|===

=== Buyer Error Code
|===
| Error Code | Usage

| B001
| Not Found Buyer
|===


=== NEWS Error Code
|===
| Error Code | Usage

| N001
| Not Found NEWS
|===


=== TOKEN Error Code
|===
| Error Code | Usage

| TK001
| Not Found Token

| TK002
| Not Authority Token

| TK003
| Not Super Token
|===

=== MANGER Error Code
|===
| Error Code | Usage

| M001
| Not Found MANAGER

| M002
| NOT VALID PASSWORD

| M003
| NOT VALID USER
|===

=== MANGER REGISTER Error Code
|===
| Error Code | Usage

| R001
| Duplicate Email

| R002
| Password 1, 2 Not Equal
|===

[[Manager]]
== Manager
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ public enum ErrorCode {

// NEWS
NOT_FOUND_NEWS("N001", "Not Found News"),

// TOKEN
NOT_FOUND_TOKEN("TK001", "Not Found Token"),
NOT_AUTHORITY("TK002", "Not Authority Token"),
NOT_AUTHORITY_SUPER("TK003", "Not Super Token"),

// MANGER
NOT_FOUND_MANAGER("M001", "NOT FOUND MANAGER"),
NOT_VALID_PASSWORD("M002", "NOT VALID PASSWORD"),
NOT_VALID_USER("M003", "NOT VALID USER"),

// REGISTER
DUPLICATE_EMAIL("R001", "Duplicate Email"),
NOT_EQUAL_PASSWORD("R002", "Password 1, 2 Not Equal"),

;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.bancow.bancowback.domain.sub.news.entity.News;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
Expand Down Expand Up @@ -122,5 +120,62 @@ protected ResponseEntity<ErrorResponse> handlerNewsException(NewsException e) {
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_FOUND_NEWS);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(TokenAuthorityException.class)
protected ResponseEntity<ErrorResponse> handlerTokenAuthorityException(TokenAuthorityException e) {
log.error("TokenAuthorityException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_AUTHORITY);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(TokenAuthoritySuperException.class)
protected ResponseEntity<ErrorResponse> handlerTokenAuthoritySuperException(TokenAuthoritySuperException e) {
log.error("TokenAuthoritySuperException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_AUTHORITY_SUPER);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(TokenNotFoundException.class)
protected ResponseEntity<ErrorResponse> handlerTokenException(TokenNotFoundException e) {
log.error("TokenNotFoundException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_FOUND_TOKEN);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(ManagerNotFoundException.class)
protected ResponseEntity<ErrorResponse> handlerManagerNotFoundException(ManagerNotFoundException e) {
log.error("ManagerNotFoundException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_FOUND_MANAGER);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(ManagerNotValidPasswordException.class)
protected ResponseEntity<ErrorResponse> handlerManagerNotValidPasswordException(ManagerNotValidPasswordException e) {
log.error("ManagerNotValidPasswordException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_VALID_PASSWORD);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(ManagerNotValidException.class)
protected ResponseEntity<ErrorResponse> handlerManagerNotValidException(ManagerNotValidException e) {
log.error("ManagerNotValidException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_VALID_USER);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(RegisterDuplicateEmailException.class)
protected ResponseEntity<ErrorResponse> handlerRegisterDuplicateEmailException(RegisterDuplicateEmailException e) {
log.error("RegisterDuplicateEmailException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.DUPLICATE_EMAIL);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(RegisterNotEqualPasswordException.class)
protected ResponseEntity<ErrorResponse> handlerRegisterNotEqualPasswordException(
RegisterNotEqualPasswordException e) {
log.error("RegisterNotEqualPasswordException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.NOT_EQUAL_PASSWORD);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bancow.bancowback.domain.common.exception;

import lombok.Getter;

@Getter
public class ManagerNotFoundException extends RuntimeException {
private ErrorCode managerErrorCode;
private String detailMessage;

public ManagerNotFoundException(ErrorCode managerErrorCode, String detailMessage) {
super(detailMessage);
this.managerErrorCode = managerErrorCode;
this.detailMessage = detailMessage;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bancow.bancowback.domain.common.exception;

import lombok.Getter;

@Getter
public class ManagerNotValidException extends RuntimeException {
private ErrorCode managerErrorCode;
private String detailMessage;

public ManagerNotValidException(ErrorCode managerErrorCode, String detailMessage) {
super(detailMessage);
this.managerErrorCode = managerErrorCode;
this.detailMessage = detailMessage;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bancow.bancowback.domain.common.exception;

import lombok.Getter;

@Getter
public class ManagerNotValidPasswordException extends RuntimeException {
private ErrorCode managerErrorCode;
private String detailMessage;

public ManagerNotValidPasswordException(ErrorCode managerErrorCode, String detailMessage) {
super(detailMessage);
this.managerErrorCode = managerErrorCode;
this.detailMessage = detailMessage;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bancow.bancowback.domain.common.exception;

import lombok.Getter;

@Getter
public class RegisterDuplicateEmailException extends RuntimeException {
private ErrorCode registerErrorCode;
private String detailMessage;

public RegisterDuplicateEmailException(ErrorCode registerErrorCode, String detailMessage) {
super(detailMessage);
this.registerErrorCode = registerErrorCode;
this.detailMessage = detailMessage;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bancow.bancowback.domain.common.exception;

import lombok.Getter;

@Getter
public class RegisterNotEqualPasswordException extends RuntimeException {
private ErrorCode registerErrorCode;
private String detailMessage;

public RegisterNotEqualPasswordException(ErrorCode registerErrorCode, String detailMessage) {
super(detailMessage);
this.registerErrorCode = registerErrorCode;
this.detailMessage = detailMessage;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bancow.bancowback.domain.common.exception;

public class TokenAuthorityException extends TokenNotFoundException {

public TokenAuthorityException(ErrorCode tokenErrorCode, String detailMessage) {
super(tokenErrorCode, detailMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bancow.bancowback.domain.common.exception;

public class TokenAuthoritySuperException extends TokenNotFoundException {

public TokenAuthoritySuperException(ErrorCode tokenErrorCode, String detailMessage) {
super(tokenErrorCode, detailMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bancow.bancowback.domain.common.exception;

import lombok.Getter;

@Getter
public class TokenNotFoundException extends RuntimeException {
private ErrorCode tokenErrorCode;
private String detailMessage;

public TokenNotFoundException(ErrorCode tokenErrorCode, String detailMessage) {
super(detailMessage);
this.tokenErrorCode = tokenErrorCode;
this.detailMessage = detailMessage;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bancow.bancowback.domain.common.util.token.service;

import static com.bancow.bancowback.domain.common.exception.ErrorCode.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Optional;
Expand All @@ -8,7 +10,9 @@

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.bancow.bancowback.domain.common.exception.BizException;
import com.bancow.bancowback.domain.common.exception.TokenAuthorityException;
import com.bancow.bancowback.domain.common.exception.TokenAuthoritySuperException;
import com.bancow.bancowback.domain.common.exception.TokenNotFoundException;
import com.bancow.bancowback.domain.common.util.token.entity.Token;
import com.bancow.bancowback.domain.common.util.token.repository.TokenRepository;
import com.bancow.bancowback.domain.manager.entity.Manager;
Expand All @@ -28,21 +32,21 @@ public Manager getManager(String token) {

public Token getToken(String token){
return tokenRepository.findByToken(token)
.orElseThrow(() -> new BizException("해당 토큰을 찾을 수 없습니다."));
.orElseThrow(() -> new TokenNotFoundException(NOT_FOUND_TOKEN, "해당 토큰을 찾을 수 없습니다."));
}

public void validTokenAuthority(String token) {
Token findToken = getToken(token);
if (!(findToken.getManager().getManagerStatus().equals(ManagerStatus.ADMIN) ||
findToken.getManager().getManagerStatus().equals(ManagerStatus.SUPER))) {
throw new BizException("유저 권한이 없습니다.");
throw new TokenAuthorityException(NOT_AUTHORITY, "유저 권한이 없습니다.");
}
}

public void validTokenSuper(String token) {
Token findToken = getToken(token);
if (!(findToken.getManager().getManagerStatus().equals(ManagerStatus.SUPER))) {
throw new BizException("SUPER 계정이 아닙니다.");
throw new TokenAuthoritySuperException(NOT_AUTHORITY_SUPER, "SUPER 계정이 아닙니다.");
}
}

Expand Down
Loading