Skip to content

Commit

Permalink
validation: add flag to log exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Sep 8, 2024
1 parent e244a02 commit 2e3c40c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class AvajeValidatorModule implements Extension {
private StatusCode statusCode = StatusCode.UNPROCESSABLE_ENTITY;
private String title = "Validation failed";
private boolean disableDefaultViolationHandler = false;
private boolean logException;

/**
* Setups a configurer callback.
Expand Down Expand Up @@ -91,6 +92,16 @@ public AvajeValidatorModule validationTitle(@NonNull String title) {
return this;
}

/**
* Ask the error handler to log the exception. Default is: false.
*
* @return This module.
*/
public AvajeValidatorModule logException() {
this.logException = true;
return this;
}

/**
* Disables default constraint violation handler. By default {@link AvajeValidatorModule} provides
* built-in error handler for the {@link ConstraintViolationException} Such exceptions are
Expand Down Expand Up @@ -147,7 +158,7 @@ public void install(@NonNull Jooby app) {
app.getServices().put(BeanValidator.class, new BeanValidatorImpl(validator));

if (!disableDefaultViolationHandler) {
app.error(new ConstraintViolationHandler(statusCode, title));
app.error(new ConstraintViolationHandler(statusCode, title, logException));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.umd.cs.findbugs.annotations.NonNull;
import io.avaje.validation.ConstraintViolation;
import io.avaje.validation.ConstraintViolationException;
Expand Down Expand Up @@ -55,17 +58,24 @@
*/
public class ConstraintViolationHandler implements ErrorHandler {
private static final String ROOT_VIOLATIONS_PATH = "";
private final Logger log = LoggerFactory.getLogger(getClass());
private final StatusCode statusCode;
private final String title;
private final boolean logException;

public ConstraintViolationHandler(@NonNull StatusCode statusCode, @NonNull String title) {
public ConstraintViolationHandler(
@NonNull StatusCode statusCode, @NonNull String title, boolean logException) {
this.statusCode = statusCode;
this.title = title;
this.logException = logException;
}

@Override
public void apply(@NonNull Context ctx, @NonNull Throwable cause, @NonNull StatusCode code) {
if (cause instanceof ConstraintViolationException ex) {
if (logException) {
log.error(ErrorHandler.errorMessage(ctx, code), cause);
}
var violations = ex.violations();

var groupedByPath = violations.stream().collect(groupingBy(ConstraintViolation::path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
requires static com.github.spotbugs.annotations;
requires typesafe.config;
requires transitive io.avaje.validation;
requires org.slf4j;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import io.jooby.Jooby;
import io.jooby.StatusCode;
import io.jooby.avaje.validator.AvajeValidatorModule;
import io.jooby.avaje.validator.ConstraintViolationHandler;
import io.jooby.jackson.JacksonModule;
import jakarta.validation.ConstraintViolationException;

public class App extends Jooby {

Expand All @@ -19,12 +17,8 @@ public class App extends Jooby {

{
install(new JacksonModule());
install(new AvajeValidatorModule());
install(new AvajeValidatorModule().validationTitle(DEFAULT_TITLE).statusCode(STATUS_CODE));

mvc(new Controller());

error(
ConstraintViolationException.class,
new ConstraintViolationHandler(STATUS_CODE, DEFAULT_TITLE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.Context;
import io.jooby.ErrorHandler;
Expand Down Expand Up @@ -54,20 +57,25 @@
* @since 3.3.1
*/
public class ConstraintViolationHandler implements ErrorHandler {

private static final String ROOT_VIOLATIONS_PATH = "";

private final Logger log = LoggerFactory.getLogger(ConstraintViolationHandler.class);
private final StatusCode statusCode;
private final String title;
private final boolean logException;

public ConstraintViolationHandler(@NonNull StatusCode statusCode, @NonNull String title) {
public ConstraintViolationHandler(
@NonNull StatusCode statusCode, @NonNull String title, boolean logException) {
this.statusCode = statusCode;
this.title = title;
this.logException = logException;
}

@Override
public void apply(@NonNull Context ctx, @NonNull Throwable cause, @NonNull StatusCode code) {
if (cause instanceof ConstraintViolationException ex) {
if (logException) {
log.error(ErrorHandler.errorMessage(ctx, code), cause);
}
var violations = ex.getConstraintViolations();

var groupedByPath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class HibernateValidatorModule implements Extension {
private StatusCode statusCode = StatusCode.UNPROCESSABLE_ENTITY;
private String title = "Validation failed";
private boolean disableDefaultViolationHandler = false;
private boolean logException = false;

/**
* Setups a configurer callback.
Expand All @@ -79,6 +80,16 @@ public HibernateValidatorModule statusCode(@NonNull StatusCode statusCode) {
return this;
}

/**
* Ask the error handler to log the exception. Default is: false.
*
* @return This module.
*/
public HibernateValidatorModule logException() {
this.logException = true;
return this;
}

/**
* Overrides the default title for the errors produced by validation. Default title is "Validation
* failed"
Expand Down Expand Up @@ -129,7 +140,8 @@ public void install(@NonNull Jooby app) throws Exception {

if (!disableDefaultViolationHandler) {
app.error(
ConstraintViolationException.class, new ConstraintViolationHandler(statusCode, title));
ConstraintViolationException.class,
new ConstraintViolationHandler(statusCode, title, logException));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
requires typesafe.config;
requires org.hibernate.validator;
requires jakarta.validation;
requires org.slf4j;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import io.jooby.Jooby;
import io.jooby.StatusCode;
import io.jooby.hibernate.validator.ConstraintViolationHandler;
import io.jooby.hibernate.validator.HibernateValidatorModule;
import io.jooby.jackson.JacksonModule;
import jakarta.validation.ConstraintViolationException;

public class App extends Jooby {

Expand All @@ -19,12 +17,8 @@ public class App extends Jooby {

{
install(new JacksonModule());
install(new HibernateValidatorModule());
install(new HibernateValidatorModule().validationTitle(DEFAULT_TITLE).statusCode(STATUS_CODE));

mvc(new Controller());

error(
ConstraintViolationException.class,
new ConstraintViolationHandler(STATUS_CODE, DEFAULT_TITLE));
}
}

0 comments on commit 2e3c40c

Please sign in to comment.