errors.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "errors"
  11. "fmt"
  12. "log"
  13. "os"
  14. )
  15. // Various errors the driver might return. Can change between driver versions.
  16. var (
  17. ErrInvalidConn = errors.New("invalid connection")
  18. ErrMalformPkt = errors.New("malformed packet")
  19. ErrNoTLS = errors.New("TLS requested but server does not support TLS")
  20. ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
  21. ErrNativePassword = errors.New("this user requires mysql native password authentication.")
  22. ErrOldPassword = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
  23. ErrUnknownPlugin = errors.New("this authentication plugin is not supported")
  24. ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+")
  25. ErrPktSync = errors.New("commands out of sync. You can't run this command now")
  26. ErrPktSyncMul = errors.New("commands out of sync. Did you run multiple statements at once?")
  27. ErrPktTooLarge = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
  28. ErrBusyBuffer = errors.New("busy buffer")
  29. // errBadConnNoWrite is used for connection errors where nothing was sent to the database yet.
  30. // If this happens first in a function starting a database interaction, it should be replaced by driver.ErrBadConn
  31. // to trigger a resend.
  32. // See https://github.com/go-sql-driver/mysql/pull/302
  33. errBadConnNoWrite = errors.New("bad connection")
  34. )
  35. var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
  36. // Logger is used to log critical error messages.
  37. type Logger interface {
  38. Print(v ...interface{})
  39. }
  40. // SetLogger is used to set the logger for critical errors.
  41. // The initial logger is os.Stderr.
  42. func SetLogger(logger Logger) error {
  43. if logger == nil {
  44. return errors.New("logger is nil")
  45. }
  46. errLog = logger
  47. return nil
  48. }
  49. // MySQLError is an error type which represents a single MySQL error
  50. type MySQLError struct {
  51. Number uint16
  52. SQLState [5]byte
  53. Message string
  54. }
  55. func (me *MySQLError) Error() string {
  56. if me.SQLState != [5]byte{} {
  57. return fmt.Sprintf("Error %d (%s): %s", me.Number, me.SQLState, me.Message)
  58. }
  59. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  60. }
  61. func (me *MySQLError) Is(err error) bool {
  62. if merr, ok := err.(*MySQLError); ok {
  63. return merr.Number == me.Number
  64. }
  65. return false
  66. }