docstr: Test that custom error messages are extracted correctly.
docstr_tok: ["Test","that","custom","error","messages","are","extracted","correctly","."]
code: 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."
                          ),
                      },
                  ],
                  "invalid": ["username"],
              },
          )
      ]
  
      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.",
                      }
                  ],
                  "invalid": ["host"],
              },
          )
      ]
  
      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.",
                      }
                  ],
                  "invalid": ["host", "port"],
              },
          )
      ]
  
      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.",
                      }
                  ],
                  "invalid": ["host", "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.",
                      }
                  ],
                  "invalid": ["host", "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."
                          ),
                      },
                  ],
                  "invalid": ["username", "password"],
              },
          )
      ]
  
      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.",
                          ),
                      }
                  ],
                  "invalid": ["database"],
              },
          )
      ]
  
      msg = "no password supplied"
      result = PostgresEngineSpec.extract_errors(Exception(msg))
      assert result == [
          SupersetError(
              message="Please re-enter the password.",
              error_type=SupersetErrorType.CONNECTION_ACCESS_DENIED_ERROR,
              level=ErrorLevel.ERROR,
              extra={
                  "invalid": ["password"],
                  "engine_name": "PostgreSQL",
                  "issue_codes": [
                      {
                          "code": 1014,
                          "message": "Issue 1014 - Either the username or the password is wrong.",
                      },
                      {
                          "code": 1015,
                          "message": "Issue 1015 - Either the database is spelled incorrectly or does not exist.",
                      },
                  ],
              },
          )
      ]
  
      msg = 'syntax error at or near "fromm"'
      result = PostgresEngineSpec.extract_errors(Exception(msg))
      assert result == [
          SupersetError(
              message='Please check your query for syntax errors at or near "fromm". Then, try running your query again.',
              error_type=SupersetErrorType.SYNTAX_ERROR,
              level=ErrorLevel.ERROR,
              extra={
                  "engine_name": "PostgreSQL",
                  "issue_codes": [
                      {
                          "code": 1030,
                          "message": "Issue 1030 - The query has a syntax error.",
                      }
                  ],
              },
          )
      ]
  
imports: from textwrap import dedent
  from unittest import mock
  from flask.ctx import AppContext
  from sqlalchemy import column, literal_column
  from sqlalchemy.dialects import postgresql
  from superset.db_engine_specs import load_engine_specs
  from superset.db_engine_specs.postgres import PostgresEngineSpec
  from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
  from superset.models.sql_lab import Query
  from superset.utils.database import get_example_database
  from tests.integration_tests.db_engine_specs.base_tests import TestDbEngineSpec
  from tests.integration_tests.fixtures.certificates import ssl_certificate
  from tests.integration_tests.fixtures.database import default_db_extra
label_desc: Use image node_id 15 for calling the TestPostgresDbEngineSpec obj's underlying member method code with example usage: obj.test_extract_errors() without return types