errors.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 `Config.MaxAllowedPacket`")
  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 defaultLogger = 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 ...any)
  39. }
  40. // NopLogger is a nop implementation of the Logger interface.
  41. type NopLogger struct{}
  42. // Print implements Logger interface.
  43. func (nl *NopLogger) Print(_ ...any) {}
  44. // SetLogger is used to set the default logger for critical errors.
  45. // The initial logger is os.Stderr.
  46. func SetLogger(logger Logger) error {
  47. if logger == nil {
  48. return errors.New("logger is nil")
  49. }
  50. defaultLogger = logger
  51. return nil
  52. }
  53. // MySQLError is an error type which represents a single MySQL error
  54. type MySQLError struct {
  55. Number uint16
  56. SQLState [5]byte
  57. Message string
  58. }
  59. func (me *MySQLError) Error() string {
  60. if me.SQLState != [5]byte{} {
  61. return fmt.Sprintf("Error %d (%s): %s", me.Number, me.SQLState, me.Message)
  62. }
  63. return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
  64. }
  65. func (me *MySQLError) Is(err error) bool {
  66. if merr, ok := err.(*MySQLError); ok {
  67. return merr.Number == me.Number
  68. }
  69. return false
  70. }