-
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
18
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.
+210
−1
Open
Changes from 6 commits
Commits
Show all changes
18 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 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
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
| package teammates.ui.errorhandlers; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import teammates.ui.exception.DevServerStartupException; | ||
|
|
||
| /** | ||
| * Handles startup errors with dev-server-friendly messages where possible. | ||
| */ | ||
| public final class DevServerStartupErrorHandler { | ||
|
|
||
| private DevServerStartupErrorHandler() { | ||
| // Utility class | ||
| } | ||
|
|
||
| /** | ||
| * Throws a dev-server-friendly exception if a handler matches. | ||
| */ | ||
| public static void throwIfHandled(Exception e) { | ||
| Optional<StartupErrorHandler> handler = DevServerStartupErrorHandlerFactory.getHandler(e); | ||
| if (handler.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| throw new DevServerStartupException(handler.get().buildErrorMessage(e)); | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/main/java/teammates/ui/errorhandlers/DevServerStartupErrorHandlerFactory.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,45 @@ | ||
| package teammates.ui.errorhandlers; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Generates the matching {@link StartupErrorHandler} for a startup error. | ||
| */ | ||
| public final class DevServerStartupErrorHandlerFactory { | ||
|
|
||
| private static final List<Class<? extends StartupErrorHandler>> ERROR_HANDLERS = new ArrayList<>(); | ||
|
|
||
| static { | ||
| map(SchemaValidationStartupErrorHandler.class); | ||
| } | ||
|
|
||
| private DevServerStartupErrorHandlerFactory() { | ||
| // Utility class | ||
| } | ||
|
|
||
| private static void map(Class<? extends StartupErrorHandler> errorHandlerClass) { | ||
| ERROR_HANDLERS.add(errorHandlerClass); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the matching {@link StartupErrorHandler} for the startup error. | ||
| */ | ||
| public static Optional<StartupErrorHandler> getHandler(Exception e) { | ||
| return ERROR_HANDLERS.stream() | ||
| .map(DevServerStartupErrorHandlerFactory::instantiate) | ||
| .filter(handler -> handler.canHandle(e)) | ||
| .findFirst(); | ||
| } | ||
|
|
||
| private static StartupErrorHandler instantiate(Class<? extends StartupErrorHandler> errorHandlerClass) { | ||
| try { | ||
| return errorHandlerClass.getDeclaredConstructor().newInstance(); | ||
| } catch (Exception e) { | ||
| assert false : "Could not create the startup error handler " + errorHandlerClass.getSimpleName(); | ||
| return null; | ||
| } | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
56 changes: 56 additions & 0 deletions
56
src/main/java/teammates/ui/errorhandlers/SchemaValidationStartupErrorHandler.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,56 @@ | ||
| package teammates.ui.errorhandlers; | ||
|
|
||
| import org.hibernate.tool.schema.spi.SchemaManagementException; | ||
|
|
||
| /** | ||
| * Formats Hibernate schema validation failures for local development startup. | ||
| */ | ||
| public final class SchemaValidationStartupErrorHandler implements StartupErrorHandler { | ||
|
|
||
| private static final String SCHEMA_VALIDATION_PREFIX = "Schema-validation:"; | ||
| private static final String 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", | ||
| "============================================================", | ||
| ""); | ||
|
|
||
| @Override | ||
| public boolean canHandle(Exception e) { | ||
| return findSchemaValidationFailure(e) != null; | ||
| } | ||
|
|
||
| @Override | ||
| public String buildErrorMessage(Exception e) { | ||
| return String.format(MESSAGE, getSchemaValidationMessage(e)); | ||
| } | ||
|
|
||
| private static String getSchemaValidationMessage(Exception e) { | ||
| Exception schemaValidationFailure = findSchemaValidationFailure(e); | ||
| if (schemaValidationFailure == null) { | ||
| return e.getMessage(); | ||
| } | ||
| return schemaValidationFailure.getMessage().trim(); | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| private static Exception findSchemaValidationFailure(Exception e) { | ||
| Exception current = e; | ||
| while (current != null) { | ||
| String message = current.getMessage(); | ||
| if (current instanceof SchemaManagementException | ||
| || message != null && message.trim().startsWith(SCHEMA_VALIDATION_PREFIX)) { | ||
| return current; | ||
| } | ||
| current = (Exception) current.getCause(); | ||
| } | ||
| return null; | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/main/java/teammates/ui/errorhandlers/StartupErrorHandler.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,17 @@ | ||
| package teammates.ui.errorhandlers; | ||
|
|
||
| /** | ||
| * Handles startup errors that can be formatted with dev-server-friendly messages. | ||
| */ | ||
| public interface StartupErrorHandler { | ||
|
|
||
| /** | ||
| * Returns whether this handler can format the given startup error. | ||
| */ | ||
| boolean canHandle(Exception e); | ||
|
|
||
| /** | ||
| * Builds the dev-server-friendly message for the given startup error. | ||
| */ | ||
| String buildErrorMessage(Exception e); | ||
| } |
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
|
||
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
| package teammates.ui.exception; | ||
|
|
||
| /** | ||
| * Exception thrown when an error occurs during dev server startup. | ||
| */ | ||
| public class DevServerStartupException extends RuntimeException { | ||
| public DevServerStartupException(String message) { | ||
| super(message, null, true, false); | ||
| } | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
|
||
36 changes: 36 additions & 0 deletions
36
...a/teammates/ui/devserverstartuperrorhandlers/DevServerStartupErrorHandlerFactoryTest.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,36 @@ | ||
| package teammates.ui.devserverstartuperrorhandlers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.hibernate.tool.schema.spi.SchemaManagementException; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import teammates.test.BaseTestCase; | ||
| import teammates.ui.errorhandlers.DevServerStartupErrorHandlerFactory; | ||
| import teammates.ui.errorhandlers.SchemaValidationStartupErrorHandler; | ||
| import teammates.ui.errorhandlers.StartupErrorHandler; | ||
|
|
||
| /** | ||
| * SUT: {@link DevServerStartupErrorHandlerFactory}. | ||
| */ | ||
| public class DevServerStartupErrorHandlerFactoryTest extends BaseTestCase { | ||
|
|
||
| @Test | ||
| public void testGetHandler_schemaValidationException_returnsSchemaValidationHandler() { | ||
| SchemaManagementException exception = new SchemaManagementException( | ||
| "Schema-validation: missing column [institute_id] in table [account_requests]"); | ||
|
|
||
| StartupErrorHandler startupErrorHandler = DevServerStartupErrorHandlerFactory.getHandler(exception) | ||
| .orElseThrow(); | ||
|
|
||
| assertTrue(startupErrorHandler instanceof SchemaValidationStartupErrorHandler); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetHandler_unknownException_returnsEmpty() { | ||
| RuntimeException exception = new RuntimeException("Failed to connect to database"); | ||
|
|
||
| assertTrue(DevServerStartupErrorHandlerFactory.getHandler(exception).isEmpty()); | ||
| } | ||
|
|
||
| } |
44 changes: 44 additions & 0 deletions
44
...est/java/teammates/ui/devserverstartuperrorhandlers/DevServerStartupErrorHandlerTest.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,44 @@ | ||
| package teammates.ui.devserverstartuperrorhandlers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.hibernate.tool.schema.spi.SchemaManagementException; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import teammates.test.BaseTestCase; | ||
| import teammates.ui.errorhandlers.DevServerStartupErrorHandler; | ||
| import teammates.ui.errorhandlers.SchemaValidationStartupErrorHandler; | ||
| import teammates.ui.exception.DevServerStartupException; | ||
|
|
||
| /** | ||
| * SUT: {@link DevServerStartupErrorHandler}. | ||
| */ | ||
| public class DevServerStartupErrorHandlerTest extends BaseTestCase { | ||
|
|
||
| private static final String SCHEMA_VALIDATION_MESSAGE = | ||
| "Schema-validation: missing column [institute_id] in table [account_requests]"; | ||
|
|
||
| @Test | ||
| public void testThrowIfHandled_schemaValidationException_throwsDevServerStartupException() { | ||
| SchemaManagementException exception = new SchemaManagementException(SCHEMA_VALIDATION_MESSAGE); | ||
| SchemaValidationStartupErrorHandler handler = new SchemaValidationStartupErrorHandler(); | ||
|
|
||
| DevServerStartupException thrown = assertThrows(DevServerStartupException.class, () -> { | ||
| DevServerStartupErrorHandler.throwIfHandled(exception); | ||
| }); | ||
|
|
||
| assertEquals(handler.buildErrorMessage(exception), thrown.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetMessage_nonSchemaValidationException_returnsEmpty() { | ||
| RuntimeException exception = new RuntimeException("Failed to connect to database"); | ||
|
|
||
| assertDoesNotThrow(() -> { | ||
| DevServerStartupErrorHandler.throwIfHandled(exception); | ||
| }); | ||
| } | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
55 changes: 55 additions & 0 deletions
55
...a/teammates/ui/devserverstartuperrorhandlers/SchemaValidationStartupErrorHandlerTest.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,55 @@ | ||
| package teammates.ui.devserverstartuperrorhandlers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.hibernate.tool.schema.spi.SchemaManagementException; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import teammates.test.BaseTestCase; | ||
| import teammates.ui.errorhandlers.SchemaValidationStartupErrorHandler; | ||
|
|
||
| /** | ||
| * SUT: {@link SchemaValidationStartupErrorHandler}. | ||
| */ | ||
| public class SchemaValidationStartupErrorHandlerTest extends BaseTestCase { | ||
|
|
||
| private static final String SCHEMA_VALIDATION_MESSAGE = | ||
| "Schema-validation: missing column [institute_id] in table [account_requests]"; | ||
|
|
||
| private final SchemaValidationStartupErrorHandler handler = new SchemaValidationStartupErrorHandler(); | ||
|
|
||
| @Test | ||
| public void testCanHandle_directSchemaManagementException_returnsTrue() { | ||
| SchemaManagementException exception = new SchemaManagementException(SCHEMA_VALIDATION_MESSAGE); | ||
|
|
||
| assertTrue(handler.canHandle(exception)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCanHandle_wrappedSchemaManagementException_returnsTrue() { | ||
| RuntimeException exception = new RuntimeException("Failed to build session factory", | ||
| new SchemaManagementException(SCHEMA_VALIDATION_MESSAGE)); | ||
|
|
||
| assertTrue(handler.canHandle(exception)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCanHandle_nonSchemaValidationException_returnsFalse() { | ||
| RuntimeException exception = new RuntimeException("Failed to connect to database"); | ||
|
|
||
| assertFalse(handler.canHandle(exception)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBuildErrorMessage_schemaValidationException_includesCommandDocsAndExactError() { | ||
| SchemaManagementException exception = new SchemaManagementException(SCHEMA_VALIDATION_MESSAGE); | ||
|
|
||
| String message = handler.buildErrorMessage(exception); | ||
|
|
||
| assertTrue(message.contains(SCHEMA_VALIDATION_MESSAGE)); | ||
| assertTrue(message.contains("./gradlew liquibaseUpdate")); | ||
| assertTrue(message.contains("docs/how-to/schema-migration.md")); | ||
| } | ||
|
|
||
| } |
4 changes: 4 additions & 0 deletions
4
src/test/java/teammates/ui/devserverstartuperrorhandlers/package-info.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,4 @@ | ||
| /** | ||
| * Contains test cases for {@link teammates.ui.errorhandlers} package. | ||
| */ | ||
| package teammates.ui.devserverstartuperrorhandlers; |
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
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.