errors.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "database/sql/driver"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "log"
  15. "os"
  16. )
  17. // Various errors the driver might return. Can change between driver versions.
  18. var (
  19. ErrInvalidConn = errors.New("invalid connection")
  20. ErrMalformPkt = errors.New("malformed packet")
  21. ErrNoTLS = errors.New("TLS requested but server does not support TLS")
  22. ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
  23. ErrNativePassword = errors.New("this user requires mysql native password authentication.")
  24. 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")
  25. ErrUnknownPlugin = errors.New("this authentication plugin is not supported")
  26. ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+")
  27. ErrPktSync = errors.New("commands out of sync. You can't run this command now")
  28. ErrPktSyncMul = errors.New("commands out of sync. Did you run multiple statements at once?")
  29. ErrPktTooLarge = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
  30. ErrBusyBuffer = errors.New("busy buffer")
  31. // errBadConnNoWrite is used for connection errors where nothing was sent to the database yet.
  32. // If this happens first in a function starting a database interaction, it should be replaced by driver.ErrBadConn
  33. // to trigger a resend.
  34. // See https://github.com/go-sql-driver/mysql/pull/302
  35. errBadConnNoWrite = errors.New("bad connection")
  36. )
  37. var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
  38. // Logger is used to log critical error messages.
  39. type Logger interface {
  40. Print(v ...interface{})
  41. }
  42. // SetLogger is used to set the logger for critical errors.
  43. // The initial logger is os.Stderr.
  44. func SetLogger(logger Logger) error {
  45. if logger == nil {
  46. return errors.New("logger is nil")
  47. }
  48. errLog = logger
  49. return nil
  50. }
  51. // MySQLError is an error type which represents a single MySQL error
  52. type MySQLError struct {
  53. Number uint16
  54. Message string
  55. }
  56. func (me *MySQLError) Error() string {
  57. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  58. }
  59. // MySQLWarnings is an error type which represents a group of one or more MySQL
  60. // warnings
  61. type MySQLWarnings []MySQLWarning
  62. func (mws MySQLWarnings) Error() string {
  63. var msg string
  64. for i, warning := range mws {
  65. if i > 0 {
  66. msg += "\r\n"
  67. }
  68. msg += fmt.Sprintf(
  69. "%s %s: %s",
  70. warning.Level,
  71. warning.Code,
  72. warning.Message,
  73. )
  74. }
  75. return msg
  76. }
  77. // MySQLWarning is an error type which represents a single MySQL warning.
  78. // Warnings are returned in groups only. See MySQLWarnings
  79. type MySQLWarning struct {
  80. Level string
  81. Code string
  82. Message string
  83. }
  84. func (mc *mysqlConn) getWarnings() (err error) {
  85. rows, err := mc.Query("SHOW WARNINGS", nil)
  86. if err != nil {
  87. return
  88. }
  89. var warnings = MySQLWarnings{}
  90. var values = make([]driver.Value, 3)
  91. for {
  92. err = rows.Next(values)
  93. switch err {
  94. case nil:
  95. warning := MySQLWarning{}
  96. if raw, ok := values[0].([]byte); ok {
  97. warning.Level = string(raw)
  98. } else {
  99. warning.Level = fmt.Sprintf("%s", values[0])
  100. }
  101. if raw, ok := values[1].([]byte); ok {
  102. warning.Code = string(raw)
  103. } else {
  104. warning.Code = fmt.Sprintf("%s", values[1])
  105. }
  106. if raw, ok := values[2].([]byte); ok {
  107. warning.Message = string(raw)
  108. } else {
  109. warning.Message = fmt.Sprintf("%s", values[0])
  110. }
  111. warnings = append(warnings, warning)
  112. case io.EOF:
  113. return warnings
  114. default:
  115. rows.Close()
  116. return
  117. }
  118. }
  119. }