Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
I
Inovasi Daerah Master
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ghitha Dinan
Inovasi Daerah Master
Commits
76247feb
Commit
76247feb
authored
Sep 27, 2021
by
Ghitha Dinan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add custom exception handler
parent
366ceea3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
1 deletion
+105
-1
pom.xml
pom.xml
+1
-1
CustomExceptionHandler.kt
...novasi/daerah/master/controller/CustomExceptionHandler.kt
+104
-0
No files found.
pom.xml
View file @
76247feb
...
@@ -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>
...
...
src/main/kotlin/id/co/sangkuriang/inovasi/daerah/master/controller/CustomExceptionHandler.kt
0 → 100644
View file @
76247feb
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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment