Which standard is used to handle SQLSTATE in PDOException?

2

I do not know if I do not understand how SQLSTATE works, however in MySQL many errors do not have SQLSTATE , and when I get Exception from PDO , the code that is returned to me is SQLSTATE instead of the error code that is specific to the bank's driver.

The application I'm working on will not use other banks, so I do not see a problem getting the driver code, but the question that intrigues me is that if by default ::PDOException->getCode() returns a SQLSTATE , then it should be much used, but I did not understand how, since it is ambiguous.

  

Example : Let's suppose that I have a class that does common procedures and only needs to return messages to the user, if it worked, or did it and why.

     

To avoid the same code, I would have a method that would handle these exceptions, getting the exception, where the method will see what type and its error code, and return me a default message.

     

If the returned SQLSTATE code was 23000, where it is returned in insert, delete, update operations that have integrity issues, fields that can not be null, and a variety of things, how would I distinguish and deliver the correct message?

I made an implementation of the example.

public function remover()
{
    ids = filter_input(INPUT_POST, 'checkbox', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY);
    try
    {
        $this->con->beginTransaction();
        $cidadeDao = new CidadeDao($this->con);
        $n = $cidadeDao->delete($ids, $this->usuario);
        $this->con->commit();
        $this->response['message'] = "$n cidades foram excluídas com sucesso";
        $this->response['rows'] = $n;
    } 
    catch (Exception $ex) 
    {
        $this->handleResponseException($ex);
    }

    $this->view->renderJSON($this->response);
}

And a handler to handle these common exceptions and just return an error message (in that case I do not need to handle anything, it's just a message myself)

protected function handleResponseException(Exception $ex)
{
    $this->response['error'] = true;

    switch( get_class($ex) )        
    {
        case 'UnexpectedValueException':
            // ..
        break;

        case 'PDOException':

            try { $this->con->rollBack(); }
            catch (Exception $ex) { /** ... **/ }

            switch( (int) $ex->getCode() )
            {
                case 1022: 
                    $this->response['message'] = "Registro duplicado";
                break;

                case 1451:
                    $this->response['message'] = "Há registros relacionados"
                break;

                default:
                    $this->response['message'] = "Ocorreu um erro na consulta com o banco de dados";
                break;
            }    
        break;
    }    
}
Assuming that $ex->getCode() returned the driver code, I would know which message to deliver, but it returns a SQLSTATE , how will I distinguish this from the ambiguity of the codes?

There is some standard to do this, or what I am doing can be considered a bad practice (in my view solves the problem and avoids code, but I do not know if it would be correct).

    
asked by anonymous 20.07.2016 / 18:03

0 answers