Skip to content

Add ValidationException Cause #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java_version: [11, 17, 20]
java_version: [17, 20]
os: [ubuntu-latest]

steps:
Expand Down
50 changes: 22 additions & 28 deletions http-api/src/main/java/io/avaje/http/api/ValidationException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,62 @@

/**
* Exception used with Validator.
* <p>
* Typically this is used when validating a bean populated by request
* body content.
* <p>
* Generally this exception type is registered with an exception handler
* and configured to return a 422 or 400 http status response with the
* errors as a map of fields to error message.
*
* <p>Typically this is used when validating a bean populated by request body content.
*
* <p>Generally this exception type is registered with an exception handler and configured to return
* a 422 or 400 http status response with the errors as a map of fields to error message.
*/
public class ValidationException extends IllegalArgumentException {

private static final long serialVersionUID = 1L;

private int status = 422;

private Map<String, Object> errors;

/**
* Create with a message.
*/
/** Create with a message. */
public ValidationException(String message) {
super(message);
}

/**
* Create with a status and message.
*/
/** Create with a status and message. */
public ValidationException(int status, String message) {
super(message);
this.status = status;
}

/**
* Create with a status message and errors.
*/
/** Create with a status message and errors. */
public ValidationException(int status, String message, Map<String, Object> errors) {
super(message);
this.status = status;
this.errors = errors;
}

/**
* Return the suggested HTTP status to use in the response.
*/
/** Create with a status message and errors. */
public ValidationException(
int status, String message, Throwable cause, Map<String, Object> errors) {
super(message, cause);
this.status = status;
this.errors = errors;
}

/** Return the suggested HTTP status to use in the response. */
public int getStatus() {
return status;
}

/**
* Set the suggested HTTP status to use in the response.
*/
/** Set the suggested HTTP status to use in the response. */
public void setStatus(int status) {
this.status = status;
}

/**
* Return the errors typically as a map of field to error message.
*/
/** Return the errors typically as a map of field to error message. */
public Map<String, Object> getErrors() {
return errors;
}

/**
* Set the errors.
*/
/** Set the errors. */
public void setErrors(Map<String, Object> errors) {
this.errors = errors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ void buildApiDocumentation(MethodDocBuilder methodDoc) {
void writeValidate(Append writer) {
if (!contextType && typeHandler == null) {
if (useValidation) {
writer.append("validator.validate(%s, ", varName);
writer.append("var validLanguage = ");
platform().writeAcceptLanguage(writer);
writer.append(";").eol();
writer.append(" validator.validate(%s, validLanguage", varName);

if (!validationGroups.isEmpty()) {
validationGroups.forEach(g -> writer.append(", %s", Util.shortName(g)));
}
validationGroups.forEach(g -> writer.append(", %s", Util.shortName(g)));

writer.append(");").eol();
} else {
Expand Down