Search code examples
mysqlangularjsspring-bootspring-jdbcerror-messaging

Show SQL error Message in AngularJS


I'm trying to create an application where the user can explicitly see the SQL rules they're violating if they enter a bad input to be persisted or used to query the database. I would like to show this as a popup message on the browser, also I'm using AngularJS on the client side and SpringBoot on the backend. I've seen the post where they are discussing how to print it on the console, Show SQL error message.

public static void printSQLException(SQLException ex) {

for (Throwable e : ex) {
    if (e instanceof SQLException) {
        if (ignoreSQLException(
            ((SQLException)e).
            getSQLState()) == false) {

            e.printStackTrace(System.err);
            System.err.println("SQLState: " +
                ((SQLException)e).getSQLState());

            System.err.println("Error Code: " +
                ((SQLException)e).getErrorCode());

            System.err.println("Message: " + e.getMessage());

            Throwable t = ex.getCause();
            while(t != null) {
                System.out.println("Cause: " + t);
                t = t.getCause();
            }
        }
    }
  }
}

But in that question it is showing how to retrieve it in the console, not the browser. By default the HttpError message is from SpringBoot where it gives a BadSQLGrammarExcpetion. I require the exact SQLError Message to show the actual error that occurs on the database. I'm a bit new to AngularJS since I'm mainly a backend developer. So if they're are any examples that can be offered I'd really appreciate it.


Solution

  • So I took a different approach...rather than explicitly trying to display the SQL errors which have occurred, based on the scenario which took place I've written my own Exceptions messages and displayed them through the error object. This doesn't give the users too much information so it may lead to security breaches, but just enough to know the problem that has occurred.