Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Colin DAMON
pouet
Commits
2bb46476
Commit
2bb46476
authored
Jul 27, 2020
by
Colin DAMON
Browse files
Added error handling for query string
parent
3e412278
Pipeline
#30370
passed with stage
in 8 minutes and 40 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/ippon/pouet/common/infrastructure/primary/PouetErrorHandler.java
View file @
2bb46476
...
...
@@ -24,6 +24,7 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.http.converter.HttpMessageNotReadableException
;
import
org.springframework.security.access.AccessDeniedException
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
...
...
@@ -178,6 +179,20 @@ public class PouetErrorHandler extends ResponseEntityExceptionHandler {
return
new
ResponseEntity
<>(
error
,
HttpStatus
.
BAD_REQUEST
);
}
@Override
protected
ResponseEntity
<
Object
>
handleBindException
(
BindException
exception
,
HttpHeaders
headers
,
HttpStatus
status
,
WebRequest
request
)
{
List
<
PouetFieldError
>
fieldErrors
=
exception
.
getFieldErrors
().
stream
().
map
(
toPouetFieldError
()).
collect
(
Collectors
.
toList
());
PouetError
error
=
new
PouetError
(
BAD_REQUEST_KEY
,
getMessage
(
BAD_REQUEST_KEY
,
null
),
fieldErrors
);
return
new
ResponseEntity
<>(
error
,
HttpStatus
.
BAD_REQUEST
);
}
@ExceptionHandler
public
ResponseEntity
<
PouetError
>
handleBeanValidationError
(
ConstraintViolationException
exception
)
{
logger
.
debug
(
"Bean validation error {}"
,
exception
.
getMessage
(),
exception
);
...
...
src/main/java/com/ippon/pouet/common/infrastructure/primary/ValidationMessage.java
View file @
2bb46476
...
...
@@ -3,6 +3,8 @@ package com.ippon.pouet.common.infrastructure.primary;
public
final
class
ValidationMessage
{
public
static
final
String
MANDATORY
=
"user.mandatory"
;
public
static
final
String
WRONG_FORMAT
=
"user.wrong-format"
;
public
static
final
String
VALUE_TOO_LOW
=
"user.too-low"
;
public
static
final
String
VALUE_TOO_HIGH
=
"user.too-high"
;
private
ValidationMessage
()
{}
}
src/main/resources/i18n/messages.properties
View file @
2bb46476
...
...
@@ -29,6 +29,8 @@ pouet.error.status-exception=Une erreur {{ status }} est survenue lors du traite
pouet.error.user.bad-request
=
Les données que vous avez saisies sont incorrectes.
pouet.error.user.mandatory
=
Le champ est obligatoire.
pouet.error.user.wrong-format
=
Le format n'est pas correct, il doit respecter "{{ regexp }}".
pouet.error.user.too-low
=
La valeur que vous avez entrée est trop petite, le minimum autorisé est {{ value }}.
pouet.error.user.too-high
=
La valeur que vous avez entrée est trop grande, le maximum autorisé est {{ value }}.
pouet.error.user.authentication-not-authenticated
=
Vous devez être authentifié pour acceder à cette ressource.
pouet.error.user.access-denied
=
Vous n'avez pas les droits suffisants pour acceder à cette ressource.
pouet.error.user.e-mail-already-used
=
Cette adresse email est déjà utilisée dans pouet.
...
...
src/main/resources/i18n/messages_en.properties
View file @
2bb46476
...
...
@@ -29,6 +29,8 @@ pouet.error.status-exception=An error {{ status }} occured while handling your r
pouet.error.user.bad-request
=
The values you entered are incorrects.
pouet.error.user.mandatory
=
The field is mandatory.
pouet.error.user.wrong-format
=
The format is incorrect, it has to match "{{ regexp }}".
pouet.error.user.too-low
=
The value you entered is too low, minimum autorized is {{ value }}.
pouet.error.user.too-high
=
The value you entered is too high, maximum authorized is {{ value }}.
pouet.error.user.authentication-not-authenticated
=
You must be authenticated to access this resource.
pouet.error.user.access-denied
=
You don't have sufficient rights to access this resource.
pouet.error.user.e-mail-already-used
=
This email address is already used in the pouet.
...
...
src/test/java/com/ippon/pouet/common/infrastructure/primary/ErrorsResource.java
View file @
2bb46476
...
...
@@ -57,6 +57,9 @@ class ErrorsResource {
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
);
}
@GetMapping
public
void
queryStringWithRangedValue
(
@Validated
QueryParameter
parameter
)
{}
@GetMapping
(
"/{complicated}"
)
public
void
complicatedArg
(
@Validated
@Pattern
(
message
=
ValidationMessage
.
WRONG_FORMAT
,
regexp
=
"complicated"
)
@PathVariable
(
"complicated"
)
String
complicated
...
...
src/test/java/com/ippon/pouet/common/infrastructure/primary/PouetErrorHandlerIntTest.java
View file @
2bb46476
...
...
@@ -111,6 +111,34 @@ class PouetErrorHandlerIntTest {
.
contains
(
"Le format n'est pas correct, il doit respecter \\\"complicated\\\"."
);
}
@Test
public
void
shouldMapMinValueQueryStringBeanValidationErrors
()
throws
Exception
{
String
response
=
mockMvc
.
perform
(
get
(
errorEndpoint
(
"?parameter=1"
)).
header
(
HttpHeaders
.
ACCEPT_LANGUAGE
,
FRANCE_TAG
))
.
andExpect
(
status
().
isBadRequest
())
.
andReturn
()
.
getResponse
()
.
getContentAsString
(
UTF_8
);
assertThat
(
response
)
.
contains
(
"Les données que vous avez saisies sont incorrectes."
)
.
contains
(
"La valeur que vous avez entrée est trop petite, le minimum autorisé est 42."
);
}
@Test
public
void
shouldMapMaxValueQueryStringBeanValidationErrors
()
throws
Exception
{
String
response
=
mockMvc
.
perform
(
get
(
errorEndpoint
(
"?parameter=100"
)).
header
(
HttpHeaders
.
ACCEPT_LANGUAGE
,
FRANCE_TAG
))
.
andExpect
(
status
().
isBadRequest
())
.
andReturn
()
.
getResponse
()
.
getContentAsString
(
UTF_8
);
assertThat
(
response
)
.
contains
(
"Les données que vous avez saisies sont incorrectes."
)
.
contains
(
"La valeur que vous avez entrée est trop grande, le maximum autorisé est 42."
);
}
@Test
public
void
shouldMapBodyBeanValidationErrors
()
throws
Exception
{
String
response
=
mockMvc
...
...
src/test/java/com/ippon/pouet/common/infrastructure/primary/QueryParameter.java
0 → 100644
View file @
2bb46476
package
com.ippon.pouet.common.infrastructure.primary
;
import
javax.validation.constraints.Max
;
import
javax.validation.constraints.Min
;
class
QueryParameter
{
private
int
parameter
;
@Min
(
message
=
ValidationMessage
.
VALUE_TOO_LOW
,
value
=
42
)
@Max
(
message
=
ValidationMessage
.
VALUE_TOO_HIGH
,
value
=
42
)
public
int
getParameter
()
{
return
parameter
;
}
public
void
setParameter
(
int
parameter
)
{
this
.
parameter
=
parameter
;
}
}
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