Commit 76247feb authored by Ghitha Dinan's avatar Ghitha Dinan

add custom exception handler

parent 366ceea3
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>id.co.sangkuriang</groupId> <groupId>id.co.sangkuriang.inovasi.daerah</groupId>
<artifactId>inovasi-daerah-master</artifactId> <artifactId>inovasi-daerah-master</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>inovasi-daerah-master</name> <name>inovasi-daerah-master</name>
......
package id.co.sangkuriang.inovasi.daerah.master.controller
import id.co.sangkuriang.inovasi.daerah.master.exception.BaseException
import id.co.sangkuriang.inovasi.daerah.master.model.response.ReturnData
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.validation.BindException
import org.springframework.validation.BindingResult
import org.springframework.validation.ObjectError
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
import java.util.function.Consumer
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
class CustomExceptionHandler : ResponseEntityExceptionHandler() {
override fun handleMethodArgumentNotValid(
ex: MethodArgumentNotValidException,
headers: HttpHeaders,
status: HttpStatus,
request: WebRequest
): ResponseEntity<Any> {
val errors = mutableListOf<Map<String, String>>()
ex.bindingResult.fieldErrors.forEach { fieldError ->
errors.add(mapOf(fieldError.field to fieldError.defaultMessage.toString()))
}
generateObjectErrors(ex.bindingResult)?.let { errors.add(it) }
return ResponseEntity<Any>(
ReturnData(
data = errors,
message = status.name
),
status
)
}
private fun generateObjectErrors(result: BindingResult): Map<String, String>? {
val fieldErrorMap: MutableMap<String, String> = HashMap()
result.globalErrors.forEach(Consumer { objectError: ObjectError ->
fieldErrorMap["confirmPassword"] = objectError.defaultMessage ?: ""
})
if (fieldErrorMap.isNotEmpty()) {
return fieldErrorMap
}
return null
}
override fun handleBindException(
ex: BindException,
headers: HttpHeaders,
status: HttpStatus,
request: WebRequest
): ResponseEntity<Any> {
val errors = mutableListOf<Map<String, String>>()
ex.bindingResult.fieldErrors.forEach { fieldError ->
errors.add(mapOf(fieldError.field to fieldError.defaultMessage.toString()))
}
generateObjectErrors(ex.bindingResult)?.let { errors.add(it) }
return ResponseEntity<Any>(
ReturnData(
data = errors,
message = status.name
),
status
)
}
@ExceptionHandler(RuntimeException::class)
fun handleRuntimeException(ex: Exception, request: WebRequest?): ResponseEntity<Any?>? {
val response = ReturnData()
response.success = false
response.message = "Interval Server Error"
response.message = ex.message
var status = HttpStatus.INTERNAL_SERVER_ERROR
if (ex is BaseException) {
response.message = ex.message
ex.data?.let {
response.data = it
}
status = ex.status
} else {
return ResponseEntity(response, status)
}
return ResponseEntity(response, status)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment