-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce validation framework (#1041)
* mvolf - web - Implement copy project functionality * mvolf - web - Implement copy project functionality * #973 - Copy project * #973 - Copy project * #973 - Don't copy project if existing project-id is used. * #973 - Fixed code formatting * #973 - Handle duplicate project id on project create and copy. Some additional small modifications and fixes. * Introduced validation framework and used it for create and copy project actions --------- Co-authored-by: mvolf <[email protected]>
- Loading branch information
Showing
25 changed files
with
863 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
karavan-web/karavan-app/src/main/java/org/apache/camel/karavan/shared/error/Error.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.apache.camel.karavan.shared.error; | ||
|
||
import java.util.Objects; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class Error { | ||
private String field; | ||
private String message; | ||
private String code; | ||
|
||
public Error(String message) { | ||
this.message = message; | ||
} | ||
|
||
public Error(String field, String message) { | ||
this.field = field; | ||
this.message = message; | ||
} | ||
|
||
public Error(String field, String message, String code) { | ||
this.field = field; | ||
this.message = message; | ||
this.code = code; | ||
} | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
public void setField(String field) { | ||
this.field = field; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Error error = (Error) o; | ||
return Objects.equals(field, error.field) && Objects.equals(message, error.message) | ||
&& Objects.equals(code, error.code); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(field, message, code); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Error{" + | ||
"field='" + field + '\'' + | ||
", message='" + message + '\'' + | ||
", code='" + code + '\'' + | ||
'}'; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...an-web/karavan-app/src/main/java/org/apache/camel/karavan/shared/error/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.apache.camel.karavan.shared.error; | ||
|
||
import java.util.Collection; | ||
import java.util.Date; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class ErrorResponse { | ||
private final Date timestamp; | ||
private final int status; | ||
private final String error; | ||
private final String message; | ||
private Collection<Error> errors; | ||
|
||
public ErrorResponse( | ||
int httpStatus, | ||
String reasonPhrase, | ||
String message | ||
) { | ||
this.timestamp = new Date(); | ||
this.status = httpStatus; | ||
this.error = reasonPhrase; | ||
this.message = message; | ||
} | ||
|
||
public ErrorResponse( | ||
int httpStatus, | ||
String reasonPhrase, | ||
String message, | ||
Collection<Error> errors | ||
) { | ||
this.timestamp = new Date(); | ||
this.status = httpStatus; | ||
this.error = reasonPhrase; | ||
this.message = message; | ||
this.errors = errors; | ||
} | ||
|
||
public Date getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public String getError() { | ||
return error; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public Collection<Error> getErrors() { | ||
return errors; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...pp/src/main/java/org/apache/camel/karavan/shared/exception/ExceptionToResponseMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.apache.camel.karavan.shared.exception; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.apache.camel.karavan.shared.error.Error; | ||
import org.apache.camel.karavan.shared.error.ErrorResponse; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.reactive.RestResponse; | ||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
|
||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
public class ExceptionToResponseMapper { | ||
private static final Logger LOGGER = Logger.getLogger(ExceptionToResponseMapper.class.getName()); | ||
|
||
@ServerExceptionMapper | ||
public RestResponse<Object> validationException(ValidationException exception) { | ||
List<Error> errors = (exception).getErrors() | ||
.stream() | ||
.map(fieldError -> new Error(fieldError.getField(), fieldError.getMessage())) | ||
.toList(); | ||
|
||
return logAndBuildResponse( | ||
exception, | ||
Response.Status.BAD_REQUEST.getStatusCode(), | ||
Response.Status.BAD_REQUEST.getReasonPhrase(), | ||
errors | ||
); | ||
} | ||
|
||
private RestResponse<Object> logAndBuildResponse( | ||
Throwable exception, | ||
int status, | ||
String reasonPhrase, | ||
Collection<Error> errors | ||
) { | ||
LOGGER.error("Error occurred", exception); | ||
|
||
String cause = (exception.getCause() != null) ? exception.getCause().getMessage() : null; | ||
String message = (cause != null) ? exception.getMessage() + ", caused by: " + cause : exception.getMessage(); | ||
|
||
if (message == null) { | ||
message = exception.getClass().toString(); | ||
} | ||
|
||
// Hide errors array if there are no errors and leave just error message | ||
if (errors != null && errors.isEmpty()) { | ||
errors = null; | ||
} | ||
|
||
ErrorResponse responseBody = new ErrorResponse( | ||
status, | ||
reasonPhrase, | ||
message, | ||
errors | ||
); | ||
|
||
return RestResponse.ResponseBuilder | ||
.create(status) | ||
.entity(responseBody) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
...n-app/src/main/java/org/apache/camel/karavan/shared/exception/ProjectExistsException.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.