github.com/XiaoMi/Gaea@v1.2.5/mysql/error.go (about)

     1  // Copyright 2016 The kingshard Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  // Package mysql error.go contains error code, SQLSTATE value, and message string.
    16  // The struct SqlError contains above informations and used in programs.
    17  
    18  // Copyright 2019 The Gaea Authors. All Rights Reserved.
    19  //
    20  // Licensed under the Apache License, Version 2.0 (the "License");
    21  // you may not use this file except in compliance with the License.
    22  // You may obtain a copy of the License at
    23  //
    24  //     http://www.apache.org/licenses/LICENSE-2.0
    25  //
    26  // Unless required by applicable law or agreed to in writing, software
    27  // distributed under the License is distributed on an "AS IS" BASIS,
    28  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    29  // See the License for the specific language governing permissions and
    30  // limitations under the License.
    31  
    32  package mysql
    33  
    34  import (
    35  	"errors"
    36  	"fmt"
    37  )
    38  
    39  var (
    40  	// ErrBadConn bad connection error
    41  	ErrBadConn = errors.New("connection was bad")
    42  	// ErrMalformPacket packet error
    43  	ErrMalformPacket = errors.New("Malform packet error")
    44  	// ErrTxDone transaction done error
    45  	ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
    46  )
    47  
    48  // SQLError contains error code、SQLSTATE and message string
    49  // https://dev.mysql.com/doc/refman/5.7/en/server-error-reference.html
    50  type SQLError struct {
    51  	Code    uint16
    52  	Message string
    53  	State   string
    54  }
    55  
    56  func (se *SQLError) Error() string {
    57  	return fmt.Sprintf("ERROR %d (%s): %s", se.Code, se.State, se.Message)
    58  }
    59  
    60  // SQLCode returns the internal MySQL error code.
    61  func (se *SQLError) SQLCode() uint16 {
    62  	return se.Code
    63  }
    64  
    65  // SQLState returns the SQLSTATE value.
    66  func (se *SQLError) SQLState() string {
    67  	return se.State
    68  }
    69  
    70  // NewDefaultError default mysql error, must adapt errname message format
    71  func NewDefaultError(errCode uint16, args ...interface{}) *SQLError {
    72  	e := new(SQLError)
    73  	e.Code = errCode
    74  
    75  	if s, ok := MySQLState[errCode]; ok {
    76  		e.State = s
    77  	} else {
    78  		e.State = DefaultMySQLState
    79  	}
    80  
    81  	if format, ok := MySQLErrName[errCode]; ok {
    82  		e.Message = fmt.Sprintf(format, args...)
    83  	} else {
    84  		e.Message = fmt.Sprint(args...)
    85  	}
    86  
    87  	return e
    88  }
    89  
    90  // NewError create new error with specified code and message
    91  func NewError(errCode uint16, message string) *SQLError {
    92  	e := new(SQLError)
    93  	e.Code = errCode
    94  
    95  	if s, ok := MySQLState[errCode]; ok {
    96  		e.State = s
    97  	} else {
    98  		e.State = DefaultMySQLState
    99  	}
   100  
   101  	e.Message = message
   102  
   103  	return e
   104  }
   105  
   106  // NewErrf creates a SQL error, with an error code and a format specifier.
   107  func NewErrf(errCode uint16, format string, args ...interface{}) *SQLError {
   108  	e := &SQLError{Code: errCode}
   109  
   110  	if s, ok := MySQLState[errCode]; ok {
   111  		e.State = s
   112  	} else {
   113  		e.State = DefaultMySQLState
   114  	}
   115  
   116  	e.Message = fmt.Sprintf(format, args...)
   117  
   118  	return e
   119  }