-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[#14312] Add more developer friendly schema validation error #14313
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
Open
TobyCyan
wants to merge
26
commits into
TEAMMATES:master
Choose a base branch
from
TobyCyan:more-friendly-schema-validation-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
24eb254
Throw start up exception with custom message and stop server
TobyCyan 460f1d1
Create dev server error handling system
TobyCyan ac3cff9
Rename package
TobyCyan 8f9b533
Rename error handler list and make private
TobyCyan 5e4037d
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan e4f4b8a
Handle exception instead of throwable
TobyCyan 793df49
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan 5f36b11
Commit suggestions
TobyCyan e0a02dc
Simplify error handling
TobyCyan 10a0583
Remove package from testng
TobyCyan addee80
Fail fast based on liquibase status
TobyCyan e7718f1
Move packages into main
TobyCyan ce35882
Use liquibase API
TobyCyan f3b2c98
Move status checker to liquibase package
TobyCyan 229170d
Use instanceof to determine exception
TobyCyan 908a88f
Fix pmd
TobyCyan 0f78125
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan 231501d
Revert package comment
TobyCyan 885715e
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan d3bf228
Upgrade liquibase version
TobyCyan 553090d
Normalize changeset file paths
TobyCyan fafd51e
Remove liquibase core testImplementation dependency
TobyCyan fc63779
Extract transform start up exception
TobyCyan 8d7190e
Merge branch 'master' into more-friendly-schema-validation-error
TobyCyan 947dabf
Revert liquibase core version
TobyCyan 717f6e6
Force commons-lang3 version
TobyCyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
80 changes: 80 additions & 0 deletions
80
src/main/java/teammates/ui/errorhandlers/DevServerStartupErrorHandler.java
This file contains hidden or 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,80 @@ | ||
| package teammates.ui.errorhandlers; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
|
|
||
| import org.hibernate.tool.schema.spi.SchemaManagementException; | ||
|
|
||
| import teammates.ui.exception.DevServerStartupException; | ||
|
|
||
| /** | ||
| * Handles startup errors with dev-server-friendly messages where possible. | ||
| */ | ||
| public final class DevServerStartupErrorHandler { | ||
|
|
||
| private static final String SCHEMA_VALIDATION_PREFIX = "Schema-validation:"; | ||
| private static final String SCHEMA_VALIDATION_MESSAGE = String.join(System.lineSeparator(), | ||
| "", | ||
| "============================================================", | ||
| "Database schema validation failed:", | ||
| "%s", | ||
| "", | ||
| "Your local database schema is out of date.", | ||
| "Run: ./gradlew liquibaseUpdate", | ||
| "Then restart the local server.", | ||
| "", | ||
| "For more details, see docs/how-to/schema-migration.md", | ||
| "============================================================", | ||
| ""); | ||
|
|
||
| /** | ||
| * Add new common startup errors here in order of specificity. | ||
| */ | ||
| private static final List<Function<Throwable, Optional<String>>> ERROR_MESSAGE_PROVIDERS = List.of( | ||
| DevServerStartupErrorHandler::buildSchemaValidationMessage); | ||
|
|
||
| private DevServerStartupErrorHandler() { | ||
| // Utility class | ||
| } | ||
|
|
||
| /** | ||
| * Transforms a recognized startup error into a dev-server-friendly exception. | ||
| * Returns the original exception when no provider recognizes it. | ||
| */ | ||
| public static Exception transform(Exception e) { | ||
| return ERROR_MESSAGE_PROVIDERS.stream() | ||
| .map(provider -> provider.apply(e)) | ||
| .flatMap(Optional::stream) | ||
| .findFirst() | ||
| .<Exception>map(message -> new DevServerStartupException(message)) | ||
| .orElse(e); | ||
| } | ||
|
|
||
| private static Optional<String> buildSchemaValidationMessage(Throwable error) { | ||
| Throwable schemaValidationFailure = findSchemaValidationFailure(error); | ||
| if (schemaValidationFailure == null) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| String details = Optional.ofNullable(schemaValidationFailure.getMessage()) | ||
| .map(String::trim) | ||
| .filter(message -> !message.isEmpty()) | ||
| .orElse("Hibernate reported a schema validation error."); | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
| return Optional.of(String.format(SCHEMA_VALIDATION_MESSAGE, details)); | ||
| } | ||
|
|
||
| private static Throwable findSchemaValidationFailure(Throwable error) { | ||
| Throwable current = error; | ||
| while (current != null) { | ||
| String message = current.getMessage(); | ||
| if (current instanceof SchemaManagementException | ||
| || message != null && message.trim().startsWith(SCHEMA_VALIDATION_PREFIX)) { | ||
| return current; | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
| current = current.getCause(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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,4 @@ | ||
| /** | ||
| * Contains handlers for errors that occur during dev server startup, and the factory to retrieve them. | ||
| */ | ||
| package teammates.ui.errorhandlers; | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
11 changes: 11 additions & 0 deletions
11
src/main/java/teammates/ui/exception/DevServerStartupException.java
This file contains hidden or 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,11 @@ | ||
| package teammates.ui.exception; | ||
|
|
||
| /** | ||
| * Exception thrown when an error occurs during dev server startup. | ||
| */ | ||
| public class DevServerStartupException extends RuntimeException { | ||
| public DevServerStartupException(String message) { | ||
| // Null cause to avoid printing the stack trace twice. | ||
| super(message, null, true, false); | ||
| } | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.