Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "database/sql"
- "errors"
- "fmt"
- "gotesting/db"
- "log"
- "net/http"
- "github.com/mattn/go-sqlite3"
- )
- type query[RES, ARG any] func(context.Context, ARG) (RES, error)
- func RunQuery[RES, ARG any](ctx context.Context, query query[RES, ARG], arg ARG) (RES, int, error) {
- res, err := query(ctx, arg)
- if err != nil {
- switch true {
- case ctx.Err() == context.DeadlineExceeded:
- return res, http.StatusGatewayTimeout, errors.New("query took too long")
- case errors.Is(err, sql.ErrNoRows):
- return res, http.StatusNotFound, errors.New("datas not found")
- case errors.As(err, &sqlite3.Error{ExtendedCode: sqlite3.ErrConstraintUnique}):
- return res, http.StatusBadRequest, errors.New("one or more fields already exists in the database")
- case errors.As(err, &sqlite3.Error{ExtendedCode: sqlite3.ErrConstraintCheck}):
- return res, http.StatusBadRequest, errors.New("one or more fields in the query are invalid")
- default:
- log.Println(err)
- return res, http.StatusInternalServerError, errors.New("database error")
- }
- }
- return res, 0, nil
- }
- func main() {
- db.InitDB()
- messages, status, err := RunQuery(context.Background(), db.Query.GetYourMessages, db.GetYourMessagesParams{
- UserID: 1,
- ChatID: 1,
- })
- if err != nil {
- log.Fatal(status, err)
- }
- fmt.Println(messages)
- }
Advertisement
Add Comment
Please, Sign In to add comment