|
| 1 | +package kr.io.team.loop.common.config |
| 2 | + |
| 3 | +import com.netflix.graphql.types.errors.ErrorType |
| 4 | +import graphql.GraphQLError |
| 5 | +import kr.io.team.loop.common.domain.exception.AccessDeniedException |
| 6 | +import kr.io.team.loop.common.domain.exception.AuthenticationException |
| 7 | +import kr.io.team.loop.common.domain.exception.BusinessRuleException |
| 8 | +import kr.io.team.loop.common.domain.exception.DuplicateEntityException |
| 9 | +import kr.io.team.loop.common.domain.exception.EntityNotFoundException |
| 10 | +import kr.io.team.loop.common.domain.exception.InvalidInputException |
| 11 | +import org.slf4j.LoggerFactory |
| 12 | +import org.springframework.graphql.data.method.annotation.GraphQlExceptionHandler |
| 13 | +import org.springframework.web.bind.annotation.ControllerAdvice |
| 14 | + |
| 15 | +@ControllerAdvice |
| 16 | +class GraphQlExceptionHandler { |
| 17 | + private val log = LoggerFactory.getLogger(javaClass) |
| 18 | + |
| 19 | + @GraphQlExceptionHandler |
| 20 | + fun handleInvalidInput(ex: InvalidInputException): GraphQLError { |
| 21 | + log.warn("[BAD_REQUEST] {}", ex.message, ex) |
| 22 | + return GraphQLError |
| 23 | + .newError() |
| 24 | + .errorType(ErrorType.BAD_REQUEST) |
| 25 | + .message(ex.message) |
| 26 | + .build() |
| 27 | + } |
| 28 | + |
| 29 | + @GraphQlExceptionHandler |
| 30 | + fun handleEntityNotFound(ex: EntityNotFoundException): GraphQLError { |
| 31 | + log.warn("[NOT_FOUND] {}", ex.message, ex) |
| 32 | + return GraphQLError |
| 33 | + .newError() |
| 34 | + .errorType(ErrorType.NOT_FOUND) |
| 35 | + .message(ex.message) |
| 36 | + .build() |
| 37 | + } |
| 38 | + |
| 39 | + @GraphQlExceptionHandler |
| 40 | + fun handleDuplicateEntity(ex: DuplicateEntityException): GraphQLError { |
| 41 | + log.warn("[FAILED_PRECONDITION] {}", ex.message, ex) |
| 42 | + return GraphQLError |
| 43 | + .newError() |
| 44 | + .errorType(ErrorType.FAILED_PRECONDITION) |
| 45 | + .message(ex.message) |
| 46 | + .build() |
| 47 | + } |
| 48 | + |
| 49 | + @GraphQlExceptionHandler |
| 50 | + fun handleBusinessRule(ex: BusinessRuleException): GraphQLError { |
| 51 | + log.warn("[FAILED_PRECONDITION] {}", ex.message, ex) |
| 52 | + return GraphQLError |
| 53 | + .newError() |
| 54 | + .errorType(ErrorType.FAILED_PRECONDITION) |
| 55 | + .message(ex.message) |
| 56 | + .build() |
| 57 | + } |
| 58 | + |
| 59 | + @GraphQlExceptionHandler |
| 60 | + fun handleAuthentication(ex: AuthenticationException): GraphQLError { |
| 61 | + log.warn("[UNAUTHENTICATED] {}", ex.message, ex) |
| 62 | + return GraphQLError |
| 63 | + .newError() |
| 64 | + .errorType(ErrorType.UNAUTHENTICATED) |
| 65 | + .message(ex.message) |
| 66 | + .build() |
| 67 | + } |
| 68 | + |
| 69 | + @GraphQlExceptionHandler |
| 70 | + fun handleAccessDenied(ex: AccessDeniedException): GraphQLError { |
| 71 | + log.warn("[PERMISSION_DENIED] {}", ex.message, ex) |
| 72 | + return GraphQLError |
| 73 | + .newError() |
| 74 | + .errorType(ErrorType.PERMISSION_DENIED) |
| 75 | + .message(ex.message) |
| 76 | + .build() |
| 77 | + } |
| 78 | +} |
0 commit comments