github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/tidbparser/dependency/mysql/error.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain 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,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package mysql
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  )
    20  
    21  // Portable analogs of some common call errors.
    22  var (
    23  	ErrBadConn       = errors.New("connection was bad")
    24  	ErrMalformPacket = errors.New("malform packet error")
    25  )
    26  
    27  // SQLError records an error information, from executing SQL.
    28  type SQLError struct {
    29  	Code    uint16
    30  	Message string
    31  	State   string
    32  }
    33  
    34  // Error prints errors, with a formatted string.
    35  func (e *SQLError) Error() string {
    36  	return fmt.Sprintf("ERROR %d (%s): %s", e.Code, e.State, e.Message)
    37  }
    38  
    39  // NewErr generates a SQL error, with an error code and default format specifier defined in MySQLErrName.
    40  func NewErr(errCode uint16, args ...interface{}) *SQLError {
    41  	e := &SQLError{Code: errCode}
    42  
    43  	if s, ok := MySQLState[errCode]; ok {
    44  		e.State = s
    45  	} else {
    46  		e.State = DefaultMySQLState
    47  	}
    48  
    49  	if format, ok := MySQLErrName[errCode]; ok {
    50  		e.Message = fmt.Sprintf(format, args...)
    51  	} else {
    52  		e.Message = fmt.Sprint(args...)
    53  	}
    54  
    55  	return e
    56  }
    57  
    58  // NewErrf creates a SQL error, with an error code and a format specifier.
    59  func NewErrf(errCode uint16, format string, args ...interface{}) *SQLError {
    60  	e := &SQLError{Code: errCode}
    61  
    62  	if s, ok := MySQLState[errCode]; ok {
    63  		e.State = s
    64  	} else {
    65  		e.State = DefaultMySQLState
    66  	}
    67  
    68  	e.Message = fmt.Sprintf(format, args...)
    69  
    70  	return e
    71  }