Skip to content

Commit

Permalink
feat: 使用globalexceptionhandler处理#1539
Browse files Browse the repository at this point in the history
  • Loading branch information
zacYL committed Oct 28, 2024
1 parent a43c822 commit 8e07e93
Showing 1 changed file with 3 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ import com.tencent.bkrepo.oci.constant.UNAUTHORIZED_DESCRIPTION
import com.tencent.bkrepo.oci.constant.UNAUTHORIZED_MESSAGE
import com.tencent.bkrepo.oci.pojo.response.OciErrorResponse
import com.tencent.bkrepo.oci.pojo.response.OciResponse
import javax.servlet.http.HttpServletResponse
import org.slf4j.LoggerFactory
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice
import javax.servlet.http.HttpServletResponse

@Order(Ordered.HIGHEST_PRECEDENCE + 1)
@RestControllerAdvice("com.tencent.bkrepo.oci")
class OciExceptionHandler(
private val ociProperties: OciProperties
) {

/**
/**
* 单独处理认证失败异常,需要添加WWW_AUTHENTICATE响应头触发浏览器登录
*/
@ExceptionHandler(AuthenticationException::class)
Expand Down Expand Up @@ -122,12 +122,6 @@ class OciExceptionHandler(
ociResponse(responseObject, exception)
}

@ExceptionHandler(ErrorCodeException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleException(exception: ErrorCodeException) {
ociResponse(exception)
}

@ExceptionHandler(PermissionException::class)
@ResponseStatus(HttpStatus.FORBIDDEN)
fun handleException(exception: PermissionException) {
Expand Down Expand Up @@ -162,7 +156,7 @@ class OciExceptionHandler(
val uri = HttpContextHolder.getRequest().requestURI
logger.warn(
"User[$userId] access oci resource[$uri] failed[${exception.javaClass.simpleName}]:" +
" ${responseObject.message}"
" ${responseObject.message}"
)
}

Expand Down

0 comments on commit 8e07e93

Please sign in to comment.