def test_extract_errors(self): """ Test that custom error messages are extracted correctly. """ msg = 'psql: error: FATAL: role "testuser" does not exist' result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.CONNECTION_INVALID_USERNAME_ERROR, message='The username "testuser" does not exist.', level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [ { "code": 1012, "message": ("Issue 1012 - The username provided when " "connecting to a database is not valid."), }, ], }, ) ] msg = ( 'psql: error: could not translate host name "locahost" to address: ' "nodename nor servname provided, or not known") result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.CONNECTION_INVALID_HOSTNAME_ERROR, message='The hostname "locahost" cannot be resolved.', level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [{ "code": 1007, "message": "Issue 1007 - The hostname provided " "can't be resolved.", }], }, ) ] msg = dedent(""" psql: error: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 12345? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 12345? """) result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.CONNECTION_PORT_CLOSED_ERROR, message= 'Port 12345 on hostname "localhost" refused the connection.', level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [{ "code": 1008, "message": "Issue 1008 - The port is closed." }], }, ) ] msg = dedent(""" psql: error: could not connect to server: Operation timed out Is the server running on host "example.com" (93.184.216.34) and accepting TCP/IP connections on port 12345? """) result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.CONNECTION_HOST_DOWN_ERROR, message=('The host "example.com" might be down, ' "and can't be reached on port 12345."), level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [{ "code": 1009, "message": "Issue 1009 - The host might be down, " "and can't be reached on the provided port.", }], }, ) ] # response with IP only msg = dedent(""" psql: error: could not connect to server: Operation timed out Is the server running on host "93.184.216.34" and accepting TCP/IP connections on port 12345? """) result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.CONNECTION_HOST_DOWN_ERROR, message=('The host "93.184.216.34" might be down, ' "and can't be reached on port 12345."), level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [{ "code": 1009, "message": "Issue 1009 - The host might be down, " "and can't be reached on the provided port.", }], }, ) ] msg = 'FATAL: password authentication failed for user "postgres"' result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.CONNECTION_INVALID_PASSWORD_ERROR, message= ('The password provided for username "postgres" is incorrect.' ), level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [ { "code": 1013, "message": ("Issue 1013 - The password provided when " "connecting to a database is not valid."), }, ], }, ) ] msg = 'database "badDB" does not exist' result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( message='Unable to connect to database "badDB".', error_type=SupersetErrorType.CONNECTION_UNKNOWN_DATABASE_ERROR, level=ErrorLevel.ERROR, extra={ "engine_name": "PostgreSQL", "issue_codes": [{ "code": 1015, "message": ("Issue 1015 - Either the database is spelled " "incorrectly or does not exist.", ), }], }, ) ]
def test_extract_errors(self): """ Test that custom error messages are extracted correctly. """ msg = 'psql: error: FATAL: role "testuser" does not exist' result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType. TEST_CONNECTION_INVALID_USERNAME_ERROR, message='The username "testuser" does not exist.', level=ErrorLevel.ERROR, extra={"engine_name": "PostgreSQL"}, ) ] msg = 'psql: error: could not translate host name "locahost" to address: nodename nor servname provided, or not known' result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType. TEST_CONNECTION_INVALID_HOSTNAME_ERROR, message='The hostname "locahost" cannot be resolved.', level=ErrorLevel.ERROR, extra={"engine_name": "PostgreSQL"}, ) ] msg = dedent(""" psql: error: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 12345? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 12345? """) result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.TEST_CONNECTION_PORT_CLOSED_ERROR, message= "Port 12345 on hostname localhost refused the connection.", level=ErrorLevel.ERROR, extra={"engine_name": "PostgreSQL"}, ) ] msg = dedent(""" psql: error: could not connect to server: Operation timed out Is the server running on host "example.com" (93.184.216.34) and accepting TCP/IP connections on port 12345? """) result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.TEST_CONNECTION_HOST_DOWN_ERROR, message=("The host example.com might be down, " "and can't be reached on port 12345"), level=ErrorLevel.ERROR, extra={"engine_name": "PostgreSQL"}, ) ] # response with IP only msg = dedent(""" psql: error: could not connect to server: Operation timed out Is the server running on host "93.184.216.34" and accepting TCP/IP connections on port 12345? """) result = PostgresEngineSpec.extract_errors(Exception(msg)) assert result == [ SupersetError( error_type=SupersetErrorType.TEST_CONNECTION_HOST_DOWN_ERROR, message=("The host 93.184.216.34 might be down, " "and can't be reached on port 12345"), level=ErrorLevel.ERROR, extra={"engine_name": "PostgreSQL"}, ) ]