errors.go 3.4 KB

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