File tree Expand file tree Collapse file tree 8 files changed +51
-20
lines changed
jooby-avaje-validator/src
test/java/io/jooby/avaje/validator/app
jooby-hibernate-validator/src
io/jooby/hibernate/validator
test/java/io/jooby/hibernate/validator/app Expand file tree Collapse file tree 8 files changed +51
-20
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ public class AvajeValidatorModule implements Extension {
5555 private StatusCode statusCode = StatusCode .UNPROCESSABLE_ENTITY ;
5656 private String title = "Validation failed" ;
5757 private boolean disableDefaultViolationHandler = false ;
58+ private boolean logException ;
5859
5960 /**
6061 * Setups a configurer callback.
@@ -91,6 +92,16 @@ public AvajeValidatorModule validationTitle(@NonNull String title) {
9192 return this ;
9293 }
9394
95+ /**
96+ * Ask the error handler to log the exception. Default is: false.
97+ *
98+ * @return This module.
99+ */
100+ public AvajeValidatorModule logException () {
101+ this .logException = true ;
102+ return this ;
103+ }
104+
94105 /**
95106 * Disables default constraint violation handler. By default {@link AvajeValidatorModule} provides
96107 * built-in error handler for the {@link ConstraintViolationException} Such exceptions are
@@ -147,7 +158,7 @@ public void install(@NonNull Jooby app) {
147158 app .getServices ().put (BeanValidator .class , new BeanValidatorImpl (validator ));
148159
149160 if (!disableDefaultViolationHandler ) {
150- app .error (new ConstraintViolationHandler (statusCode , title ));
161+ app .error (new ConstraintViolationHandler (statusCode , title , logException ));
151162 }
152163 }
153164
Original file line number Diff line number Diff line change 1313import java .util .List ;
1414import java .util .Map ;
1515
16+ import org .slf4j .Logger ;
17+ import org .slf4j .LoggerFactory ;
18+
1619import edu .umd .cs .findbugs .annotations .NonNull ;
1720import io .avaje .validation .ConstraintViolation ;
1821import io .avaje .validation .ConstraintViolationException ;
5558 */
5659public class ConstraintViolationHandler implements ErrorHandler {
5760 private static final String ROOT_VIOLATIONS_PATH = "" ;
61+ private final Logger log = LoggerFactory .getLogger (getClass ());
5862 private final StatusCode statusCode ;
5963 private final String title ;
64+ private final boolean logException ;
6065
61- public ConstraintViolationHandler (@ NonNull StatusCode statusCode , @ NonNull String title ) {
66+ public ConstraintViolationHandler (
67+ @ NonNull StatusCode statusCode , @ NonNull String title , boolean logException ) {
6268 this .statusCode = statusCode ;
6369 this .title = title ;
70+ this .logException = logException ;
6471 }
6572
6673 @ Override
6774 public void apply (@ NonNull Context ctx , @ NonNull Throwable cause , @ NonNull StatusCode code ) {
6875 if (cause instanceof ConstraintViolationException ex ) {
76+ if (logException ) {
77+ log .error (ErrorHandler .errorMessage (ctx , code ), cause );
78+ }
6979 var violations = ex .violations ();
7080
7181 var groupedByPath = violations .stream ().collect (groupingBy (ConstraintViolation ::path ));
Original file line number Diff line number Diff line change 1111 requires static com .github .spotbugs .annotations ;
1212 requires typesafe .config ;
1313 requires transitive io .avaje .validation ;
14+ requires org .slf4j ;
1415}
Original file line number Diff line number Diff line change 88import io .jooby .Jooby ;
99import io .jooby .StatusCode ;
1010import io .jooby .avaje .validator .AvajeValidatorModule ;
11- import io .jooby .avaje .validator .ConstraintViolationHandler ;
1211import io .jooby .jackson .JacksonModule ;
13- import jakarta .validation .ConstraintViolationException ;
1412
1513public class App extends Jooby {
1614
@@ -19,12 +17,8 @@ public class App extends Jooby {
1917
2018 {
2119 install (new JacksonModule ());
22- install (new AvajeValidatorModule ());
20+ install (new AvajeValidatorModule (). validationTitle ( DEFAULT_TITLE ). statusCode ( STATUS_CODE ) );
2321
2422 mvc (new Controller ());
25-
26- error (
27- ConstraintViolationException .class ,
28- new ConstraintViolationHandler (STATUS_CODE , DEFAULT_TITLE ));
2923 }
3024}
Original file line number Diff line number Diff line change 1313import java .util .List ;
1414import java .util .Map ;
1515
16+ import org .slf4j .Logger ;
17+ import org .slf4j .LoggerFactory ;
18+
1619import edu .umd .cs .findbugs .annotations .NonNull ;
1720import io .jooby .Context ;
1821import io .jooby .ErrorHandler ;
5457 * @since 3.3.1
5558 */
5659public class ConstraintViolationHandler implements ErrorHandler {
57-
5860 private static final String ROOT_VIOLATIONS_PATH = "" ;
59-
61+ private final Logger log = LoggerFactory . getLogger ( ConstraintViolationHandler . class );
6062 private final StatusCode statusCode ;
6163 private final String title ;
64+ private final boolean logException ;
6265
63- public ConstraintViolationHandler (@ NonNull StatusCode statusCode , @ NonNull String title ) {
66+ public ConstraintViolationHandler (
67+ @ NonNull StatusCode statusCode , @ NonNull String title , boolean logException ) {
6468 this .statusCode = statusCode ;
6569 this .title = title ;
70+ this .logException = logException ;
6671 }
6772
6873 @ Override
6974 public void apply (@ NonNull Context ctx , @ NonNull Throwable cause , @ NonNull StatusCode code ) {
7075 if (cause instanceof ConstraintViolationException ex ) {
76+ if (logException ) {
77+ log .error (ErrorHandler .errorMessage (ctx , code ), cause );
78+ }
7179 var violations = ex .getConstraintViolations ();
7280
7381 var groupedByPath =
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ public class HibernateValidatorModule implements Extension {
5454 private StatusCode statusCode = StatusCode .UNPROCESSABLE_ENTITY ;
5555 private String title = "Validation failed" ;
5656 private boolean disableDefaultViolationHandler = false ;
57+ private boolean logException = false ;
5758
5859 /**
5960 * Setups a configurer callback.
@@ -79,6 +80,16 @@ public HibernateValidatorModule statusCode(@NonNull StatusCode statusCode) {
7980 return this ;
8081 }
8182
83+ /**
84+ * Ask the error handler to log the exception. Default is: false.
85+ *
86+ * @return This module.
87+ */
88+ public HibernateValidatorModule logException () {
89+ this .logException = true ;
90+ return this ;
91+ }
92+
8293 /**
8394 * Overrides the default title for the errors produced by validation. Default title is "Validation
8495 * failed"
@@ -129,7 +140,8 @@ public void install(@NonNull Jooby app) throws Exception {
129140
130141 if (!disableDefaultViolationHandler ) {
131142 app .error (
132- ConstraintViolationException .class , new ConstraintViolationHandler (statusCode , title ));
143+ ConstraintViolationException .class ,
144+ new ConstraintViolationHandler (statusCode , title , logException ));
133145 }
134146 }
135147 }
Original file line number Diff line number Diff line change 1212 requires typesafe .config ;
1313 requires org .hibernate .validator ;
1414 requires jakarta .validation ;
15+ requires org .slf4j ;
1516}
Original file line number Diff line number Diff line change 77
88import io .jooby .Jooby ;
99import io .jooby .StatusCode ;
10- import io .jooby .hibernate .validator .ConstraintViolationHandler ;
1110import io .jooby .hibernate .validator .HibernateValidatorModule ;
1211import io .jooby .jackson .JacksonModule ;
13- import jakarta .validation .ConstraintViolationException ;
1412
1513public class App extends Jooby {
1614
@@ -19,12 +17,8 @@ public class App extends Jooby {
1917
2018 {
2119 install (new JacksonModule ());
22- install (new HibernateValidatorModule ());
20+ install (new HibernateValidatorModule (). validationTitle ( DEFAULT_TITLE ). statusCode ( STATUS_CODE ) );
2321
2422 mvc (new Controller ());
25-
26- error (
27- ConstraintViolationException .class ,
28- new ConstraintViolationHandler (STATUS_CODE , DEFAULT_TITLE ));
2923 }
3024}
You can’t perform that action at this time.
0 commit comments