123456789_123456789_123456789_123456789_123456789_

Exception: Mysql2::Error

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, StandardError
Instance Chain:
self, StandardError
Inherits: StandardError
Defined in: lib/mysql2/error.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

  • #error
  • #clean_message(message) private

    In MySQL 5.5+ error messages are always constructed server-side as UTF-8 then returned in the encoding set by the character_set_results system variable.

Constructor Details

.new(msg, server_version = nil, error_number = nil, sql_state = nil) ⇒ Error

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 53

def initialize(msg, server_version = nil, error_number = nil, sql_state = nil)
  @server_version = server_version
  @error_number = error_number
  @sql_state = sql_state ? sql_state.encode(**ENCODE_OPTS) : nil

  super(clean_message(msg))
end

Class Method Details

.new_with_args(msg, server_version, error_number, sql_state)

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 61

def self.new_with_args(msg, server_version, error_number, sql_state)
  error_class = CODES.fetch(error_number, self)
  error_class.new(msg, server_version, error_number, sql_state)
end

Instance Attribute Details

#errno (readonly)

Alias for #error_number.

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 50

alias errno error_number

#error_number (readonly) Also known as: #errno

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 47

attr_reader :error_number, :sql_state

#sql_state (readonly)

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 47

attr_reader :error_number, :sql_state

Instance Method Details

#clean_message(message) (private)

In MySQL 5.5+ error messages are always constructed server-side as UTF-8 then returned in the encoding set by the character_set_results system variable.

See dev.mysql.com/doc/refman/5.5/en/charset-errors.html for more context.

Before MySQL 5.5 error message template strings are in whatever encoding is associated with the error message language. See dev.mysql.com/doc/refman/5.1/en/error-message-language.html for more information.

The issue is that the user-data inserted in the message could potentially be in any encoding MySQL supports and is insert into the latin1, euckr or koi8r string raw. Meaning there’s a high probability the string will be corrupt encoding-wise.

See dev.mysql.com/doc/refman/5.1/en/charset-errors.html for more information.

So in an attempt to make sure the error message string is always in a valid encoding, we’ll assume UTF-8 and clean the string of anything that’s not a valid UTF-8 character.

Returns a valid UTF-8 string.

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 93

def clean_message(message)
  if @server_version && @server_version > 50500
    message.encode(**ENCODE_OPTS)
  else
    message.encode(Encoding::UTF_8, **ENCODE_OPTS)
  end
end

#error

[ GitHub ]

  
# File 'lib/mysql2/error.rb', line 51

alias error message