-1

t

I am trying to convert an err in Go to go-sqlite3.Error, but it fails always. Above image represents the snapshot of my debug windows, which shows that the err is of type go-sqlite3.Error

I am using below code to type cast.

import (
    "github.com/mattn/go-sqlite3"
)

if err != nil {
    if sqlite3Err, ok := err.(*sqlite3.Error); ok {
        if sqlite3Err.Code == sqlite3.ErrConstraint && sqlite3Err.ExtendedCode == 1555 {
            // SQLITE3 ERROR 1555 : PRIMARY KEY CONSTRAINT ERROR
            return errors.New("Log Error")
        }
    }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ashish Mittal
  • 643
  • 3
  • 12
  • 32
  • 2
    What do you see if you print its type? `fmt.Printf("%T", err)`? – icza Aug 26 '19 at 07:58
  • 2
    There are no type casts in Go. What your code tries to do is a type assertion. If this tape assertion fails it means you botched up the types. You should read the documentation of the function which returns your error and take a close look at its signature. – Volker Aug 26 '19 at 08:14

1 Answers1

3

try the following example. err.(*sqlite3.Error) is changed to err.(sqlite3.Error)

if sqlite3Err, ok := err.(sqlite3.Error); ok {
    if sqlite3Err.Code == sqlite3.ErrConstraint &&
        sqlite3Err.ExtendedCode == 1555 {
        // logic 
    }
}
Akshay Deep Giri
  • 3,139
  • 5
  • 25
  • 33