Skip to content

Commit

Permalink
Fixed error mapper for constraint errors (#63)
Browse files Browse the repository at this point in the history
* Fixed error mapper for constraint errors

Fixed error mapper for constraint errors and added error 1451.

* Fixed error mapper regardless of whether SQLSTATE is present or not

* Combined the two constraint errors

Co-authored-by: Surya Asriadie <surya.asriadie@gmail.com>

---------

Co-authored-by: Surya Asriadie <surya.asriadie@gmail.com>
  • Loading branch information
nItroFreeZer and Fs02 committed Jan 23, 2024
1 parent 57be534 commit 685eddb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,28 @@ func errorMapper(err error) error {
msg = err.Error()
errCodeSep = ':'
errCodeIndex = strings.IndexRune(msg, errCodeSep)
errStateSep = '('
errStateIndex = -1
)

if errCodeIndex < 0 {
errCodeIndex = 0
}

switch msg[:errCodeIndex] {
errStateIndex = strings.IndexRune(msg[:errCodeIndex], errStateSep)
errStateIndex--
if errStateIndex < 0 {
errStateIndex = errCodeIndex
}

switch (msg[:errCodeIndex])[:errStateIndex] {
case "Error 1062":
return rel.ConstraintError{
Key: sql.ExtractString(msg, "key '", "'"),
Type: rel.UniqueConstraint,
Err: err,
}
case "Error 1452":
case "Error 1451", "Error 1452":
return rel.ConstraintError{
Key: sql.ExtractString(msg, "CONSTRAINT `", "`"),
Type: rel.ForeignKeyConstraint,
Expand Down

0 comments on commit 685eddb

Please sign in to comment.