errors.go 3.6 KB

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