Skip to content

Instantly share code, notes, and snippets.

@kaiyan910
Created July 3, 2023 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaiyan910/6313b01a9fbcde52ce19cef7f9e61aec to your computer and use it in GitHub Desktop.
Save kaiyan910/6313b01a9fbcde52ce19cef7f9e61aec to your computer and use it in GitHub Desktop.
package com.qbssystem.timburr.controller
import com.qbssystem.timburr.ErrorScope
import com.qbssystem.timburr.exception.SystemException
import com.qbssystem.timburr.extractStackTraceLines
import org.springframework.beans.ConversionNotSupportedException
import org.springframework.beans.TypeMismatchException
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.http.converter.HttpMessageNotWritableException
import org.springframework.security.access.AccessDeniedException
import org.springframework.validation.BindException
import org.springframework.validation.FieldError
import org.springframework.web.HttpMediaTypeNotAcceptableException
import org.springframework.web.HttpMediaTypeNotSupportedException
import org.springframework.web.HttpRequestMethodNotSupportedException
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.MissingPathVariableException
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.bind.ServletRequestBindingException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.context.request.WebRequest
import org.springframework.web.context.request.async.AsyncRequestTimeoutException
import org.springframework.web.multipart.support.MissingServletRequestPartException
import org.springframework.web.servlet.NoHandlerFoundException
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
import java.io.IOException
import java.lang.Exception
import javax.persistence.EntityNotFoundException
import javax.validation.ConstraintViolationException
@ControllerAdvice
class ExceptionAdvisor : ResponseEntityExceptionHandler() {
@ResponseBody
@ExceptionHandler(SystemException::class)
fun handleCustomException(ex: SystemException): ResponseEntity<Any> {
return ex.asHttpResponse()
}
@ResponseBody
@ExceptionHandler(AccessDeniedException::class)
fun handleAccessDeniedException(ex: AccessDeniedException): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "ACCESS DENIED",
details = extractStackTraceLines(ex),
status = HttpStatus.UNAUTHORIZED
).asHttpResponse()
}
@ResponseBody
@ExceptionHandler(KotlinNullPointerException::class)
fun handleKotlinNullPointerException(ex: KotlinNullPointerException): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "NULL POINTER EXCEPTION",
details = extractStackTraceLines(ex),
status = HttpStatus.INTERNAL_SERVER_ERROR
).asHttpResponse()
}
@ResponseBody
@ExceptionHandler(EntityNotFoundException::class)
fun handleEntityNotFoundException(ex: EntityNotFoundException): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "ENTITY NOT FOUND",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
@ResponseBody
@ExceptionHandler(ConstraintViolationException::class)
fun handleConstraintViolationException(ex: ConstraintViolationException): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "INVALID ARGUMENT(S) PROVIDED",
details = ex.message
?.split(", ")
?: listOf(),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
@ResponseBody
@ExceptionHandler(IOException::class)
fun handleIOException(ex: IOException): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "IO EXCEPTION",
details = extractStackTraceLines(ex),
status = HttpStatus.INTERNAL_SERVER_ERROR
).asHttpResponse()
}
override fun handleHttpRequestMethodNotSupported(ex: HttpRequestMethodNotSupportedException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "METHOD NOT SUPPORTED",
details = extractStackTraceLines(ex),
status = HttpStatus.METHOD_NOT_ALLOWED
).asHttpResponse()
}
override fun handleHttpMediaTypeNotSupported(ex: HttpMediaTypeNotSupportedException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "MEDIA TYPE NOT SUPPORTED",
details = extractStackTraceLines(ex),
status = HttpStatus.UNSUPPORTED_MEDIA_TYPE
).asHttpResponse()
}
override fun handleHttpMediaTypeNotAcceptable(ex: HttpMediaTypeNotAcceptableException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "MEDIA TYPE NOT ACCEPTABLE",
details = extractStackTraceLines(ex),
status = HttpStatus.NOT_ACCEPTABLE
).asHttpResponse()
}
override fun handleMissingPathVariable(ex: MissingPathVariableException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "MISSING PATH VARIABLE",
details = extractStackTraceLines(ex),
status = HttpStatus.INTERNAL_SERVER_ERROR
).asHttpResponse()
}
override fun handleMissingServletRequestParameter(ex: MissingServletRequestParameterException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "MISSING SERVLET REQUEST PARAMETER",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleServletRequestBindingException(ex: ServletRequestBindingException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "SERVLET REQUEST BINDING EXCEPTION",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleConversionNotSupported(ex: ConversionNotSupportedException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "CONVERSION NOT SUPPORTED",
details = extractStackTraceLines(ex),
status = HttpStatus.INTERNAL_SERVER_ERROR
).asHttpResponse()
}
override fun handleTypeMismatch(ex: TypeMismatchException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "TYPE MISMATCH",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleHttpMessageNotReadable(ex: HttpMessageNotReadableException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "HTTP MESSAGE NOT READABLE",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleHttpMessageNotWritable(ex: HttpMessageNotWritableException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "HTTP MESSAGE NOT WRITEABLE",
details = extractStackTraceLines(ex),
status = HttpStatus.INTERNAL_SERVER_ERROR
).asHttpResponse()
}
override fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
val fields = ex
.bindingResult
.allErrors
.map { (it as FieldError).field }
.asSequence()
val errors = ex
.bindingResult
.fieldErrors
.map { it.defaultMessage ?: "" }
.asSequence()
return SystemException(
scope = ErrorScope.SYSTEM,
message = "INVALID ARGUMENT(S) PROVIDED",
details = fields.zip(errors) { field, error -> "$field: $error" }.toList(),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleMissingServletRequestPart(ex: MissingServletRequestPartException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "MISSING SERVLET REQUEST PART",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleBindException(ex: BindException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "BIND EXCEPTION",
details = extractStackTraceLines(ex),
status = HttpStatus.BAD_REQUEST
).asHttpResponse()
}
override fun handleNoHandlerFoundException(ex: NoHandlerFoundException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "NO HANDLER FOUND EXCEPTION",
details = extractStackTraceLines(ex),
status = HttpStatus.NOT_FOUND
).asHttpResponse()
}
override fun handleAsyncRequestTimeoutException(ex: AsyncRequestTimeoutException, headers: HttpHeaders, status: HttpStatus, webRequest: WebRequest): ResponseEntity<Any>? {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "ASYNC REQUEST TIMEOUT EXCEPTION",
details = extractStackTraceLines(ex),
status = HttpStatus.SERVICE_UNAVAILABLE
).asHttpResponse()
}
override fun handleExceptionInternal(ex: Exception, body: Any?, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
return SystemException(
scope = ErrorScope.SYSTEM,
message = "INTERNAL SERVER ERROR",
details = extractStackTraceLines(ex),
status = HttpStatus.INTERNAL_SERVER_ERROR
).asHttpResponse()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment