Handle AddChat errors from SQLite.
This commit is contained in:
parent
f3bb7b7494
commit
d2659f9afc
1 changed files with 8 additions and 4 deletions
|
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
|
@ -22,15 +23,18 @@ func NewChatModel(ctx context.Context, db *sqlx.DB) *ChatModel {
|
|||
return &ChatModel{ctx, db}
|
||||
}
|
||||
|
||||
func (cm *ChatModel) AddChat(id int, name string) (*Chat, error) {
|
||||
var chat *Chat
|
||||
err := cm.db.GetContext(cm.ctx, chat,
|
||||
func (cm *ChatModel) AddChat(id int, name string) (chat *Chat, err error) {
|
||||
chat = &Chat{}
|
||||
err = cm.db.GetContext(cm.ctx, chat,
|
||||
`insert into chats (id, name) values (?, ?)`, id, name)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return chat, nil
|
||||
}
|
||||
var serr sqlite3.Error
|
||||
if errors.As(err, &serr) {
|
||||
switch serr.ExtendedCode {
|
||||
case sqlite3.ErrConstraintUnique:
|
||||
case sqlite3.ErrConstraintUnique, sqlite3.ErrConstraintPrimaryKey:
|
||||
return nil, ErrUniqueConstraint.Do(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue