Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
order-management-backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ascend
order-management-backend
Commits
3030a1ce
Commit
3030a1ce
authored
May 11, 2021
by
Alex Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AFP-91]
🥅
Create custom exceptions & advices (
@asegers
)
parent
c863039c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
147 additions
and
0 deletions
+147
-0
BadAccessTokenException.java
...fp/ordermanagement/exception/BadAccessTokenException.java
+11
-0
ControllerExceptionAdvice.java
.../ordermanagement/exception/ControllerExceptionAdvice.java
+74
-0
ErrorResponse.java
...java/com/afp/ordermanagement/exception/ErrorResponse.java
+13
-0
InvalidEntityResponse.java
.../afp/ordermanagement/exception/InvalidEntityResponse.java
+34
-0
ResourceNotFoundException.java
.../ordermanagement/exception/ResourceNotFoundException.java
+15
-0
No files found.
src/main/java/com/afp/ordermanagement/exception/BadAccessTokenException.java
0 → 100644
View file @
3030a1ce
package
com
.
afp
.
ordermanagement
.
exception
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
value
=
HttpStatus
.
UNAUTHORIZED
)
public
class
BadAccessTokenException
extends
RuntimeException
{
public
BadAccessTokenException
()
{
super
(
"Invalid access token"
);
}
}
src/main/java/com/afp/ordermanagement/exception/ControllerExceptionAdvice.java
0 → 100644
View file @
3030a1ce
package
com
.
afp
.
ordermanagement
.
exception
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.support.WebExchangeBindException
;
import
org.springframework.web.server.ServerWebExchange
;
@ControllerAdvice
@RequiredArgsConstructor
public
class
ControllerExceptionAdvice
{
@ExceptionHandler
(
BadAccessTokenException
.
class
)
public
ResponseEntity
<
ErrorResponse
>
handleBadAccessTokenException
(
RuntimeException
exc
,
ServerWebExchange
exchange
)
{
final
HttpStatus
status
=
HttpStatus
.
UNAUTHORIZED
;
final
ServerHttpRequest
request
=
exchange
.
getRequest
();
return
new
ResponseEntity
<>(
new
ErrorResponse
(
status
.
value
(),
request
.
getPath
().
value
(),
status
.
getReasonPhrase
(),
exc
.
getMessage
()
),
status
);
}
@ExceptionHandler
(
ResourceNotFoundException
.
class
)
public
ResponseEntity
<
ErrorResponse
>
handleEntityNotFoundException
(
RuntimeException
exc
,
ServerWebExchange
exchange
)
{
final
HttpStatus
status
=
HttpStatus
.
NOT_FOUND
;
final
ServerHttpRequest
request
=
exchange
.
getRequest
();
return
new
ResponseEntity
<>(
new
ErrorResponse
(
status
.
value
(),
request
.
getPath
().
value
(),
status
.
getReasonPhrase
(),
exc
.
getMessage
()
),
status
);
}
@ExceptionHandler
(
RuntimeException
.
class
)
public
ResponseEntity
<
ErrorResponse
>
handleRuntimeException
(
RuntimeException
exc
,
ServerWebExchange
exchange
)
{
final
HttpStatus
status
=
HttpStatus
.
INTERNAL_SERVER_ERROR
;
final
ServerHttpRequest
request
=
exchange
.
getRequest
();
return
new
ResponseEntity
<>(
new
ErrorResponse
(
status
.
value
(),
request
.
getPath
().
value
(),
status
.
getReasonPhrase
(),
exc
.
getMessage
()
),
status
);
}
@ExceptionHandler
(
WebExchangeBindException
.
class
)
public
ResponseEntity
<
ErrorResponse
>
webExchangeBindException
(
WebExchangeBindException
exc
,
ServerWebExchange
exchange
)
{
final
HttpStatus
status
=
exc
.
getStatus
();
final
ServerHttpRequest
request
=
exchange
.
getRequest
();
if
(
InvalidEntityResponse
.
isEntityValid
(
exc
.
getTarget
()))
{
return
new
ResponseEntity
<>(
new
ErrorResponse
(
status
.
value
(),
request
.
getPath
().
value
(),
status
.
getReasonPhrase
(),
exc
.
getMessage
()
),
status
);
}
return
new
ResponseEntity
<>(
new
InvalidEntityResponse
(
status
.
value
(),
request
.
getPath
().
value
(),
status
.
getReasonPhrase
(),
"Validation failed"
,
exc
.
getTarget
()
),
status
);
}
}
src/main/java/com/afp/ordermanagement/exception/ErrorResponse.java
0 → 100644
View file @
3030a1ce
package
com
.
afp
.
ordermanagement
.
exception
;
import
lombok.Data
;
import
lombok.RequiredArgsConstructor
;
import
java.time.Instant
;
@Data
@RequiredArgsConstructor
public
class
ErrorResponse
{
public
final
Integer
status
;
public
final
String
path
,
error
,
message
;
public
String
timestamp
=
Instant
.
now
().
toString
();
}
src/main/java/com/afp/ordermanagement/exception/InvalidEntityResponse.java
0 → 100644
View file @
3030a1ce
package
com
.
afp
.
ordermanagement
.
exception
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.Validation
;
import
javax.validation.Validator
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
InvalidEntityResponse
extends
ErrorResponse
{
public
List
<
Map
<
String
,
String
>>
fieldErrors
;
public
InvalidEntityResponse
(
Integer
status
,
String
path
,
String
type
,
String
message
,
Object
entity
)
{
super
(
status
,
path
,
type
,
message
);
this
.
fieldErrors
=
InvalidEntityResponse
.
parseViolations
(
entity
);
}
public
static
boolean
isEntityValid
(
Object
entity
)
{
return
validator
.
validate
(
entity
).
isEmpty
();
}
static
private
final
Validator
validator
=
Validation
.
buildDefaultValidatorFactory
().
getValidator
();
static
private
List
<
Map
<
String
,
String
>>
parseViolations
(
Object
entity
)
{
List
<
Map
<
String
,
String
>>
fieldErrors
=
new
ArrayList
<>();
for
(
ConstraintViolation
<
Object
>
cv
:
validator
.
validate
(
entity
))
{
Map
<
String
,
String
>
errorMap
=
new
HashMap
<
String
,
String
>()
{{
put
(
"field"
,
cv
.
getPropertyPath
().
toString
());
put
(
"message"
,
cv
.
getMessage
());
}};
fieldErrors
.
add
(
errorMap
);
}
return
fieldErrors
;
}
}
src/main/java/com/afp/ordermanagement/exception/ResourceNotFoundException.java
0 → 100644
View file @
3030a1ce
package
com
.
afp
.
ordermanagement
.
exception
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
value
=
HttpStatus
.
NOT_FOUND
)
public
class
ResourceNotFoundException
extends
RuntimeException
{
public
ResourceNotFoundException
()
{
super
(
"Resource not found"
);
}
public
ResourceNotFoundException
(
String
message
)
{
super
(
message
);
}
}
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