Improve errors throught codebase

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-20 01:09:24 +00:00
parent c3df9d791d
commit 6a719d815b
7 changed files with 79 additions and 89 deletions

View file

@ -39,23 +39,29 @@ func (e *executor) executeCreateStatement(ast *sql.CreateStatement) (out []inter
case *sql.Thing: case *sql.Thing:
key := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: what.ID} key := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: what.ID}
kv, _ := e.txn.Get(0, key.Encode()) if kv, err := e.txn.Get(0, key.Encode()); err != nil {
return nil, err
} else {
doc := item.New(kv, e.txn, key, e.ctx) doc := item.New(kv, e.txn, key, e.ctx)
if ret, err := create(doc, ast); err != nil { if ret, err := create(doc, ast); err != nil {
return nil, err return nil, err
} else if ret != nil { } else if ret != nil {
out = append(out, ret) out = append(out, ret)
} }
}
case *sql.Table: case *sql.Table:
key := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: uuid.NewV5(uuid.NewV4().UUID, ast.KV).String()} key := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: uuid.NewV5(uuid.NewV4().UUID, ast.KV).String()}
kv, _ := e.txn.Get(0, key.Encode()) if kv, err := e.txn.Get(0, key.Encode()); err != nil {
return nil, err
} else {
doc := item.New(kv, e.txn, key, e.ctx) doc := item.New(kv, e.txn, key, e.ctx)
if ret, err := create(doc, ast); err != nil { if ret, err := create(doc, ast); err != nil {
return nil, err return nil, err
} else if ret != nil { } else if ret != nil {
out = append(out, ret) out = append(out, ret)
} }
}
} }

View file

@ -36,6 +36,8 @@ import (
// _ "github.com/abcum/surreal/kvs/dendro" // _ "github.com/abcum/surreal/kvs/dendro"
) )
var QueryNotExecuted = fmt.Errorf("Query not executed")
var pool sync.Pool var pool sync.Pool
func init() { func init() {
@ -325,7 +327,7 @@ func (e *executor) execute(quit <-chan bool, send chan<- *Response) {
if err == nil { if err == nil {
res, err = e.operate(stm) res, err = e.operate(stm)
} else { } else {
res, err = []interface{}{}, fmt.Errorf("Query not executed") res, err = []interface{}{}, QueryNotExecuted
} }
rsp = &Response{ rsp = &Response{
@ -483,10 +485,6 @@ func status(e error) (s string) {
return "OK" return "OK"
case *kvs.DBError: case *kvs.DBError:
return "ERR_DB" return "ERR_DB"
case *kvs.TXError:
return "ERR_TX"
case *kvs.CKError:
return "ERR_CK"
case *kvs.KVError: case *kvs.KVError:
return "ERR_KV" return "ERR_KV"
case error: case error:

View file

@ -16,17 +16,6 @@ package kvs
import "fmt" import "fmt"
// DSError is an error which occurs when there is a
// problem with writing keys/values to the database.
type DSError struct {
Err error
}
// Error returns the string representation of the error.
func (e *DSError) Error() string {
return fmt.Sprintf("There was a problem connecting to the datastore")
}
// DBError is an error which occurs when there is a // DBError is an error which occurs when there is a
// problem with writing keys/values to the database. // problem with writing keys/values to the database.
type DBError struct { type DBError struct {
@ -38,28 +27,6 @@ func (e *DBError) Error() string {
return fmt.Sprintf("There was a problem writing to the database") return fmt.Sprintf("There was a problem writing to the database")
} }
// TXError is an error which occurs when there is a
// problem with a writable transaction.
type TXError struct {
Err error
}
// Error returns the string representation of the error.
func (e *TXError) Error() string {
return fmt.Sprintf("There was a problem with the transaction")
}
// CKError is an error which occurs when there is a
// problem with encrypting/decrypting values.
type CKError struct {
Err error
}
// Error returns the string representation of the error.
func (e *CKError) Error() string {
return fmt.Sprintf("This cipherkey used is not valid")
}
// KVError is an error which occurs when there is a // KVError is an error which occurs when there is a
// problem with a conditional put or delete. // problem with a conditional put or delete.
type KVError struct { type KVError struct {

View file

@ -23,6 +23,36 @@ type TX struct {
pntr *rixxdb.TX pntr *rixxdb.TX
} }
func one(all kvs.KV, err error) (kvs.KV, error) {
switch err {
case nil:
break
default:
return nil, &kvs.DBError{}
case rixxdb.ErrTxNotExpectedValue:
return nil, &kvs.KVError{}
}
return all, err
}
func many(all []kvs.KV, err error) ([]kvs.KV, error) {
switch err {
case nil:
break
default:
return nil, &kvs.DBError{}
case rixxdb.ErrTxNotExpectedValue:
return nil, &kvs.KVError{}
}
return all, err
}
func (tx *TX) Closed() bool { func (tx *TX) Closed() bool {
return tx.pntr.Closed() return tx.pntr.Closed()
} }
@ -36,7 +66,8 @@ func (tx *TX) Commit() error {
} }
func (tx *TX) Get(ver int64, key []byte) (kvs.KV, error) { func (tx *TX) Get(ver int64, key []byte) (kvs.KV, error) {
return tx.pntr.Get(ver, key) all, err := tx.pntr.Get(ver, key)
return one(all, err)
} }
func (tx *TX) GetL(ver int64, key []byte) ([]kvs.KV, error) { func (tx *TX) GetL(ver int64, key []byte) ([]kvs.KV, error) {
@ -45,7 +76,7 @@ func (tx *TX) GetL(ver int64, key []byte) ([]kvs.KV, error) {
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) GetP(ver int64, key []byte, max uint64) ([]kvs.KV, error) { func (tx *TX) GetP(ver int64, key []byte, max uint64) ([]kvs.KV, error) {
@ -54,7 +85,7 @@ func (tx *TX) GetP(ver int64, key []byte, max uint64) ([]kvs.KV, error) {
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) GetR(ver int64, beg []byte, end []byte, max uint64) ([]kvs.KV, error) { func (tx *TX) GetR(ver int64, beg []byte, end []byte, max uint64) ([]kvs.KV, error) {
@ -63,11 +94,12 @@ func (tx *TX) GetR(ver int64, beg []byte, end []byte, max uint64) ([]kvs.KV, err
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) Del(ver int64, key []byte) (kvs.KV, error) { func (tx *TX) Del(ver int64, key []byte) (kvs.KV, error) {
return tx.pntr.Del(ver, key) all, err := tx.pntr.Del(ver, key)
return one(all, err)
} }
func (tx *TX) DelC(ver int64, key []byte, exp []byte) (kvs.KV, error) { func (tx *TX) DelC(ver int64, key []byte, exp []byte) (kvs.KV, error) {
@ -80,7 +112,7 @@ func (tx *TX) DelL(ver int64, key []byte) ([]kvs.KV, error) {
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) DelP(ver int64, key []byte, max uint64) ([]kvs.KV, error) { func (tx *TX) DelP(ver int64, key []byte, max uint64) ([]kvs.KV, error) {
@ -89,7 +121,7 @@ func (tx *TX) DelP(ver int64, key []byte, max uint64) ([]kvs.KV, error) {
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) DelR(ver int64, beg []byte, end []byte, max uint64) ([]kvs.KV, error) { func (tx *TX) DelR(ver int64, beg []byte, end []byte, max uint64) ([]kvs.KV, error) {
@ -98,15 +130,17 @@ func (tx *TX) DelR(ver int64, beg []byte, end []byte, max uint64) ([]kvs.KV, err
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) Put(ver int64, key []byte, val []byte) (kvs.KV, error) { func (tx *TX) Put(ver int64, key []byte, val []byte) (kvs.KV, error) {
return tx.pntr.Put(ver, key, val) all, err := tx.pntr.Put(ver, key, val)
return one(all, err)
} }
func (tx *TX) PutC(ver int64, key []byte, val []byte, exp []byte) (kvs.KV, error) { func (tx *TX) PutC(ver int64, key []byte, val []byte, exp []byte) (kvs.KV, error) {
return tx.pntr.PutC(ver, key, val, exp) all, err := tx.pntr.PutC(ver, key, val, exp)
return one(all, err)
} }
func (tx *TX) PutL(ver int64, key []byte, val []byte) ([]kvs.KV, error) { func (tx *TX) PutL(ver int64, key []byte, val []byte) ([]kvs.KV, error) {
@ -115,7 +149,7 @@ func (tx *TX) PutL(ver int64, key []byte, val []byte) ([]kvs.KV, error) {
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) PutP(ver int64, key []byte, val []byte, max uint64) ([]kvs.KV, error) { func (tx *TX) PutP(ver int64, key []byte, val []byte, max uint64) ([]kvs.KV, error) {
@ -124,7 +158,7 @@ func (tx *TX) PutP(ver int64, key []byte, val []byte, max uint64) ([]kvs.KV, err
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }
func (tx *TX) PutR(ver int64, key []byte, val []byte, exp []byte, max uint64) ([]kvs.KV, error) { func (tx *TX) PutR(ver int64, key []byte, val []byte, exp []byte, max uint64) ([]kvs.KV, error) {
@ -133,5 +167,5 @@ func (tx *TX) PutR(ver int64, key []byte, val []byte, exp []byte, max uint64) ([
for i, v := range all { for i, v := range all {
out[i] = v out[i] = v
} }
return out, err return many(out, err)
} }

View file

@ -43,24 +43,14 @@ func (e *BlankError) Error() string {
return fmt.Sprint("You need to specify a namespace and a database to use") return fmt.Sprint("You need to specify a namespace and a database to use")
} }
// NSError represents an error that occured when switching access. // PermsError represents an error that occured when switching access.
type NSError struct { type PermsError struct {
NS string Resource string
} }
// Error returns the string representation of the error. // Error returns the string representation of the error.
func (e *NSError) Error() string { func (e *PermsError) Error() string {
return fmt.Sprintf("You don't have permission to access the '%s' namespace", e.NS) return fmt.Sprintf("You don't have permission to access the '%s' resource", e.Resource)
}
// DBError represents an error that occured when switching access.
type DBError struct {
DB string
}
// Error returns the string representation of the error.
func (e *DBError) Error() string {
return fmt.Sprintf("You don't have permission to access the '%s' database", e.DB)
} }
// ParseError represents an error that occurred during parsing. // ParseError represents an error that occurred during parsing.

View file

@ -75,7 +75,7 @@ func (o *options) ns(ns string) (err error) {
// to be able to specify this namespace. // to be able to specify this namespace.
if o.auth.Possible.NS != "*" && o.auth.Possible.NS != ns { if o.auth.Possible.NS != "*" && o.auth.Possible.NS != ns {
return &NSError{NS: ns} return &PermsError{Resource: ns}
} }
// Specify the NS on the context session, so // Specify the NS on the context session, so
@ -95,7 +95,7 @@ func (o *options) db(db string) (err error) {
// to be able to specify this namespace. // to be able to specify this namespace.
if o.auth.Possible.DB != "*" && o.auth.Possible.DB != db { if o.auth.Possible.DB != "*" && o.auth.Possible.DB != db {
return &DBError{DB: db} return &PermsError{Resource: db}
} }
// Specify the DB on the context session, so // Specify the DB on the context session, so

View file

@ -16,29 +16,24 @@ package web
import ( import (
"github.com/abcum/fibre" "github.com/abcum/fibre"
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
) )
func errors(val error, c *fibre.Context) { func errors(val error, c *fibre.Context) {
if c.Socket() != nil {
return
}
var code int var code int
var info string var info string
switch e := val.(type) { switch e := val.(type) {
default: default:
code, info = 400, e.Error() code, info = 400, e.Error()
case *kvs.DBError: case *sql.PermsError:
code, info = 503, e.Error()
case *kvs.TXError:
code, info = 500, e.Error()
case *kvs.KVError:
code, info = 409, e.Error()
case *kvs.CKError:
code, info = 403, e.Error() code, info = 403, e.Error()
case *sql.NSError: case *sql.BlankError:
code, info = 403, e.Error()
case *sql.DBError:
code, info = 403, e.Error() code, info = 403, e.Error()
case *sql.QueryError: case *sql.QueryError:
code, info = 401, e.Error() code, info = 401, e.Error()