mysqlerrors

package
v0.0.0-...-652937c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseMySQLError

func ParseMySQLError(err error, entity *Entity, fallback error) error

ParseMySQLError parses a MySQL error and returns a more specific error based on the error code. If the error is nil, it returns nil. If the error is a duplicate entry error (ERROR 1062), it returns the corresponding error for the entity. If the error is a foreign key constraint error (ERROR 1451), it returns the corresponding error for the entity or table. If the error is a foreign key constraint error indicating that the entity does not exist (ERROR 1452), it returns the corresponding error for the entity or table. If the error does not match any specific error code, it returns the fallback error.

Types

type Entity

type Entity string
var (
	EntitySheet   Entity = "sheets"
	EntityRoaster Entity = "roaster"
	EntityBeans   Entity = "beans"
	EntityShot    Entity = "shots"
)

func ExtractTableNameFromError1451

func ExtractTableNameFromError1451(err mysql.MySQLError) (Entity, error)

ExtractTableNameFromError1451 extracts the table name from a MySQL error with code 1451. It uses a regular expression to find the table name in the error message. If a match is found, it returns the table name. Otherwise, it returns an error.

Example error message: "Cannot delete or update a parent row: a foreign key constraint fails (`espresso-api`.`beans`, CONSTRAINT `beans_ibfk_1` FOREIGN KEY (`roaster_id`) REFERENCES `roasters` (`id`))"

func ExtractTableNameFromError1452

func ExtractTableNameFromError1452(err mysql.MySQLError) (Entity, error)

ExtractTableNameFromError1452 extracts the table name from a MySQL error with code 1452. It uses a regular expression to find the table name in the error message. If a match is found, it returns the table name. Otherwise, it returns an error.

Example error message: "Cannot add or update a child row: a foreign key constraint fails (`espresso-api`.`shots`, CONSTRAINT `shots_ibfk_1` FOREIGN KEY (`sheet_id`) REFERENCES `sheets` (`id`))"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL